Delete item on ListView after click delete button on ListView

不想你离开。 提交于 2020-01-23 11:18:43

问题


I wrote a code to display images, button & texts dynamically in a ListView. So ListView load the button. I want to delete the selected item in ListView by clicking this button.

Adapter class:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoaderLogoUnder imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoaderLogoUnder(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.inflatelistview, null);

        TextView text=(TextView)vi.findViewById(R.id.textView1);
        ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
        Button btn=(Button)vi.findViewById(R.id.button1);
        btn.setTag(position);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Integer index = (Integer) v.getTag();
                //items.remove(index.intValue());  
                notifyDataSetChanged();

            }
        });
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}

This is a ListView Activity class

ShowData show = new ShowData();

        String s[] = show.getData();
        adapter = new LazyAdapter(this, s);

        inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);

How to do this?


回答1:


You can try using ArrayList instead as below:

 public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<String> data;
private static LayoutInflater inflater=null;
public ImageLoaderLogoUnder imageLoader; 

public LazyAdapter(Activity a, ArrayList<String> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoaderLogoUnder(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.inflatelistview, null);

    TextView text=(TextView)vi.findViewById(R.id.textView1);
    ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
    Button btn=(Button)vi.findViewById(R.id.button1);
    btn.setTag(position);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Integer index = (Integer) v.getTag();
            //items.remove(index.intValue());  
            data.remove(position);
            notifyDataSetChanged();

        }
    });
    text.setText("item "+position);
    imageLoader.DisplayImage(data.get(position), image);
    return vi;
}
}



回答2:


  1. remove the item from "data".
  2. call adapter.notifyDataSetChanged()



回答3:


Your commented-out section in the OnClick listener looks like you are almost there. I would change the type of your data d element from an array of strings to an ArrayList (from private String[] data to private ArrayList data). This will make for easier removal of the item you want gone.

Removal of an item from a simple [] array is more work, you basically have to create a new array and repopulate with the items to keep, return the new array. It doesn't have a "remove" method like ArrayList. The adapter class also doesn't have an "items" member, you have to manipulate the "data" member.

Then change the signature of your adapter constructor to LazyAdapter(Activity a, ArrayList d). Change your "remove" line from items.remove(index.intValue()) to data.remove(position). You don't need btn.setTag or the v.getTag as position is already available. The call to NotifyDataSetChanged() will refresh the listview.

The different constructor will mean you need to send an ArrayList into your adapter. Show.getData() will need to be changed to return an ArrayList, the type of s will need to change from String[] to ArrayList.

This is essentially the same answer that was given, a little more verbose. Hope it helps...




回答4:


@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View row = null;
        LayoutInflater inflater = getLayoutInflater();

        row = inflater.inflate(R.layout.one_result_details_row, parent, false);

        // inflate other items here : 
        Button deleteButton = (Button) row.findViewById(R.id.Details_Button01);
         deleteButton.setTag(position);

        deleteButton.setOnClickListener(
            new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Integer index = (Integer) view.getTag();
                    items.remove(index.intValue());  
                    notifyDataSetChanged();
                }
            }
        );

In the getView() method you tag the ListItem to postion

deleteButton.setTag(position);

Convert the getTag() Object to an Integer

In the OnClickListener() you then delete the item

items.remove(index.intValue());  


来源:https://stackoverflow.com/questions/19142630/delete-item-on-listview-after-click-delete-button-on-listview

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