gpt4 book ai didi

java - 如何从 Android 上的 ListView 中删除项目

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:04 25 4
gpt4 key购买 nike

我想在长按时删除项目。首先我想获得位置,然后我想删除任何项目。我的主要 Activity

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

请帮助我如何从以下 ListView 中删除所选项目:

最佳答案

我假设您想在按下按钮时删除列表中的项目 - 在调用按钮按下的函数中,获取项目位置并删除该项目,如下所示:

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 而不是位置 - 显然你必须在函数参数中使用位置变量,因为它给出了一个整数,其中项目位于 ListView 中。

关于java - 如何从 Android 上的 ListView 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com