我正在尝试从listview中删除值id和name,其中我们使用ArrayList> DataListResult;和用户ListAdapter私有ListAdapter适配器= null;当我使用DataListResult.clear();它是工作,它清除所有列表项,但我想通过点击只删除一个项目。
ArrayList<HashMap<String, String>> DataListResult = new ArrayList<HashMap<String, String>>();
HashMap<String, String> DataList;
ListView listView;
private ArrayAdapter arrayAdapter = null;
private ListAdapter adapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_apointment__list, container, false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
adapterView.getItemAtPosition(position);
toRemove= listView.getItemAtPosition(position);
Object item = adapter.getItem(position);
Toast.makeText(getActivity().getApplication(), value, Toast.LENGTH_SHORT).show();
//DataListResult.clear();
DataList.remove(item);
((BaseAdapter)adapter).notifyDataSetChanged();
}
});
return v;
}
@Override
protected void onPostExecute(Integer result) {
if(result == 1){
// arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, DataListResult);
// listView.setAdapter(arrayAdapter);
adapter = new SimpleAdapter(
getActivity(),DataListResult,
R.layout.list_item, new String[] {
TAG_ID, TAG_NAME,
}
, new int[] {
R.id.list_id,R.id.list_name,
}
);
listView.setAdapter(adapter);
private void parseResult(String result) {
try{
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("appontment");
for(int i=0; i< posts.length();i++ ){
JSONObject post = posts.optJSONObject(i);
String id = post.optString("id");
String name = post.optString("name");
DataList = new HashMap<String, String>();
DataList.put(TAG_ID, id);
DataList.put(TAG_NAME, name);
// DataList.remove(toRemove);
DataListResult.add(DataList);
}
}catch (JSONException e){
e.printStackTrace();
}
}
我在做错了,请有人告诉我..谢谢
答案 0 :(得分:2)
更改
DataList.remove(item);
到
DataListResult.revome(position);