[英]How to remove an item from an ListView programmatically in Android?
我創建了一個ListView,其中顯示了數據庫中的一些記錄。 當某人單擊ListView中的項目時,我需要將該項目從ListView中完全刪除。 如果我使用view.setVisibility(View.GONE);
或view.setVisibility(View.INVISIBLE);
僅文本消失。 我需要刪除整個項目。 我正在使用ListAdapter接口而不是SimpleAdapter類。
我怎樣才能做到這一點? 這是我的代碼:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, showProductsUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
final String productId = jsonObject.getString("id");
final String productName = jsonObject.getString("productName");
HashMap<String, String> product = new HashMap<>();
product.put(ID, productId);
product.put(PRODUCT_NAME, productName);
productList.add(product);
String[] from = {ID, PRODUCT_NAME};
int[] to = {R.id.id, R.id.productName};
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), productList, R.layout.list_products_to_buy, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setVisibility(View.GONE);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
提前致謝!
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
productList.remove(position);
listView.getAdapter().notifyDataSetChanged();
}
});
好吧,您只需使用ArrayAdapter的remove()方法從列表中刪除所需的項目。
一種可能的方法是:
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, showProductsUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
final String productId = jsonObject.getString("id");
final String productName = jsonObject.getString("productName");
HashMap<String, String> product = new HashMap<>();
product.put(ID, productId);
product.put(PRODUCT_NAME, productName);
productList.add(product);
String[] from = {ID, PRODUCT_NAME};
int[] to = {R.id.id, R.id.productName};
//NOTE: See I changed it to SimpleAdapter so you can call the notifydatasetchanged method after removing the element from the list.
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), productList, R.layout.list_products_to_buy, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
productList.remove(position);
adapter.notifyDataSetChanged();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.