我有一个自定义列表视图,每行都有删除按钮。列表视图中的项目显示在数据库中。单击删除按钮,数据库中的项目被删除。但它没有反映在列表视图中。我需要删除相应的该项目的一行。
CartAdapter.java
public class CartAdapter extends BaseAdapter
{
private final String[] pname;
private final String[] price;
private Context cntxt;
public CartAdapter(Context c,String [] pname,String [] price)
{
cntxt=c;
this.pname=pname;
this.price=price;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pname.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View List;
LayoutInflater mLayoutInflater=(LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView==null) {
List=new View(cntxt);
List=mLayoutInflater.inflate(R.layout.add2crt_sub,parent, false);
}
else {
List=(View)convertView;
}
Button button=(Button)List.findViewById(R.id.remove);
button.setTag(position);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (Integer)v.getTag();
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
String pnam = pname[position];
mydb.execSQL("DELETE FROM add2cart WHERE pnme ='"+pnam+"'");
Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null);
String [] pname = new String[cr.getCount()];
String [] price = new String[cr.getCount()];
int i = 0;
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pnme"));
String prprice = cr.getString(cr.getColumnIndex("prate"));
pname[i] = name;
price[i] = prprice;
i++;
}
CartAdapter cart=CartAdapter.this;
add2cart.adlstvw.setAdapter(cart);
notifyDataSetChanged();
}
});
TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme);
final TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate);
final TextView totltxt=(TextView)List.findViewById(R.id.prdt_totl);
final EditText edittext=(EditText)List.findViewById(R.id.prdt_qnty);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String s1=pricetxt.getText().toString();
String s2=edittext.getText().toString();
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int res=i1*i2;
totltxt.setText(Integer.toString(res));
if (s2.matches("")) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
nametxt.setText(pname[position].toString());
pricetxt.setText(price[position]);
return List;
}
}
答案 0 :(得分:2)
首先,您需要引用Context类来打开数据库
SQLiteDatabase mydb=CartAdapter.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
以上代码错误
你必须将一个上下文传递给我在上面一行中看不到的数据库类...如果我错了,请纠正我
android.database.sqlite.SQLiteException: near "(": syntax error: DELETE FROM add2cart WHERE pname=button.setTag(position
从上面的行中安静地清除以下代码中存在语法错误
mMydb.execSQL("DELETE FROM add2cart WHERE pname=button.setTag(position)");
我猜正确的代码可能是
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (Integer)v.getTag();
//this will give you the position of the data been clicked
// how extract the data item from the list at the position
// for eg that data is in pname vriable
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
String pnam = pname[position];
mydb.execSQL("DELETE FROM add2cart WHERE pnme = '"+pnam+"'");
notifyDataSetChanged();
}
});
你也必须删除列表中的项目String [] pname以反映listview中的更改你最好使用List pname,因为删除在列表数据结构中更好
答案 1 :(得分:0)
尝试将CartAdapter.this替换为您在适配器类的构造函数中传递的活动上下文。
替换
SQLiteDatabase mydb=CartAdapter.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
带
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
答案 2 :(得分:0)
OnClickListener的整个设计是错误的。每次用户点击按钮时打开数据库都没有任何意义。
您应该将以下行粘贴到适配器的构造函数:
mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
将mydb保留为局部变量,您可以将其称为mMydb以获得正确的代码约定。
然后,在onClick()中,只需保留以下行:
mMydb.execSQL("DELETE FROM add2cart WHERE pname=button.setTag(position)");
notifyDataSetChanged();
此外,正如@dragon所建议的那样 - 你应该使用封装的活动上下文,所以只需将它添加到适配器的上下文中并使用它。