5

In app I have a Listactivity which has an adapter with TextView and Button(labeled delete). Now I want to remove corresponding Button clicked item. please check the code and suggest???? `

public class MySimpleArrayAdapter extends ArrayAdapter<String> implements OnClickListener {
    private final Activity context;
    private final String[] names;
    private Button deleteButton= null;
    public MySimpleArrayAdapter(Activity context, String[] names) {
        super (context, R.layout.imagelistlayout, names);
        this.context = context;
        this.names = names;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
        deleteButton.setOnClickListener(this); 
        return rowView;

    }

    @Override
    public void onClick(View convertView) {
        System.out.println(deleteButton.getTag());

    }

}` 

I want to know how can I delete the item whose button has been clicked.

mthpvg
  • 3,789
  • 3
  • 26
  • 34
Sandroid
  • 328
  • 1
  • 10
  • 19

4 Answers4

9

just handle on click listener inside getview where you find the button using findviewbyid

this will handle the current row button click

public class MySimpleArrayAdapter extends ArrayAdapter<String> implements OnClickListener {
    private final Activity context;
    private final String[] names;
    private Button deleteButton= null;
    public MySimpleArrayAdapter(Activity context, String[] names) {
        super (context, R.layout.imagelistlayout, names);
        this.context = context;
        this.names = names;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
       deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //try to hide textview or something it may help
            }
        });
        return rowView;

    }

}`
ud_an
  • 4,939
  • 4
  • 27
  • 43
8

You should try not to hard code your onClick handler in the getView method but look at how you assign a onClick method to a listview. Here you assign the method from the activity and that is what you should do here too.

In you adapter create a method called setOnXXXClickListener

public void setOnXXXClickListener(final OnClickListener onClickListener) {
    this.onXXXClickListener = onClickListener;
}

and in your getView assign this to the button like so

viewHolder.xxx.setOnClickListener(this.onXXXClickListener);

From you Activity you can then assign the onClick method like this

this.adapter.setOnXXXClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "OnXXXClickListener");
    }
});
slott
  • 3,266
  • 1
  • 35
  • 30
  • Excellent answer if you need to call some function not related to the listview adapter ! – Leonardo Jan 09 '14 at 07:37
  • How to get position by this approach ? – Sumit Jul 26 '14 at 14:35
  • If you need the position you would most likely be using the setOnItemClickListener. However you always have the option to user setTag on the view you assign the setOnXXXClickListener and then extract the tag value which could then be the position. – slott Jul 27 '14 at 16:56
  • Can u explain whats "onXXXClickListener" in this.onXXXClickListener? – Sharath Jun 20 '15 at 05:49
  • Say your list item contains a buy button then it could be onBuyButtonClickListener. – slott Jun 21 '15 at 20:25
0
deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.d("TAG", "position: " + position);
        }
    });

And the position you need to make to final in getView(final int position, ... , ...) When you have the position, do whatever you want with it! Delete, modify or whatever..

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • System.out.println(deleteButton.getTag()); By this line I get last value of the position Suppose I have three items in my list the value of position will b 2. – Sandroid Nov 14 '11 at 12:32
  • Don't use tag, what you want is the position in the listview where you clicked the button, right? Then you have the position in getView. If you want to delete the that specific row, just delete it from the list your sending in the adapter at that position.. then use yourAdapter.notifyDataSetChanged(); – Carnal Nov 14 '11 at 12:39
0

Very simple you have position int variable in getView which refers to the item on which button has been inflate so use position variable to delete the item on which you clicked. Just use below code.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
       // deleteButton.setOnClickListener(this); 
deleteButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //use position here
                            // delete the item from data string havng position index and the use notifydataset
            }

        });
        return rowView;

    }
Maneesh
  • 6,098
  • 5
  • 36
  • 55