2014-02-28 165 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 –