gpt4 book ai didi

java - 删除了最后一个 ListView 项目而不是选定的项目

转载 作者:行者123 更新时间:2023-12-01 12:38:51 25 4
gpt4 key购买 nike

我有一个带有项目的 ListView 。当我长按其中一个时,会出现一个删除按钮。但是当我按下它时,它是最后一个被删除的项目,而不是我按下的那个......

这是我的代码:

appList0 = new ArrayList<Appreciation>();
String[] app0 = settings.getString("App0", "aucune remarque").toString().split(";");
for (String app : app0)
appList0.add(new Appreciation(app));
dataAdapter0 = new MyCustomAdapter(this,R.layout.affichageitem, appList0);
ListView listView0 = (ListView) findViewById(R.id.listView0);
listView0.setAdapter(dataAdapter0);

和适配器

private class MyCustomAdapter extends ArrayAdapter<Appreciation> {

private ArrayList<Appreciation> appList;

public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Appreciation> appList) {
super(context, textViewResourceId, appList);
this.appList = new ArrayList<Appreciation>();
this.appList.addAll(appList);
}

private class ViewHolder {
TextView text;
ImageButton button;
}

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

ViewHolder holder=null;
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = vi.inflate(R.layout.affichageitem, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textview);
holder.button = (ImageButton) convertView.findViewById(R.id.editButton);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
holder.button.setVisibility(View.INVISIBLE);
}


holder.text.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) v ;
//To DO
}
});

holder.button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
ListView listView0=(ListView)findViewById(R.id.listView0);

Log.e("DELETING : ",appList0.get(position).getValue());
appList0.remove(position); //DELETING HERE
dataAdapter0.notifyDataSetChanged();

ImageButton iB = (ImageButton) v ;
iB.setVisibility(View.INVISIBLE);
}
});

holder.text.setOnLongClickListener( new View.OnLongClickListener() {
public boolean onLongClick(View v) {
TextView tv = (TextView) v ;
ImageButton editButton = (ImageButton) ((ViewGroup) v.getParent()).findViewById(R.id.editButton);
editButton.setVisibility(View.VISIBLE);
return true;
}
});

Appreciation currentApp = appList.get(position);
holder.text.setText(currentApp.getValue());
holder.button.setTag(currentApp);
return convertView;

}

}

最佳答案

由于getView()针对每个项目运行,因此您需要在按钮上设置一个标签,以便它可以获得正确的位置。

类似于之前

// use position for the tag param
holder.button.setTag(position);
holder.button.setOnClickListener( new View.OnClickListener() {

然后从被单击的View中检索标签并使用该标签

 holder.button.setOnClickListener( new View.OnClickListener() { 
public void onClick(View v) {
int position = ((Button)v).getTag(); // cast the view to Button and get the tag

Log.e("DELETING : ",appList0.get(position).getValue());
appList0.remove(position); //DELETING HERE
dataAdapter0.notifyDataSetChanged();

关于java - 删除了最后一个 ListView 项目而不是选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313788/

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