1

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Siva
  • 9,043
  • 12
  • 40
  • 63
  • get all element in to text view from array listview and then set the values of in side of button click event textview.settext("") ; that solve i think......do this it may help u – Amitsharma Mar 09 '14 at 09:46
  • Thanks amit but if I get all values of listview to textview then if I delete any item of listview how that effects already populated listview? – Siva Mar 09 '14 at 09:53
  • u get list all items (whose are in textview from another layout u have need to call )in to list ..........then u can clear these items values set(""); ...... – Amitsharma Mar 10 '14 at 15:59

1 Answers1

1

as you Have button and click listener on your Adapter class, The problem is that If a view contains either focusable or clickable item the OnItemCLickListener won't be called.

set for row item param like android:descendantFocusability="blocksDescendants", i think it is stocklist on your code.

for more info please see this and this

// Edit

after discus on Chat understand that you want setClick just for you checkBox, so you need following code:

 @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);


                }
            });
        }
Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63