从自定义列表视图中删除项目的问题

时间:2013-12-01 11:28:26

标签: java android listview android-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);

在这里,如果我没有使用自定义列表视图,我可以使用上面的remove()方法从列表视图中删除项目,但我使用自定义列表视图,它在删除方法下显示错误。所以如何删除来自适配器的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);
}

}

1 个答案:

答案 0 :(得分:1)

试试这个..

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

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