android - 在android中删除列表项表单 ListView

标签 android list

我阅读了更多与此问题相关的内容,但没有得到更多的想法。之后,我尝试发布,在这张图片中,我的列表中有 3 个项目,我有 2 个项目点击。所以我想删除这两个勾选项。但我是 Android 的新手,所以无法了解这背后的更多想法。

Code

 public class CountryList extends Activity implements OnClickListener,
    OnItemClickListener {
private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return tempCountry.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.bookmarks_list_item,
                    null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView
                    .findViewById(R.id.country);

            holder.checkBox = (CheckedTextView) convertView
                    .findViewById(android.R.id.checkbox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtcnt.setText(country[position]);
        return convertView;
    }
    static class ViewHolder {
        TextView txtcnt;
        CheckBox checkBox;
    }}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.bokmarksjoks);

    try {
        db = (new DatabaseHelper(this)).getWritableDatabase();
    } catch (IOException e) {
        e.printStackTrace();
    }
    lv = (ListView) findViewById(R.id.list);
            btn_delete = (Button) findViewById(R.id.delete);
    btn_delete.setOnClickListener(this);
    checkbox = (CheckBox) findViewById(R.id.checkbox);
    txtname= (TextView) findViewById(R.id.body);

    String name= pref.getString("name", "");
    country= name.split(",");

    lv.setAdapter(new EfficientAdapter(this));

    lv.setItemsCanFocus(false);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    lv.setOnItemClickListener(this);
        }

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.delete:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder .setMessage("Are you Sure want to delete checked country ?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // how to remove country

                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        alert.setTitle("Delete Country");
        alert.show();
    case R.id.checkbox:
     //What is the procedue in this section
    default:
        break;
    }
}
public void onItemClick(AdapterView<?> pareant, View view, int position,
        long id) {

    try {
        // I have trying this but could not proper output or only errors
                 SparseBooleanArray sp = lv.getCheckedItemPositions();
        /*String str = "";
        for (int i = 0; i < sp.size(); i++) {
            str += country[sp.keyAt(i)] + ",";

        }*/

    } catch (Exception e) {
        e.printStackTrace();
}}}

这是唯一的三个国家,实际上,我有超过数百个国家。

最佳答案

tempCountry 中删除项目,然后调用 adapter.notifyDataSetChanged()

关于android - 在android中删除列表项表单 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9052019/

相关文章:

android - 带过滤器的 LiveData?

python - 仅获取特定索引处具有最低值的子列表

python - 如何将具有定义的共享子字符串的列表中的字符串移动到新列表?

python - 使用列表(的元素)作为字典中的键的 Pythonic 方法是什么?

java - 比较字符串列表的元素

android - 为什么最新版本的 Stripe gradle 不允许 .setName?

android - 房间持久性库 : Cannot resolve symbol Room

android - Android 中的水平可扩展 ListView

使用嵌套列表索引时 Pythonic 交换导致索引错误

Android 应用程序如何测试开发版与实时版