• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Accessing arraylist problem from another class

ridoy

Lurker
Jan 21, 2012
3
0
Hello,i'm new in android development field and in my first project i'm doing a facebook application which loads friends name and birthdates.

Below is the method which returns friends name and birthday..
Code:
//class1.java

    public void getDates_method() throws JSONException, MalformedURLException, IOException, FacebookError
    {
        List<Model> list = new ArrayList<Model>();
        //String name="Test";

        try
        {
            Bundle bdle=new Bundle();
            bdle.putString("fields","name,birthday");
            //facebook.request("me/friends",bdle);
            
            JSONObject json = Util.parseJson(facebook.request("me/friends",bdle));
            View tv=findViewById(R.id.textbox_textview);
            //EditText tvv=(EditText)tv;
            //tvv.setText(" ");
            //edt.setText(json.toString());
            
            
            JSONArray jsonarr=json.getJSONArray("data");
            
            int len=jsonarr.length();
            TotalFriend=len;
            CharSequence l=String.valueOf(len);
            
            //tvv.append(l+"\n");
            String s1,s2;
            for(int i=0;i<len;i++)
            {
                String istring=String.valueOf(i);
                JSONObject j=jsonarr.getJSONObject(i);
                if(j.toString().contains("birthday"))
                {
                    s1=j.getString("name");
                    s2=j.getString("birthday");
                    user_name[i]=s1;
                    birth_dates[i]=s2;
                    list.add(get(user_name[i],birth_dates[i]));
                
    }catch(Exception e)
        {
            showToast(e.getMessage());
        }
    }


I want to show the result in a listview.
Below is the code from where i need to access the Arraylist list..

Code:
public class ShowList extends ListActivity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        List<Model> list = new ArrayList<Model>();
        ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
        setListAdapter(adapter);   

     ///////I need to load the Arraylist list here to print in listview  ///////
   
    }
       private Model get(String s,String place) 
       {
           return new  Model(s,place);
       }
}

I made all those codes in 2 hours but this problem makes me stop and tired for 2 days! So anyone can give good suggestions me how can i load and print the contents of "list" in ShowList class,that was loaded in class1?Thanks for advance..
 
In class1 add a static holder class that can be accessed by ShowList. e.g.

Code:
static class NameBirthdayHolder {
   private static volatile List<Model> namesBirthdays;
   
   public static List<Model> getNamesBirthdays(Context context){
      return namesBirthdays;
   }

   public static void setNamesBirthdays (List<Model> list){
      namesBirthdays = list;
   }
}

Call setNamesBirthdays() at the end of getDates_method(). In ShowList call class1.NameBirthdayHolder.getNamesBirthdays() to get the list.

There might be better ways of doing this, but it's hard to tell what you're doing.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones