Andics
Andics

Reputation: 140

ListView remove item from custom BaseAdapter

I have the following BaseAdapter class:

public class ListViewAdapter extends BaseAdapter{
String [] result, players;
Context context;
View rowView;
int [] imageId;
Holder holder;
private static LayoutInflater inflater=null;
public ListViewAdapter(MainActivity.PageFragment mainActivity, String[] prgmNameList, String[] players, int[] prgmImages) {
    result=prgmNameList;
    this.players=players;
    context=mainActivity.getContext();
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return result.length;
}

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

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

public class Holder
{
    TextView title, players;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    // TODO Auto-generated method stub
    holder=new Holder();
    rowView = inflater.inflate(R.layout.server_layout, null);
    holder.title=(TextView) rowView.findViewById(R.id.textView1);
    holder.players=(TextView) rowView.findViewById(R.id.textView5);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView7);
    holder.title.setText(result[position]);
    holder.players.setText(players[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            new AlertDialog.Builder(context)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Remove")
                    .setMessage("Are you sure you want to remove this server?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(context, "You LongClicked " + result[position], Toast.LENGTH_LONG).show();
                            //REMOVE ITEM
                            notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();
            return false;
        }
    });
    return rowView;
 }
}

And the following onCreate():

            ListView servers =(ListView) view.findViewById(R.id.list_view);
            BaseAdapter e = new ListViewAdapter(this,prgmNameList,players,prgmImages);
            servers.setAdapter(e);

My question is, how can i remove the clicked item in the onClick method. I tried to remove it from the ListView and also rowView=null; but those didn't work. Thanks in advance

Upvotes: 1

Views: 1007

Answers (3)

greenrobo
greenrobo

Reputation: 813

Define a method called removeRow in ListViewAdapter.

public void removeRow(int position) {
    // Remove the element from the results array here.
    notifyDataSetChanged();
}

Some suggestions: Use RecyclerView if you can. That framework has better ways to handle removal animation etc. Please use the convertView in getView(). Don't inflate a new view for every call to getView. It will make your listView much faster.

Upvotes: 2

Hay Zohar
Hay Zohar

Reputation: 238

Once the item was removed from your list inside the adapter, try to do the following in your adapter:

this.notifyItemRemoved(position);

When position is the position of the removed item.

Upvotes: 0

pooja
pooja

Reputation: 1

If removeViewAt() is giving you an error then just get the position of the item clicked and remove it from your arraylist. And then say adapter.notifyDataSetChanged();The view will get updated on its own.

Upvotes: 0

Related Questions