0

当用户长按列表并刷新列表时,我想从列表中删除项目。

当我使用此代码时,它会删除该项目但不刷新列表:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return true;
    }
});

现在我在stackoverflow上找到了这个Android how to delete items from list with longclicklistener,但我的代码不起作用:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    @Override
    public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();               
    }
});

自定义适配器:

private class CustomAdapterCode extends ArrayAdapter<HashMap<String, Object>>{
    public CustomAdapterCode(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
        super(context, textViewResourceId, Strings);
    }
    private class ViewHolder{
        TextView code_id, code_layout, name_layout, prio1, prio2, prio3;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){        
            convertView = inflater.inflate(R.layout.code_list_item, parent, false);
            viewHolder=new ViewHolder();

            viewHolder.code_id=(TextView) convertView.findViewById(R.id.code_id);
            viewHolder.code_layout=(TextView) convertView.findViewById(R.id.code_layout);
            viewHolder.name_layout=(TextView) convertView.findViewById(R.id.name_layout);
            viewHolder.prio1=(TextView) convertView.findViewById(R.id.prio1);
            viewHolder.prio2=(TextView) convertView.findViewById(R.id.prio2);
            viewHolder.prio3=(TextView) convertView.findViewById(R.id.prio3);

            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.code_id.setText(codeList.get(position).get("_id").toString());
        viewHolder.code_layout.setText(codeList.get(position).get("code").toString());
        viewHolder.name_layout.setText(codeList.get(position).get("name").toString());
        viewHolder.prio1.setText(codeList.get(position).get("prio1").toString());
        viewHolder.prio2.setText(codeList.get(position).get("prio2").toString());
        viewHolder.prio3.setText(codeList.get(position).get("prio3").toString());


        return convertView;
    }
}   

日食 说:

此行有多个标记

  • 实现 android.widget.AdapterView.OnItemLongClickListener.onItemLongClick
  • 返回类型与 AdapterView.OnItemLongClickListener.onItemLongClick(AdapterView, View, int, long) 不兼容
4

1 回答 1

0

我发现了问题,我忘记从用于构建 ListView 的 ArrayList 中删除该项目:

codeList.remove(position);
于 2013-11-02T16:30:59.433 回答