0

Hi i have an arraylist that i want to access from another class, i'm new to android please try to explain in a simple way

//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();
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • You should clarify the issue, but looks like you simply need to expose the `List` via a property accessor method e.g. `public List getItems()`. – alphazero Apr 22 '14 at 00:43
  • @alphazero from the above code where can i put 'public List getItems()'? – Junius L Apr 22 '14 at 00:48
  • See @aksdch11 answer below. If you wish to insure that the caller of the "accessor method" `getList` does not change the content of your list, return a copy of the list. See http://stackoverflow.com/questions/689370/java-collections-copy-list-i-dont-understand – alphazero Apr 22 '14 at 12:28

1 Answers1

2

I think you should use basic OOP concept for this.

in your class you can declare the list as class instance variable and use getter method to get the list in other class.

public class abcd {
   private List<String> list;


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

   //your code which uses list

}

Hope this will help you.

aksdch11
  • 683
  • 6
  • 14