2014-02-28 167 views
1

我有一個在活動中使用自定義適配器填充的gridview,並且我希望您在另一個gridview中的另一個活動中按下目標項目。android將項目添加到arraylist from arraylist

private static class MyAdapter extends BaseAdapter 
{ 
    static List<Item> items = new ArrayList<Item>(); 
    private LayoutInflater inflater; 

    public MyAdapter(Context context) 
    { 
     inflater = LayoutInflater.from(context); 

     items.add(new Item("Restaurante Arsenal", R.drawable.arsenal)); 

     items.add(new Item("Restaurante The Grill", R.drawable.grill)); 
     items.add(new Item("Rte Las Delicias", R.drawable.delicias)); 
     items.add(new Item("Restaurante Europa", R.drawable.logo_restaurante_europa)); 


     items.add(new Item("Restaurante Coconut", R.drawable.coco)); 
     items.add(new Item("Rte GastroXabia", R.drawable.gastroxabia)); 
    } 

我曾計劃使用此:

MisLocalesFavActivity.MyAdapter.items.add(MyAdapter.items.get(position)); 

所以:

public boolean onItemLongClick(AdapterView<?> parent, View v, final int position, long id) { 
      // TODO Auto-generated method stub 

      String[] opc = new String[] { "Añadir a mis locales favoritos", "Copiar", "Eliminar"}; 



      AlertDialog opciones = new AlertDialog.Builder(
        Restaurantes_Activity.this) 
      .setTitle("Opciones") 
      .setItems(opc, 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, 
         int selected) { 
        if (selected == 0) { 
         //acciones para editar 


         MisLocalesFavActivity.MyAdapter.items.add(MyAdapter.items.get(position)); 





         Toast.makeText(getApplicationContext(), 
           "Añadido a mis locales favoritos! " , Toast.LENGTH_SHORT).show(); 
        } else if (selected == 1) { 
         //acciones para copiar 
        }else if (selected == 2) { 
         //acciones para eliminar 
        } 
       } 
      }).create(); 
      opciones.show(); 

      return true; 
     } 
    }); 

但我有。新增

具體如下錯誤: Add方法(類型列表中的MisLocalesFavActivity.MyAdapter.Item)不適用於參數

(Restaurantes_Activity.MyAdapter.Item)

謝謝!!!

回答

0

替換此,

public MyAdapter(Context context) 
{ 
    inflater = LayoutInflater.from(context); 

    // Add this if condition in your code 
    if(items != null && items.size() != 0) { 
     items.clear(); 
    } 
    items.add(new Item("Restaurante Arsenal", R.drawable.arsenal)); 

    items.add(new Item("Restaurante The Grill", R.drawable.grill)); 
    items.add(new Item("Rte Las Delicias", R.drawable.delicias)); 
    items.add(new Item("Restaurante Europa", R.drawable.logo_restaurante_europa)); 


    items.add(new Item("Restaurante Coconut", R.drawable.coco)); 
    items.add(new Item("Rte GastroXabia", R.drawable.gastroxabia)); 
} 

不要使用這樣的:

MisLocalesFavActivity.MyAdapter.items.add(MyAdapter.items.get(位置));

如果你想挑FoodItem在onItemLongClick特定的位置,那麼你使用方法如下:

if (selected == 0 && position >= 0 && position < items.size()) { 

    FoodItem itemObj = items.get(position); 
    // Do whatever you want .... 
} 

,改變它的型號名稱「項」爲「FoodItem」,它會爲你工作。我希望它會非常有用..........

+0

謝謝,但這不起作用,請按照錯誤。添加 –

0

不要使用staticitems。在MyAdapter中定義一個方法來添加項目。 這是我的代碼來解決你的問題。

MyAdapter

public void addItem(Item item){items.add(item);} 

OverWirte getItem(int position)return items.get(position)

在方法onItemLongClick

adapter.additem(adapter.getItem(position)); 
0

嘗試集中在應用程序類的的ArrayLists。它會幫助你更多。

+0

檢查這個答案,你會得到一個想法。 http://stackoverflow.com/questions/22093997/android-quiz-score-point/22095806#22095806 –