簡體   English   中英

如何使用在項目內創建的按鈕刪除ListView項目。

[英]how to delete a ListView item using a button created inside that item.

我有一個具有Listview的類。 使用其中具有按鈕的pattern.xml文件填充ListView。 當調用該類時,將為列表視圖中的每個項目復制Button。 現在我想要的是訪問那些按鈕以從列表中刪除相應的項目。 那我該怎么辦呢? 請幫我解決這個問題。 該類的代碼在下面給出。

public class Secondscreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen); 
    ListView lv= (ListView) findViewById(R.id.listView1);


    final Button thirdBtn = (Button) findViewById(R.id.third);


    final Controller aController = (Controller) getApplicationContext();

        final int cartSize = aController.getCart().getCartSize();

        final ArrayList<Listitem> arrayList=new ArrayList<Listitem>();


         BaseAdapter adapter= new BaseAdapter(){

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return arrayList.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return arrayList.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            @Override
            public View getView(final int position, View view, ViewGroup viewgroup) {
                if (view == null) {
                    view=inflater.inflate(R.layout.pattern, null);
                }
                TextView tv=(TextView) view.findViewById(R.id.nameview);
                TextView tv2=(TextView) view.findViewById(R.id.pdesc);
                TextView tv3=(TextView) view.findViewById(R.id.priceView);



                tv.setText(arrayList.get(position).getName());
                tv2.setText(""+arrayList.get(position).getPrice());
                tv3.setText(arrayList.get(position).getDesc());
                return view;
            }       

        };
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);


        if(cartSize >0)
            {   

                for(int i=0;i<cartSize;i++)
                {

                   String pName   = aController.getCart().getProducts(i).getProductName();
                   int pPrice      = aController.getCart().getProducts(i).getProductPrice();
                   String pDisc       = aController.getCart().getProducts(i).getProductDesc();

                    Listitem item=new Listitem(pName, pPrice, pDisc);
                    arrayList.add(item);
                    adapter.notifyDataSetChanged();

                }

            }
    }
}

你必須指定OnClickListenerButton和刪除項目arrayList在你BaseAdapter並調用notifyDataSetChanged();

        @Override
        public View getView(final int position, View view, ViewGroup viewgroup) {
            if (view == null) {
                view=inflater.inflate(R.layout.pattern, null);
            }
            Button button = (Button) view.findViewById(R.id.button);
            //button.setTag(position);
            button.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view) {
                    //Integer position = (Integer)view.getTag();
                    arrayList.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
            // Your other views...
            return view;
        } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM