使用android中列表视图外的删除按钮删除listview中的项目

时间:2014-03-09 09:36:12

标签: android listview android-cursoradapter

我正在使用具有check boxbutton的自定义列表视图,并使用游标适配器填充列表视图。

现在,我想在单击按钮Delete时删除列表视图的项目。所以我先使用按钮点击监听器,然后在列表视图中捕获项目点击我为listview写了itemclicklistener然后尝试捕获......但是这里没有任何工作,因为当一个项目时控件不会转到ItemClicklistener在列表视图中单击。

代码:

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());
            }
        });

此处控制正在进行,但在我选择所有列表视图后,并且itemposition又返回为-1

1 个答案:

答案 0 :(得分:1)

正如您在Adapter类上有按钮并单击侦听器一样,问题是如果视图包含可聚焦或可单击的项目,则不会调用OnItemCLickListener。

设置行项目参数,例如 android:descendantFocusability =“blocksDescendants”,我认为你的代码是stocklist

有关详细信息,请参阅thisthis

//编辑

在讨论聊天之后,了解到你想要setClick的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);


                }
            });
        }