[英]Delete checked item from list view and data from database, using adapter class
public class delete extends BaseAdapter {
SQLiteDatabase db;
String am;
private final Context c;
private final ArrayList<String> str;
private final String[] number;
ArrayList<Integer> checks=new ArrayList<Integer>();
public delete(Context c, ArrayList<String> str, String[] number, ArrayList<Integer> checks) {
this.c = c;
this.str = str;
this.number = number;
this.checks=checks;
}
@Override
public int getCount() {
return str.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View ListView ;
if(convertView==null)
{
ListView=new View(c);
ListView=inflater.inflate(R.layout.button,null);
TextView textview=(TextView) ListView.findViewById(R.id.textView6);
textview.setText(str.get(position));
}
else
{
ListView=(View)convertView;
}
Button btnupdate=(Button)ListView.findViewById(R.id.button4);
//checkbox declaration
CheckBox chk=(CheckBox)ListView.findViewById(R.id.chk1);
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db=c.openOrCreateDatabase("Student",Context.MODE_PRIVATE, null);
String num = number[position];
Cursor cc=db.rawQuery("select mobile from detail where mobile='"+num+"'", null);
if (cc.moveToFirst()) {
do {
am = cc.getString(cc.getColumnIndex("mobile"));
} while (cc.moveToNext());
}
Intent i=new Intent(c,Update.class);
i.putExtra("value",am);
c.startActivity(i);
}
});
return ListView;
}
}
//它在列表视图中显示所有数据,并带有更新按钮和列表视图中的复选框。 有一个删除按钮,用于删除所有选中的项目。 //“删除”按钮位于添加列表视图的主活动上。
您可以在DataBase类中创建一个方法,并传递要删除的表名和条目以及ID(如果有)。在ListView项目的onClick上调用该方法。
public void deleteEntry(String tableName,String columnName,String id)
{
openDatabase();
database.delete(tableName, columnName + " = ?",new String[] { id });
closeDatabase();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.