我有一個Adapter
類爲gridview
增加項目。我正在使用ArrayList
來存儲點擊的gridview
項目的某些細節。我面臨的問題是最後一個元素不能單獨識別。如果我點擊最後一個元素,它會提供關於它上面元素的信息。休息所有其他元素都很好。Arraylist讀取最後一個元素作爲第二個最後一個元素
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;
}
}
}
您是否試圖在onClick方法中獲取Item對象? –
@DhavalPatel是的,當我點擊按鈕添加,gridview項目應該被添加到rowitem arraylist。 – TeeKay