Skip to content

unable to transfer some data to a new page

I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?

My Code:

public void searchView (View view){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        SearchFragment searchFragment = new SearchFragment();
        fragmentTransaction.replace(R.id.frameLayout, searchFragment).commit();

        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                city = cityAdd.getEditText().getText().toString().trim();
                String url = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+apikey;
                RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            Log.d("Temperature",(url));
                            JSONObject temps = response.getJSONObject("main");
                            String temperatures = temps.getString("temp");
                            country.setText(city);
                            temp.setText(temperatures);
                            Intent intent = new Intent(MainActivity.this, SearchFragment.class);
                            intent.putExtra("city", country.getText().toString());
                            intent.putExtra("temp",temp.getText().toString());
                            startActivity(intent);


                        } catch (JSONException e) {
                            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                        }
                    }
                }, new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, "Please check the city name", Toast.LENGTH_SHORT).show();

                    }
                }

                );
                queue.add(request);

            }
        });

    }

Search Fragment.java

package com.nisaefendioglu.weatherapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class SearchFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.search_layout, container,false);
        TextView country = viewGroup.findViewById(R.id.country);
        TextView temp = viewGroup.findViewById(R.id.temp);
        TextView back = viewGroup.findViewById(R.id.back);

        return viewGroup;



    }




}

XML Country İtem

        <?xml version="1.0" encoding="utf-8"?>
        <com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:cardElevation="3dp"
        android:layout_marginHorizontal="8dp"
        android:layout_marginTop="8dp">
        
       <RelativeLayout
       android:id="@+id/relativeLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginVertical="7dp"
       android:layout_marginHorizontal="15dp">
        
        
       <TextView
            
    
    android:id="@+id/countryName"
    android:layout_width="wrap_content"
     android:layout_height="40dp"
     android:layout_alignParentStart="true"
     android:layout_marginEnd="30dp"
     android:textColor="@color/black"
     android:text="Türkiye"
     android:layout_centerVertical="true"
     android:gravity="center"
      android:textSize="15dp" ></TextView>
    
     <TextView
     android:id="@+id/temperature"
    android:layout_width="wrap_content"
     android:layout_height="40dp"
    android:layout_alignParentEnd="true"
     android:layout_marginEnd="30dp"
      android:textColor="@color/black"
      android:text="Temperature"
       android:layout_centerVertical="true"
      android:gravity="center"
     android:textSize="15dp" ></TextView>
        
      </RelativeLayout>
      </com.google.android.material.card.MaterialCardView>
    



Main xml
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:orientation="vertical">
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="12dp"
                >
        
            <EditText
                android:layout_width="270dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:hint="Enter City" />
            <Button
                android:onClick="searchView"
                android:id="@+id/enterButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="300dp"
                android:text="Enter"
                android:textAllCaps="false"
                />
            </RelativeLayout>
        
            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/weather"/>
        
            <RelativeLayout
                android:id="@+id/activityRelative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="12dp"
                >
        
                <TextView
                    android:id="@+id/text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_marginStart="30dp"
                    android:textColor="@color/black"
                    android:text="Country Name"
                    android:textSize="15dp" ></TextView>
        
        
                <TextView
                    android:id="@+id/text2"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="30dp"
                    android:text="Temperature"
                    android:textColor="@color/black"
                    android:textSize="15dp" ></TextView>
        
            </RelativeLayout>
        
        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/countries"
                tools:listitem="@layout/country_item_layout"
                app:layoutManager="LinearLayoutManager"
                />
        
        
        </FrameLayout>
        </LinearLayout>
    
    
    Search Layout
    
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">
        
        
                <TextView
                    android:id="@+id/country"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_marginStart="30dp"
                    android:textColor="@color/black"
                    android:text=""
                    android:textSize="15dp" ></TextView>
        
        
                <TextView
                    android:id="@+id/temp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
       android:layout_alignParentEnd="true"
     android:layout_marginEnd="30dp"
      android:text=""
    android:textColor="@color/black"
       android:textSize="15dp" ></TextView>
     <TextView
      android:onClick="tempBack"
       android:id="@+id/back"
      android:layout_width="50dp"
      android:layout_height="50dp"
     android:layout_gravity="left"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="200dp"
       android:gravity="center"
        android:text="←"
      android:textColor="#ff793f"
      android:textSize="40dp"></TextView>
        
        
     </LinearLayout>

Hello, I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?

Answer

You are retrieving your data in your main activity and sending the data through your intent

intent.putExtra("city", country.getText().toString()); intent.putExtra("temp",temp.getText().toString());

However, you are not fetching the data that you sent through intent in your fragment. You have to fetch them according to the key values by which you sent. In your onCreateView() in your fragment, you can fetch each data you sent through your intent in the form:

String city = getArguments().getString("city");