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

寵の児 提交于 2019-12-07 04:08:33

问题


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.


回答1:


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;

    }

}`



回答2:


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



回答3:


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..




回答4:


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;

    }


来源:https://stackoverflow.com/questions/8121476/how-to-setonclicklistener-on-the-button-inside-the-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!