我想活动发送片段。我在下面的照片中看到了错误。片段数据的活动发送。我想做listView onClick我的片段il值临时数据。片段想要在setOnItemClickListener
时看到湿度值,临时值MainActivity.java
public class FragmentA extends Fragment implements SendData {
private TextView textView, textView2,textView3,textView4;
View view ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a,container,false) ;
textView4= (TextView) view. findViewById(R.id.textIsım);
textView3= (TextView) view.findViewById(R.id.textView3);
textView2= (TextView) view.findViewById(R.id.textView2);
textView= (TextView) view. findViewById(R.id.textView);
return view;
}
@Override
public void gonder(String isim) {
Factory.getInstance().havaModel(isim,APPID_KEY).enqueue(new Callback<HavaModel>() {
@Override
public void onResponse(Call<HavaModel> call, Response<HavaModel> response) {
textView.setText(Float.toString((float) (response.body().main.temp-273.15)));
textView2.setText(Float.toString(response.body().coord.lon));
textView3.setText(Integer.toString(response.body().main.humidity)+" %");
textView4.setText(response.body().name);
}
@Override
public void onFailure(Call<HavaModel> call, Throwable t) {
}
});
}
}
FragmentA.java
public interface SendData {
public void gonder(String isim) ;
}
SendData.java
{{1}}
非常感谢你 你的建议对我很重要 我希望你能帮助我
答案 0 :(得分:1)
您必须通过调用您的活动来设置您的界面。
您的 MainActivity 应该是这样的。
public class MainActivity extends AppCompatActivity {
private String text ;
private TextView textView, textView2,textView3,textView4;
private SearchView searchView;
private ListView listView ;
private SendData sendData; // initialize it globally
private String[] il={"Adana", "Adıyaman", "Afyon", "Ağrı", "Amasya", "Ankara", "Antalya", "Artvin",
"Aydın", "Balıkesir", "Bilecik", "Bingöl", "Bitlis", "Bolu", "Burdur", "Bursa", "Çanakkale",
"Çankırı", "Çorum", "Denizli", "Diyarbakır", "Edirne", "Elazığ", "Erzincan", "Erzurum", "Eskişehir",
"Gaziantep", "Giresun", "Gümüşhane", "Hakkari", "Hatay", "Isparta", "Mersin", "İstanbul", "İzmir",
"Kars", "Kastamonu", "Kayseri", "Kırklareli", "Kırşehir", "Kocaeli", "Konya", "Kütahya", "Malatya",
"Manisa", "Kahramanmaraş", "Mardin", "Muğla", "Muş", "Nevşehir", "Niğde", "Ordu", "Rize", "Sakarya",
"Samsun", "Siirt", "Sinop", "Sivas", "Tekirdağ", "Tokat", "Trabzon", "Tunceli", "Şanlıurfa", "Uşak",
"Van", "Yozgat", "Zonguldak", "Aksaray", "Bayburt", "Karaman", "Kırıkkale", "Batman", "Şırnak",
"Bartın", "Ardahan", "Iğdır", "Yalova", "Karabük", "Kilis", "Osmaniye", "Düzce"};
private ArrayAdapter<String> arrayAdapter;
public void setSendData(SendData sendData){ //create setter for interface
this.sendData = sendData;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView= (SearchView) findViewById(R.id.searchView);
listView= (ListView) findViewById(R.id.listView);
FragmentA fragmentA = new FragmentA() ;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.frameLayout,fragmentA,"fragA");
transaction.commit();
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,il);
listView.setAdapter(arrayAdapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
arrayAdapter.getFilter().filter(newText);
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long id) {
text = (String) listView.getItemAtPosition(position);
sendData.gonder(text);
}
});
}
片段应该是这样的。
public class FragmentA extends Fragment implements SendData {
private TextView textView, textView2,textView3,textView4;
View view ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a,container,false) ;
textView4= (TextView) view. findViewById(R.id.textIsım);
textView3= (TextView) view.findViewById(R.id.textView3);
textView2= (TextView) view.findViewById(R.id.textView2);
textView= (TextView) view. findViewById(R.id.textView);
if(getActivity() instanceOf MainActivity)
(MainActivity)getActivity().setSendData(this); //set your interface in MainActivity
return view;
}
@Override
public void gonder(String isim) {
Factory.getInstance().havaModel(isim,APPID_KEY).enqueue(new Callback<HavaModel>() {
@Override
public void onResponse(Call<HavaModel> call, Response<HavaModel> response) {
textView.setText(Float.toString((float) (response.body().main.temp-273.15)));
textView2.setText(Float.toString(response.body().coord.lon));
textView3.setText(Integer.toString(response.body().main.humidity)+" %");
textView4.setText(response.body().name);
}
@Override
public void onFailure(Call<HavaModel> call, Throwable t) {
}
});
}
希望有所帮助:)