1

我在教程中有一个自定义的 ArrayAdapter

这是代码:

public class EntryAdapter extends ArrayAdapter {

    private Context context;
    private ArrayList<Item> items;
    private LayoutInflater vi;

    public EntryAdapter(Context context,ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        final Item i = items.get(position);
        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.list_item_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());
            }else{
                EntryItem ei = (EntryItem)i;
                v = vi.inflate(R.layout.list_item_entry, null);
                final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);

                if (title != null) 
                    title.setText(ei.title);
                if(subtitle != null)
                    subtitle.setText(ei.subtitle);

            }
        }
        return v;
    }

}

现在我正在尝试使用此 ArrayAdapter 从列表视图中删除一行

这是我到目前为止所尝试的:

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        DataHandler.itemToCart.remove(position);
        DataHandler.pricesOnCart.remove(position);
        items.remove(position);
        for(int i = 0 ; i<DataHandler.itemToCart.size(); i ++){
            Log.e("DataHandler.itemToCart", ""+DataHandler.itemToCart.get(i).toString());
            Log.e("DataHandler.pricesOnCart", ""+DataHandler.pricesOnCart.get(i).toString());
        }
        adapter.notifyDataSetChanged();
    }

ListView包含两个TextViews我填充的,并且DataHandler.itemToCartDataHandler.pricesOnCart一个itemsArrayList,它是ListView.

我尝试使用items.remove(position)从 ListView 中删除一行,但它对我不起作用。

4

0 回答 0