gpt4 book ai didi

java - 从自定义 ListView 中删除项目的问题

转载 作者:行者123 更新时间:2023-11-30 03:14:47 24 4
gpt4 key购买 nike

我正在使用自定义 ListView 来显示项目。

SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
lv,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
View child = listView.getChildAt(position);
if(child != null) {
TextView cat = (TextView) child.findViewById(R.id.Category_name);
TextView pri = (TextView) child.findViewById(R.id.Price);
TextView det = (TextView) child.findViewById(R.id.Details);
String time = det.getText().toString().substring(9);
db_data.delete(cat.getText().toString(), pri.getText().toString(), time,edit_date);
adapter.remove(adapter.getItem(position));
}
}
adapter.notifyDataSetChanged();
}
});
lv.setOnTouchListener(touchListener);

在这里,如果我没有使用自定义 ListView ,我可以使用上面的 remove() 方法从 ListView 中删除项目,但我将其用于自定义 ListView ,它在删除方法下显示错误。那么如何删除一个来自适配器的带有 onDismiss 的项目。

CustomListAdapter_Data_Edit.java

public class CustomListAdapter_Data_Edit extends BaseAdapter{

private ArrayList<Data> listData;
private LayoutInflater layoutInflater;
private ArrayList<Data> arraylist;

public CustomListAdapter_Data_Edit(Context context, ArrayList<Data> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<Data>();
this.arraylist.addAll(listData);
}

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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item_row_for_data, null);
holder = new ViewHolder();
holder.CatNameView = (TextView) convertView.findViewById(R.id.Category_name);
holder.DetailsView = (TextView) convertView.findViewById(R.id.Details);
holder.PriceView = (TextView) convertView.findViewById(R.id.Price);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.CatNameView.setText(listData.get(position).getCat());
holder.DetailsView.setText(listData.get(position).getTime());
holder.PriceView.setText(listData.get(position).getPrice());

return convertView;
}

static class ViewHolder {
TextView CatNameView;
TextView DetailsView;
TextView PriceView;
}

public void removeRecord(int index) {
arraylist.remove(index);
}

最佳答案

试试这个..

listData.remove(postion);    // this will remove the item from the ArrayList 

adapter.notifyDataSetChanged(); //this will refresh your adapter

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

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