Arraylist将最后一个元素作为第二个元素读取

时间:2015-12-29 10:45:26

标签: java android gridview arraylist baseadapter

我有一个Adapter类,可以为gridview的项目进行通胀。我使用ArrayList来存储点击的gridview项的某些详细信息。我面临的问题是最后一个元素无法单独识别。如果我点击最后一个元素,它会提供有关它上面元素的信息。休息所有其他元素都很好。

public class MyAdaptertwo extends BaseAdapter
{
    public List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;
    int j;
    public static List<list> rowitems = new ArrayList<list>();
    static int size=0;

    public MyAdaptertwo(Context context)
    {
        inflater = LayoutInflater.from(context);
        items.add(new Item("S", R.drawable.s,"1",R.drawable.v));
        items.add(new Item("F", R.drawable.t,"2",R.drawable.v));
    }

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

    @Override
    public Object getItem(int i)
    {
        j=i;
        return items.get(i);
    }

    public void setCost(int i,String a)
    {
        items.get(i).cost=a;
    }

    @Override
    public long getItemId(int i)
    {
        return items.get(i).colorId;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
        View v = view;
        ImageView picture;
        ImageView vn;
        TextView name;
        TextView costs;
        if(v == null)
        {
            v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.vnon, v.findViewById(R.id.vnon));
            v.setTag(R.id.text, v.findViewById(R.id.text));
            v.setTag(R.id.cost,v.findViewById(R.id.cost));
        }

        picture = (ImageView)v.getTag(R.id.picture);
        vn = (ImageView)v.getTag(R.id.vnon);
        name = (TextView)v.getTag(R.id.text);
        costs = (TextView)v.getTag(R.id.cost);
        Item item = (Item)getItem(i);
        picture.setImageResource(item.colorId);
        name.setText(item.name);
        costs.setText(item.cost);
        vn.setImageResource(item.vnId);
        Button add=(Button)v.findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)  
            {
               list item = new list(items.get(j - 1).colorId, items.get(j - 1).name, Integer.toString(co[j-1]), Integer.toString(no[j - 1]));
                    rowitems.add(item);
              //something
            }
        });

        Button sub=(Button)v.findViewById(R.id.sub);
        sub.setOnClickListener(new View.OnClickListener() {  
            @Override
            public void onClick(View v) 
            {
               //something
            }
        });

        return v;
    }

    private class Item
    {
        final String name;
        final int colorId;
        String cost;
        final int vnId;

        Item(String name, int drawableId,String cost,int vnId)
        {
            this.name = name;
            this.colorId = drawableId;
            this.cost=cost;
            this.vnId=vnId;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用View类的setTaggetTag方法,在onClick方法中单击Item对象。

Item item = (Item)getItem(i);
Button add=(Button)v.findViewById(R.id.add);
add.setTag(i);
add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)  {
        int position = (int)v.getTag();
        //do something with item object
        Item item = items.get(position);
        list listItem = new list(item.colorId, item.name, Integer.toString(co[position]), Integer.toString(no[position]));
        rowitems.add(listItem);       
    }
});