简体   繁体   中英

Remove adapter item with OnClickListener

This is in the inside of a BaseAdapter that gets called in a Fragment. The OnClickListener should remove the hole adapteritem but for some reason it only removes the content but it won't remove the frame. It allways keeps the size of the adapter just the content disapears, the underlining stays too.

public View getView(final int position, View convertView, ViewGroup parent) {
    boolean memCache = true;
    boolean fileCache = true;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
        activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.favorite_list_item, null);
    }

    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    txtTitle.setText(allFavorite.get(position).getTitle());

    Button deleteButton = (Button) convertView.findViewById(R.id.delete_button);
    deleteButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            ((View) view.getParent()).setVisibility(View.GONE);
        }
    });

    return convertView;
  }
}

try like this,

deleteButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            adapter.remove(adapter.getItem(position));
        }
    });

hope it will help you

You should remove the data bind to your view from your data set and call a notifyDataSetChanged() afterward rather than hide the view itself.

Button deleteButton = (Button) convertView.findViewById(R.id.delete_button);
    deleteButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            allFavorite.remove(position);
            notifyDataSetChanged();
        }
    });

Your code just make view invisible, not delete them. You have to delete from adapter like this :

listItem.remove(position);
adapter.notifyDataSetChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM