gpt4 book ai didi

android - 在哪里放置 adapter.notifyDataSetChanged();使用 Volley 时

转载 作者:行者123 更新时间:2023-11-29 15:43:20 25 4
gpt4 key购买 nike

此代码运行良好,将我的 JSON 响应显示到 Arraylist 中。问题是,每次我点击按钮再次显示列表时,它只是简单地复制了之前的 JSON 响应,导致了重复结果的列表。

如何让它在我每次单击按钮时刷新数组列表,从而删除所有以前的结果?我需要放置另一个 notifyDataSetChanged 吗?如果有,在哪里?

Details.java代码:

public class Details extends AppCompatActivity {

private static final String TAG = Details.class.getSimpleName();
String url="removed";
private Button ShowDetailsButton;
private Button AddDetails;
private CustomListAdapter adapter;
private ProgressDialog pDialog;
private List<LoadUsers> mList = new ArrayList<LoadUsers>();
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.studio_student_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

listView = (ListView) findViewById(R.id.lv);
adapter = new CustomListAdapter(this, mList);
listView.setAdapter(adapter);

ShowDetailsButton = (Button) findViewById(R.id.show_details);
AddDetails = (Button) findViewById(R.id.add_details);

// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);

ShowDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// Creating volley request obj
JsonArrayRequest studentReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
LoadUsers details = new LoadUsers();
details.setTitle(obj.getString("name"));
details.setThumbnailUrl(obj.getString("image"));
details.setEmail(obj.getString("email"));
details.setPhone(obj.getString("phone"));

// adding
mList.add(details);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();

}

});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(studentReq);
}

});

}
public void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
}

CustomListAdapter.java代码:

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<LoadUsers> usersItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<LoadUsers> usersItems) {
this.activity = activity;
this.usersItems = usersItems;
}

@Override
public int getCount() {
return usersItems.size();
}

@Override
public Object getItem(int location) {
return usersItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

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

if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView email = (TextView) convertView.findViewById(R.id.lvemail);
TextView phone = (TextView) convertView.findViewById(R.id.lvphone);


// getting user data for the row
LoadUsers m = usersItems.get(position);

// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

// title
title.setText(m.getTitle());

// email
email.setText("Email: " + String.valueOf(m.getEmail()));

// phone
phone.setText("Phone: " + String.valueOf(m.getPhone()));

return convertView;

}

}

最佳答案

错误:

数据正在重复,因为您没有清除具有以前数据集的 ArrayList。

解决方案:在进行 API 调用之前清除 ArrayList。在您的情况下,这应该是按钮点击监听器代码的第一行!

// clear collection 
mList.clear();

关于android - 在哪里放置 adapter.notifyDataSetChanged();使用 Volley 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995606/

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