如何在Android上从Listview中删除项目

时间:2016-02-22 15:15:45

标签: java android listview

我想在长时间点击时删除项目。首先ı想要获得位置然后ı想要删除任何项目。 我的主要活动

public class MainActivity extends AppCompatActivity {

    final List<Kisi> kisiler = new ArrayList<Kisi>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        kisiler.add(new Kisi("Ahmet Yılmaz", false, 0));
        kisiler.add(new Kisi("Ayşe Küçük", true, 1));
        kisiler.add(new Kisi("Fatma Bulgurcu", true, 2));
        kisiler.add(new Kisi("İzzet Altınmeşe", false, 3));
        kisiler.add(new Kisi("Melek Subaşı", true, 4));
        kisiler.add(new Kisi("Selim Serdilli", false, 5));
        kisiler.add(new Kisi("Halil İbrahim", false, 6));

        final ListView listemiz = (ListView) findViewById(R.id.liste);
        final ozelAdapter adaptorumuz = new ozelAdapter(this, kisiler);
        listemiz.setAdapter(adaptorumuz);
    }
}

我的适配器:

package com.endroidteam.customlistview;


public class ozelAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Kisi> mKisiListesi;

    public  ozelAdapter(Activity activity, List<Kisi> kisiler) {
        mInflater = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        mKisiListesi = kisiler;
    }

    @Override
    public int getCount() {
        return mKisiListesi.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View satirView;

        satirView = mInflater.inflate(R.layout.satir, null);
        TextView textView =
                (TextView) satirView.findViewById(R.id.isimsoyisim);
        ImageView imageView =
                (ImageView) satirView.findViewById(R.id.simge);

        Kisi kisi = mKisiListesi.get(position);
        textView.setText(kisi.getIsim());

        if (kisi.isKadinMi()) {
            imageView.setImageResource(R.drawable.android_icon_big_13);
        }
        else {
            imageView.setImageResource(R.drawable.android_icon_big_4);
        }
        return satirView;
    }
}

我的GetterSetter:

  private String isim;
    private boolean kadinMi;
    private int konum;


    public Kisi(String isim, boolean kadinMi, int konum) {
        super();
        this.isim = isim;
        this.kadinMi = kadinMi;
        this.konum = konum;
    }

    @Override
    public String toString() {
        return isim;
    }

    public String getIsim() {
        return isim;
    }

    public void setIsim(String isim) {
    this.isim = isim;
    }

    public  int getKonum()  {
        return konum;
    }

    public void setKonum(int konum) {
        this.konum = konum;
    }

    public boolean isKadinMi() {
        return kadinMi;
    }

    public void setKadinMi(boolean kadinMi) {
        this.kadinMi = kadinMi;
    }
}

请帮助我如何从以下列表视图中删除所选项目:

2 个答案:

答案 0 :(得分:0)

从适配器中删除元素并使用

通知listView
adapter.notifyDataSetChanged()

答案 1 :(得分:0)

我假设您想要在按下按钮时删除列表中的项目 - 在按下按钮的功能中,获取项目位置并删除项目,如下所示:

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
    //Do some
    return true;
}

});

kissiler.remove(position)

然后致电

adapter.notifyDataSetChanged()

这将解决您的问题。

完整代码:

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
    //Do some
    kissiler.remove(position);
    adapter.notifyDataSetChanged();
    return true;
}});
编辑:我犯了一个可怕的错误并且使用了id而不是位置 - 显然你必须在函数参数中使用位置变量,因为它给出了一个整数,其中项目位于列表视图中。