【问题标题】:remove a value from Hashmap by onitemclicklistner in listview通过列表视图中的 onitemclicklistner 从 Hashmap 中删除一个值
【发布时间】:2016-04-14 07:17:35
【问题描述】:

我正在尝试从我们使用 ArrayList> DataListResult 的列表视图中删除值 id 和 name;和用户 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();
    }
}

我在哪里做错了请任何人告诉我..谢谢

【问题讨论】:

    标签: java android listview hashmap


    【解决方案1】:

    改变

    DataList.remove(item);
    

    DataListResult.revome(position);
    

    【讨论】:

    • 谢谢先生,它正在工作:) 我可以使用 DataList.remove(position);动态删除值?
    • DataList 是 hashmap,您将它存储在列表 DataListResult 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2017-08-12
    • 2016-08-28
    • 2023-04-08
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多