Android:使用复选框从自定义列表中获取选中的项目名称

时间:2012-10-22 08:20:46

标签: android android-listview android-checkbox

我有列表视图。为每行我自定义为文本视图和复选框。现在我想获取已检查项目的名称。我试图使用checkedItemPositons但是这不起作用是因为2布局??

我想在按下删除按钮时删除已检查的主题。如何执行此操作? 这是我的代码

      public class ManageLikes extends Activity implements OnClickListener {

ListView lv;
Button btndelsel, btndelall;
CheckBox chk;
HashMap<Integer, Boolean> mCartItems = new HashMap<Integer, Boolean>();

List<String> likes = new ArrayList<String>();
//DatabaseHandler db = new DatabaseHandler(getApplicationContext());


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.managelikes);

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    //String data[] = { "a", "b", "c", "d", "e"};
    likes = db.getAllLikes();

    lv = (ListView) findViewById(R.id.listView);
    btndelsel = (Button) findViewById(R.id.btndelsel);
    btndelall = (Button) findViewById(R.id.btndelall);
    btndelsel.setOnClickListener(this);
    btndelall.setOnClickListener(this);

    lv.setCacheColorHint(0);

    LayoutInflater mLInflater = getLayoutInflater();
    final LAdapter adapter = new LAdapter(getApplicationContext(),likes, mLInflater);
    lv.setAdapter(adapter);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    int count = likes.size();
    String checked = "";

    if (v == btndelsel) {
        //i want delete the checked items here
    }


    if (v == btndelall) {
        //delete all items

    }
    }

     }

1 个答案:

答案 0 :(得分:0)

尝试以下方法。

  1. 内部适配器具有复选框监听器,并在复选框单击监听器时跟踪某种数据结构中的选中项目。
  2. 点击删除按钮后,使用数据结构跟踪检查项目,从数据源(即数据库)中删除相应的数据。
  3. 删除后通过调用notifyDataSetChanged()刷新UI来更新适配器。
相关问题