0

I have a class which has a Listview. The ListView is populated using a pattern.xml file which has a button in it. when the class is called the Button gets copied for each item in the listview. now what i want is to access those button to delete the corresponding item from the list. so how can i do that? Please help me to solve this. The code for that class is given bellow.

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

                }

            }
    }
}

1 Answer 1

1

You have to specify OnClickListener of Button and remove Item from arrayList in your BaseAdapter and call 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;
        } 
Sign up to request clarification or add additional context in comments.

6 Comments

I know that but i can not figure out where to put those code in. how can i get access to any particular button in the list to apply an on click action on it.
I have tried the above procedure already and i get an error in adapter.notifyDataSetChanged(); line and it says "The local variable adapter may not have been initialized" and i don't know how to do that.
just add final before BaseAdapter adapter = ... line
Actually your code is bit messy... But this can be quick fix for existing code.
i did that as well and the error that i mentioned above comes after putting the final keyword before BaseAdapter adapter = ... line.And sorry for being messy with code. can not help it. I guess it is understandable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.