在另一个类中访问arraylist

时间:2014-04-22 00:31:49

标签: java android arraylist

嗨我有一个arraylist,我想从另一个类访问,我是android的新手请尝试以简单的方式解释

//Adding item to ArrayList
            Cursor cursor2=shoppingListDB.rawQuery("SELECT * FROM " + selected_spinner + ";", null);

            if  (cursor2.moveToFirst()){
                list.clear();
                do{
                    String itemName = cursor2.getString(cursor2.getColumnIndex("ITEM_NAME"));
                    String shopList = cursor2.getString(cursor2.getColumnIndex("SHOP_LIST"));
                    String numbItems = cursor2.getString(cursor2.getColumnIndex("NUMB_ITEMS"));

                   //Adding Items to the arraylist
                    list.add(numbItems + " x"+ " " +itemName + " " + "@"+shopList);
                }
                while (cursor2.moveToNext());


                //====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================
                view_list = (ListView)findViewById(R.id.listItems);
                ArrayAdapter <String> adapter=new ArrayAdapter<String>(CreateList.this,android.R.layout.simple_list_item_1,list);
                view_list.setAdapter(adapter);
            }
            else{
                Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
            }
            cursor2.close();

1 个答案:

答案 0 :(得分:2)

我认为你应该使用基本的OOP概念。

在你的类中,你可以将列表声明为类实例变量,并使用getter方法获取其他类中的列表。

public class abcd {
   private List<String> list;


   public List<String> getList() {
      return list;
   }

   //your code which uses list

}

希望这会对你有所帮助。