-5

I want to declare arrayList into this line:

public class tlcity extends Activity {

    //ArrayList<String> idArray = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ....


and into the other method,for example this method:

 protected void onPostExecute(String result) {

          //fill the arraylist
          ...


and into the other method for example this method read arraylist data:

public void readlist(){
   //read the arraylist data and show
}


How can i do this?

behzad razzaqi
  • 11
  • 1
  • 1
  • 3

7 Answers7

4

You can declare ArrayList like this

ArrayList<String> list;

    list = new ArrayList<String>();

You can add, remove items in ArrayList Like this

list.add("A");
list.remove(0);
Prabhakar
  • 1,781
  • 1
  • 17
  • 23
1
ArrayList<String> abc=new ArrayList<String>();
Khubab Hamza
  • 184
  • 12
1

You can initialize or create an instance of your array list like this idArray = new ArrayList();

You can perform any operations to it using idArray object. For example you can add items like this idArray.add("item1");//In you case its a list of strings.

Suhas K
  • 74
  • 8
0

In the same way you would do that in another Java app / class:

public class tlcity extends Activity {   


List<String> idArray;   

protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);
    idArray = new ArrayList<String>();   
}

protected void onPostExecute(String result) { 
   idArray.add("One");  
   idArray.add("Two");  
   idArray.add("Three");  
   ...
}

public void readlist(){
  for (final String element : idArray) {
    // Use the nth string
  }
}
Andrea
  • 2,714
  • 3
  • 27
  • 38
0

I want to declare arrayList into this line:

ArrayList<String> myList;

and into the other method,for example this method:

myList = new ArrayList<String>;

and into the other method for example this method read arraylist data:

for(int i=0; i<myList.size(); i++)
System.out.println(myList.get(i).toString());
Karan
  • 2,120
  • 15
  • 27
0

If you want to use ArrayList locally then declare it locally. if you want to use it in all methods then declare it globally in class.

public class tlcity extends Activity {

    ArrayList<String> idArray = new ArrayList<>();  // to Use this arraylist globally.
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<String> localaraaylist = new ArrayList<>();  //to use arraylist in only in oncreate method.
    ....
Shvet
  • 1,159
  • 1
  • 18
  • 30
0

According to your post, telling how to declae ArrayList will not enough as you have some methods like onPreExecute() which is a method ofAsyncTask Interface.

Look at this,

public class MainActivity extends ActionBarActivity {

ArrayList<String> arrayList; // declaring ArrayList here

ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayList = new ArrayList<String>(); // Initializing arrayList

    arrayList.add("initial text"); // adding a data to arrayList

    ListView listView = (ListView)findViewById(R.id.listView);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList); // setting the arrayList in ArrayAdapter
    listView.setAdapter(adapter);

    new LongOperation().execute(); // async Task


}


  private class LongOperation extends AsyncTask<Void, Void, Void> {

    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

    // progress dialog starts here
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    progressDialog.setMessage("Loading...");
    progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        // for understanding purpose, i made a thread to sleep for 5 sec and later it will add A,B & C to ArrayList.

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

       // adding few more items to arrayList

        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("C");

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressDialog.dismiss(); // dismissing the progress Dialog
        adapter.notifyDataSetChanged(); // refreshing listview

        readA(); // read the arrayList 


    }


}

public void readA()
{
    for (int i = 0; i<arrayList.size(); i++)
    {
        Log.d("key",arrayList.get(i));

    }
}
}

Output :

If you run the above code, Initially your list view will only contain only one item & after 5 sec loading it will add another 3 items. The below information will print in logcat that reads the ArrayList.

04-13 14:07:32.395    1123-1123/? D/key﹕ initial text
04-13 14:07:32.395    1123-1123/? D/key﹕ A
04-13 14:07:32.395    1123-1123/? D/key﹕ B
04-13 14:07:32.395    1123-1123/? D/key﹕ C
Yuva Raj
  • 3,881
  • 1
  • 19
  • 30