Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to setOnclickListener() on the Button inside the ListView?

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.

like image 796
Sandroid Avatar asked Nov 14 '11 12:11

Sandroid


2 Answers

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;

    }

}`
like image 125
ud_an Avatar answered Oct 20 '22 15:10

ud_an


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");
    }
});
like image 40
slott Avatar answered Oct 20 '22 16:10

slott