2015-12-29 292 views
0

我有一個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; 
     } 
    } 
} 
+0

您是否試圖在onClick方法中獲取Item對象? –

+0

@DhavalPatel是的,當我點擊按鈕添加,gridview項目應該被添加到rowitem arraylist。 – TeeKay

回答

1

使用setTag和View類的getTag方法,要獲得點擊的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);  
    } 
}); 
+0

沒有工作!最後一個項目仍然是第二個項目。 – TeeKay

+0

我在向rowitem添加項目的代碼中添加了一行。請看一看。 – TeeKay

+0

@TeeKay所以你想要項目點擊的位置,而不是對象? –

相關問題