gpt4 book ai didi

android - 使用 Android 中 ListView 外的删除按钮删除 ListView 中的项目

转载 作者:行者123 更新时间:2023-11-30 03:02:58 24 4
gpt4 key购买 nike

我正在使用具有复选框按钮 的自定义 ListView ,并使用游标适配器填充 ListView 。

现在我想在单击按钮 Delete 时删除 ListView 的项目。所以我首先使用了按钮点击监听器,然后在 ListView 中捕获项目点击我为 ListView 编写了 itemclicklistener 然后 try catch ,但是这里没有任何工作因为控件不会 ItemClicklistener 当一个在 ListView 中单击项目。

代码:

public class manager extends Activity{

String getentry;
private int storeID=0;
//Database d;
StockTable st;
private String getstocks;
public Cursor a1;
Intent bd;
Intent sd;
//ListView popstocks;

/*public stockmanager() {
// TODO Auto-generated constructor stub

}*/

static class ViewHolder {

CheckBox cb;
Button view1;
}


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
// d=new Database(getApplicationContext());
st=new StockTable(getApplicationContext());
final Button AddStock=(Button) findViewById(R.id.button1);
final Button DeleteStock=(Button) findViewById(R.id.button3);
final EditText entry=(EditText) findViewById(R.id.editText1);
final Button BroDetail=(Button) findViewById(R.id.button2);
//final ListView popstocks=(ListView) findViewById(R.id.listView1);
final ListView popstocks =(ListView) findViewById(R.id.listView1);
final TextView displaystocks=(TextView) findViewById(R.id.textView2);

********button that is used for deleting the item in listview**************************
DeleteStock.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popstocks.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), popstocks.getCheckedItemCount(), Toast.LENGTH_LONG).show();
System.out.println(popstocks.getCheckedItemCount());
}


});
System.out.println(popstocks.isItemChecked(popstocks.getCheckedItemPosition()));
}
});

}
public class poplist extends CursorAdapter{

public poplist(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
//StockTable st1=new StockTable(getApplicationContext());
//Database d1=new Database(getApplicationContext());


@Override
public void bindView(View view, Context context, Cursor c) {
// TODO Auto-generated method stub
final ViewHolder myviewholder=new ViewHolder();
myviewholder.cb=(CheckBox) view.findViewById(R.id.checkBox1);
myviewholder.view1=(Button) view.findViewById(R.id.button1);

//if(c.moveToFirst()){

//cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
//do{
//cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
myviewholder.cb.setText(c.getString(c.getColumnIndexOrThrow(st.column3)));
// }while (a1.moveToNext());

// }

myviewholder.view1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sd=new Intent(v.getContext(),StockDetail.class);
sd.putExtra("StockName", myviewholder.cb.getText());
startActivity(sd);


}
});
}

@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);

View v = inflater.inflate(R.layout.stocklist, parent, false);
bindView(v, context, c);
return v;
// return null;
}


}


}

我该如何解决这个问题?如果我走错了方向,我很乐意收到任何指导。我是 Android 的初学者。

编辑

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
// d=new Database(getApplicationContext());
st=new StockTable(getApplicationContext());
final Button AddStock=(Button) findViewById(R.id.button1);
final Button DeleteStock=(Button) findViewById(R.id.button3);
final EditText entry=(EditText) findViewById(R.id.editText1);
final Button BroDetail=(Button) findViewById(R.id.button2);
//final ListView popstocks=(ListView) findViewById(R.id.listView1);
final ListView popstocks =(ListView) findViewById(R.id.listView1);
final TextView displaystocks=(TextView) findViewById(R.id.textView2);
displaystocks.setText("You have not added any stocks start by entering stock name and clicking ADD Stock button");
getstocks="Select " + st.column1 + " as _id, " + st.column3 + " From "+ st.tablename;
a1=Database.getInstance(getApplicationContext()).getWritableDatabase().rawQuery(getstocks, null);
if(a1.moveToNext())
{
displaystocks.setVisibility(View.INVISIBLE);
}
poplist populatestocks=new poplist(getApplicationContext(),a1) ;
popstocks.setAdapter(populatestocks);

popstocks.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

System.out.println("Item Clicked");
System.out.println(popstocks.getCheckedItemPosition());
}
});

此处控制正在进行,但在我选择所有 ListView 项之后,itemposition 返回为 -1

最佳答案

因为您在 Adapter 类上有按钮和点击监听器,问题是如果 View 包含可聚焦或可点击的项目,则不会调用 OnItemCLickListener。

设置行项目参数,例如 android:descendantFocusability="blocksDescendants",我认为它是您代码中的 stocklist

更多信息请参阅thisthis

//编辑

在讨论 Chat 后了解到您只想为您的复选框设置 setClick,因此您需要以下代码:

 @Override
public void bindView(View view, Context context, Cursor c) {
// TODO Auto-generated method stub
final ViewHolder myviewholder=new ViewHolder();
myviewholder.cb=(CheckBox) view.findViewById(R.id.checkBox1);
myviewholder.view1=(Button) view.findViewById(R.id.button1);

//if(c.moveToFirst()){

//cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
//do{
//cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
myviewholder.cb.setText(c.getString(c.getColumnIndexOrThrow(st.column3)));
// }while (a1.moveToNext());

// }

myviewholder.cb.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// CheckBox Clicked


}
});

myviewholder.view1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sd=new Intent(v.getContext(),StockDetail.class);
sd.putExtra("StockName", myviewholder.cb.getText());
startActivity(sd);


}
});
}

关于android - 使用 Android 中 ListView 外的删除按钮删除 ListView 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22280618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com