I am using a custom list view that has a check box
and button
and populating the list view using cursor adapter.
Now I want to delete the item of the list view when I click a button Delete
. So I have used button click listener first and then to capture the item click in listview I wrote itemclicklistener for listview and then tried to capture, but nothing is working here as the control is not going to ItemClicklistener
when a item is clicked in listview.
Code:
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;
}
}
}
How can I resolve this issue? I am happy to receive any guidance if I am going in the wrong direction. I am a beginner to Android.
Edit
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());
}
});
Here control is going but after I select all list viewitems and also itemposition is returning as -1