无法将某些数据传输到新页面
unable to transfer some data to a new page
我想在搜索布局部分的 android 项目中显示从 json 获得的数据,但数据不可见。你能帮帮我吗?
我的代码:
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);
}
});
}
搜索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
国家项目
<?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>
您好,我想在搜索布局部分的 android 项目中显示从 json 获得的数据,但是数据不可见。你能帮帮我吗?
您正在主 activity 中检索数据并通过您的意图发送数据
intent.putExtra("city", country.getText().toString()); intent.putExtra("temp",temp.getText().toString());
但是,您没有获取通过片段中的意图发送的数据。
您必须根据发送的键值来获取它们。
在片段中的 onCreateView() 中,您可以通过以下形式获取通过意图发送的每个数据:
String city = getArguments().getString("city");
我想在搜索布局部分的 android 项目中显示从 json 获得的数据,但数据不可见。你能帮帮我吗?
我的代码:
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);
}
});
}
搜索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 国家项目
<?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>
您好,我想在搜索布局部分的 android 项目中显示从 json 获得的数据,但是数据不可见。你能帮帮我吗?
您正在主 activity 中检索数据并通过您的意图发送数据
intent.putExtra("city", country.getText().toString()); intent.putExtra("temp",temp.getText().toString());
但是,您没有获取通过片段中的意图发送的数据。 您必须根据发送的键值来获取它们。 在片段中的 onCreateView() 中,您可以通过以下形式获取通过意图发送的每个数据:
String city = getArguments().getString("city");