Android:通过复选框从自定义列表中获取选中项目名称

Android:通过复选框从自定义列表中获取选中项目名称,android,android-listview,android-checkbox,Android,Android Listview,Android Checkbox,我有列表视图。对于每一行,我都自定义为文本视图和复选框。现在我想获取选中项目的名称。我尝试使用CheckEditePositions,但不起作用,因为有两个布局 当按下“删除”按钮时,我想删除选中的itmes。我该怎么做? 这是我的密码 public class ManageLikes extends Activity implements OnClickListener { ListView lv; Button btndelsel, btndelall; CheckBox ch

我有列表视图。对于每一行,我都自定义为文本视图和复选框。现在我想获取选中项目的名称。我尝试使用CheckEditePositions,但不起作用,因为有两个布局

当按下“删除”按钮时,我想删除选中的itmes。我该怎么做? 这是我的密码

      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

    }
    }

     }
公共类ManageLikes扩展活动实现OnClickListener{
ListView lv;
按钮btndelsel,btndelall;
chk;
HashMap mCartItems=新的HashMap();
List likes=new ArrayList();
//DatabaseHandler db=新的DatabaseHandler(getApplicationContext());
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.managelikes);
DatabaseHandler db=新的DatabaseHandler(getApplicationContext());
//字符串数据[]={“a”、“b”、“c”、“d”、“e”};
likes=db.getAllLikes();
lv=(ListView)findViewById(R.id.ListView);
btndelsel=(按钮)findViewById(R.id.btndelsel);
btndelall=(按钮)findViewById(R.id.btndelall);
btndelsel.setOnClickListener(此);
btndelall.setOnClickListener(此);
lv.setCacheColorHint(0);
LayoutInflater mLInflater=getLayoutInflater();
最终LAdapter适配器=新的LAdapter(getApplicationContext(),likes,mLInflater);
低压设置适配器(适配器);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
int count=likes.size();
勾选字符串=”;
如果(v==btndelsel){
//我想在这里删除选中的项目
}
如果(v==btndelall){
//删除所有项目
}
}
}

尝试以下方法

  • 在适配器内部有一个用于复选框的侦听器,并在复选框单击侦听器时跟踪某种数据结构中的选中项
  • 单击“删除”按钮后,通过使用数据结构跟踪选中的项目,从数据源(即数据库)中删除相应的数据
  • 删除后,通过调用notifyDataSetChanged()刷新UI来更新适配器
  • 根据常见问题解答,请勿在帖子中添加签名/标语(即“干杯”)。另外,不要在你所有的帖子中都添加到你的网站的链接,同样,根据常见问题解答:-继续这样做会导致版主采取进一步的行动。