-1

I have a fragment activity with a custom arraylist format

    public class giftsListFormat {
        String id;
        String name;
        String url;
        String points;
    }
    ArrayList<giftsListFormat> giftList = new ArrayList<giftsListFormat>();

I am trying to access this list format from an asynctask class

private static class MyAsyncTask extends AsyncTask<Void, Void, Void> {

 private MyAsyncTask () {
   [How can i create an object of giftsListFormat here?]
   [How can i access giftList from here]
 }
  }
Veeru
  • 4,936
  • 2
  • 43
  • 61
  • you can send your list as a parameter to constructor of your class, define one constructor for `MyAsyncTask` that get one `List` and in Activity pass your list to this constructor – Shayan Pourvatan Feb 12 '14 at 06:09

3 Answers3

1

Try this - This may be the custom class that you need.

Declare this as global :

ArrayList<giftsListFormat> details = new ArrayList<giftsListFormat>();

    public class giftsListFormat {
    String id;
    String name;
    String url;
    String points;

    public String getid() {
    return id;
    }
    public void setid(String id)
    {
    this.id = id;
    }

    public String getname() {
    return name;
    }
    public void setname(String name)
    {
    this.name = name;
    }

    public String geturl() {
    return url;
    }
    public void seturl(String url)
    {
    this.url = url;
    }

    public String getpoints() {
    return points;
    }
    public void setpoints(String points)
    {
    this.points = points;
    }
}

To put the value into the array list - Do this

    giftsListFormat Detail;
    Detail = new giftsListFormat();
    Detail.setid("test1");
    Detail.setname("test2");

    details.add(Detail); 

Now to retrieve the values put the following code inside the async task

details.get(i).id.toString();
details.get(i).name.toString();
details.get(i).url.toString();

You can iterate over the size of the array list.

details.size();
Bala Prasanna
  • 170
  • 1
  • 9
  • Hi Bala, thanks for the response. I have tried to create a giftsListFormatClass. When i try to access the same in my asynctask class giftsListFormat tmpGiftFormat; tmpGiftFormat=new giftsListFormat(); It throws "No enclosing instance of type GiftItemsListActivity is accessible. Must qualify the allocation with an enclosing instance of type GiftItemsListActivity (e.g. x.new A() where x is an instance of GiftItemsListActivity)." – Veeru Feb 12 '14 at 06:39
  • You can try refering the following post for this. Its quite simple, You can resolve it. http://stackoverflow.com/questions/9744639/must-qualify-the-allocation-with-an-enclosing-instance-of-type-geolocation http://stackoverflow.com/questions/19141792/no-enclosing-instance-of-type-test-is-accessible-must-qualify-the-allocation-wi – Bala Prasanna Feb 12 '14 at 06:55
0

Make "giftList" global variable in your activity. Send the context to your AsyncTask through Constructor. Now you can access that variable as follows:

private static class MyAsyncTask extends AsyncTask<Void, Void, Void> {
 ArrayList<giftsListFormat> giftList;
 private MyAsyncTask (Activity activity) {
    giftList = activity. giftList;
      giftsListFormat ref = giftList.get(pos);
 }
  }
Rama
  • 1,156
  • 1
  • 7
  • 13
  • Hi Rama, thanks. But, what if i need to create an object of type giftsListFormat? – Veeru Feb 12 '14 at 06:11
  • i don't think so make global value is a right Decision, why OP don't send list to the constructor instead of context? – Shayan Pourvatan Feb 12 '14 at 06:12
  • yes you can send that arrayList as parameter.It is every thing based on your requirement. – Rama Feb 12 '14 at 06:13
  • In fact, i would need to add items to the giftList after parsing a json object inside the asynctask. JSONObject currentRecord = (JSONObject) resultsArray .get(i); giftsListFormat tmpFormat=new giftsListFormat(); tmpFormat.id=(String) currentRecord.get("IDN"); tmpFormat.name=(String) currentRecord.get("ItemNameCN"); tmpFormat.points=(String) currentRecord.get("Cost"); tmpFormat.url=(String) currentRecord.get("Image"); System.out.println(tmpFormat); //to the main list giftList.add(tmpFormat); – Veeru Feb 12 '14 at 06:28
0

create a giftsListFormat class like:

public class giftsListFormat{
    String id;
    String name;
    String url;
    String points;

 //create constructure  and assign parameters which you want to set   
   public giftsListFormat(String id, String name,String url, String points){
    this.id = id;
    this.name = name;
    this.url = url; 
    this.points = points;
   } 

  public getId(){
  return id;
  }

  public getName(){
  return name;
  }

  public getUrl(){
  return url;
  }

  public getPoints(){
  return points;
  }
}

And use this class like: ArrayList<giftsListFormat>...

Inside AsyncTask set all your data at the time of initialization..like:

giftsListFormat mData = new giftsListFormat(id, name, url, points);

And when you want to get just use get methods...

Ranjit
  • 5,130
  • 3
  • 30
  • 66