在第一个片段中,有一些文本视图。例如,a,b和c。
当我点击a时,它会转到另一个片段并显示一个for apple。 但是,当我回到第一个片段并单击b时,它仍会显示一个苹果。我需要回到第一个片段并再次单击b然后它将显示正确的单词。
public class location extends Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("gateway");
LinearLayout linearLayout=(LinearLayout)getActivity().findViewById(R.id.locationlayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
params.setMargins(20,10,10,10);
Intent intent=getActivity().getIntent();
String ble_num=intent.getStringExtra("num");
int number=Integer.parseInt(ble_num);
final String ble_id[]=new String[number];
final String alias[]=new String[number];
final String serial_number[]=new String[number];
final TextView[] textView= new TextView[number];
final SharedPreferences sharedPreferences= getActivity().getSharedPreferences("test", MODE_PRIVATE);
for(int i=0; i<number; i++){
ble_id[i]=sharedPreferences.getString("ble_id"+i,"123");
alias[i]=sharedPreferences.getString("alias"+i,"123");
serial_number[i]=sharedPreferences.getString("serial_number"+i,"123");
}
for (int j=0; j<number; j++){
textView[j]= new TextView(getActivity());
textView[j].setText("location: "+alias[j]+"\n"+"id: "+ble_id[j]+", serial number: "+serial_number[j]);
textView[j].setId(j);
textView[j].setLayoutParams(params);
linearLayout.addView(textView[j]);
final int finalJ = j;
textView[j].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Response.Listener<String> ResponseListener= new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonRespon = new JSONObject(response);
JSONArray array=jsonRespon.getJSONArray("result");
String num=jsonRespon.getString("num");
int number=Integer.parseInt(num);
sharedPreferences.edit().putString("slave_num",num).commit();
String slave_id[]=new String[number];
String slave_alias[]=new String[number];
for(int i=0;i<number;i++){
JSONObject item= array.getJSONObject(i);
slave_id[i]=item.getString("slave_id");
slave_alias[i]=item.getString("slave_alias");
sharedPreferences.edit().putString("slave_id"+i,slave_id[i]).putString("slave_alias"+i,slave_alias[i])
.commit();
}
}catch(JSONException e){
e.printStackTrace();
}
}
};
slave slave = new slave(ble_id[finalJ], ResponseListener);
RequestQueue queue= Volley.newRequestQueue(getActivity());
queue.add(slave);
Fragment fragment = new slavepage();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_drawer, fragment);
fragmentTransaction.commit();
}
});
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.location, container, false);
}
}
这是第一个片段。
public class slavepage extends Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("slave");
LinearLayout linearLayout = (LinearLayout) getActivity().findViewById(R.id.slavelayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
params.setMargins(20, 10, 10, 10);
final SharedPreferences sharedPreferences = getActivity().getSharedPreferences("test", MODE_PRIVATE);
String slave_num = sharedPreferences.getString("slave_num", "");
int number = Integer.parseInt(slave_num);
String slave_id[]=new String[number];
String slave_alias[]=new String[number];
TextView[] textViews=new TextView[number];
for (int i = 0; i < number; i++) {
slave_id[i]=sharedPreferences.getString("slave_id"+i,"123");
slave_alias[i]=sharedPreferences.getString("slave_alias"+i,"123");
}
for (int j=0;j<number;j++){
textViews[j]= new TextView(getActivity());
textViews[j].setText("alias: "+slave_alias[j]+"\n"+"id: "+slave_id[j]);
textViews[j].setId(j);
textViews[j].setLayoutParams(params);
linearLayout.addView(textViews[j]);
}
}
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, Bundle
savedInstanceState){
return inflater.inflate(R.layout.slave, container, false);
}
}
这是第二个片段。
问题出在哪里?
答案 0 :(得分:0)
Fragment fragment = new slavepage();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_drawer, fragment);
fragmentTransaction.commit();
您的上述代码会在
之后立即运行queue.add(slave);
不在网络阻止功能内