ListView从自定义BaseAdapter中删除项目

时间:2016-03-17 21:08:42

标签: android listview baseadapter listviewitem

我有以下BaseAdapter课程:

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

以下onCreate()

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

我的问题是,如何删除onClick方法中单击的项目。我尝试将其从ListViewrowView=null;中移除,但这些都无效。提前致谢

3 个答案:

答案 0 :(得分:2)

在ListViewAdapter中定义一个名为removeRow的方法。

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

一些建议:如果可以,请使用RecyclerView。该框架有更好的方法来处理删除动画等。 请在getView()中使用convertView。每次调用getView时都不要为新视图充气。它会使你的listView更快。

答案 1 :(得分:0)

如果removeViewAt()给你一个错误,那么只需点击该项目的位置并将其从你的arraylist中删除。然后说adapter.notifyDataSetChanged();视图将自行更新。

答案 2 :(得分:0)

从适配器内的列表中删除项目后,请尝试在适配器中执行以下操作:

this.notifyItemRemoved(position);

当position是被移除项目的位置时。