gpt4 book ai didi

java - 单击 fragment 中的后退按钮后 ListView 正在更新

转载 作者:行者123 更新时间:2023-12-02 10:55:15 25 4
gpt4 key购买 nike

这里我尝试从 URL 获取数据并显示在 fragment A 的 Listview 中。它工作得很好。但是,单击 ListView 的项目后,它会导航到下一个 fragment B。当我使用后退按钮从 fragment B 导航到 fragment A 时, ListView 项目会再次更新。如果 ListView 中有 3 个项目,并且用户从 B 导航到 A 2 次,则总数为 1。 Listview 中的项目数将为 9。请帮助停止这种冗余

public class EnglishFragment extends Fragment {


private List<DataItem> dataFeed= new ArrayList<DataItem>();
ListView listView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

final ArrayAdapter<DataItem> adapter = new EnglishFragment.Adapter();

View view;

view = inflater.inflate(R.layout.fragment_topic2,container,false);

RequestQueue queue = Volley.newRequestQueue(getActivity());
// JsonObjectRequest myReq= new JsonObjectRequest(Request.Method.GET,

listView = view.findViewById(R.id.listview);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, **URL**, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray dataArray = response.getJSONArray("topic");
Log.i("Temp",""+dataArray.length());
for(int i=0;i<dataArray.length();i++) {
JSONObject temp = dataArray.getJSONObject(i);
Log.i("Temp",temp.toString());


String first_name = temp.getString("name");
String last_name = temp.getString("details");
String image_path =temp.getString("img_path");;
String chapter_path = temp.getString("url");
Log.i("Image",temp.getString("url"));

dataFeed.add(new DataItem(first_name,last_name," "," ", image_path,chapter_path));
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
jsonObjectRequest.setRetryPolicy( new DefaultRetryPolicy(10000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

queue.add(jsonObjectRequest);

listView.setAdapter( adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

DataItem currentItem = dataFeed.get(position);

Log.v("position", currentItem.getImageURL());
Toast.makeText(getActivity(), currentItem.getChapterURL(), Toast.LENGTH_SHORT).show();
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.getChapterList(currentItem.getChapterURL());

}

});


return view;
}
private class Adapter extends ArrayAdapter<DataItem> {

public Adapter() {
super(getActivity(), R.layout.data_items,dataFeed);
}


@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

if (convertView == null)
{
convertView = getLayoutInflater().inflate(R.layout.data_items,null,false);

}

DataItem currentItem = dataFeed.get(position);
TextView heading = (TextView)convertView.findViewById(R.id.subject_name);
TextView desc = (TextView)convertView.findViewById(R.id.subject_details);
ImageView imageView = (ImageView)convertView.findViewById(R.id.leftIcon);


heading.setText(currentItem.getNewsHeading() + " ");
desc.setText(Html.fromHtml(currentItem.getNewsDesc()));
imageView.setImageResource(currentItem.getImageId());
Picasso.get().load(currentItem.getImageURL()).into(imageView);

return convertView;
}
}

MainActivity.getChapterURL()代码

public void getChapterList(String ChapterURL ){

FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ChapterListFragment chapterListFragment = new ChapterListFragment();

Bundle bundle = new Bundle();
bundle.putString("url",ChapterURL);
chapterListFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame_container,chapterListFragment).addToBackStack(null)
.commit();



}

最佳答案

添加新数据之前先清除 dataFeed 列表... dataFeed.clear()

关于java - 单击 fragment 中的后退按钮后 ListView 正在更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51817144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com