【发布时间】:2023-12-25 18:36:01
【问题描述】:
此代码运行良好,将我的 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;
}
}
【问题讨论】:
-
添加
mList.clear();正上方评论行// Creating volley request obj或正上方// Adding request to request queue:) -
谢谢,成功了。
标签: android json arraylist adapter android-volley