Reputation: 12857
I add items to ArrayList and then add it to adapter. here is my adapter
public class LazyAdapterInventory extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater=null;
public LazyAdapterInventory(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public void clearCache() {
imageLoader.clearCache();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_list_all, null);
final TextView title = (TextView)vi.findViewById(R.id.title); // title
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(convertFromUTF8(song.get("key_title")));
return vi;
}
}
here is my MainActivity
public class UserInfoActivity extends SherlockActivity {
ArrayList<HashMap<String, String>> friendsList = new ArrayList<HashMap<String, String>>();
LazyAdapterInventory friendsadapter;
private GridView friendslist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
friendsadapter=new LazyAdapterInventory(this, friendsList);
friendslist=(GridView)findViewById(R.id.friends_list);
friendslist.setAdapter(friendsadapter);
GetInfoList().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
here is get
class GetInfoList extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
JSONObject json = jParser.makeHttpRequest(URL, "GET", param);
JSONObject friendslist = json.getJSONObject("friendslist");
JSONArray friends = friendslist.getJSONArray("friends");
for (int i =0;i<friends.length();i++){
JSONObject friend = friends.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("key_title", friend.getString("steamid"));
friendsList.add(map);
}
return null;
}
protected void onPostExecute(String file_url)
{
friendsadapter.notifyDataSetChanged();
}
}
Responce:
{
"friendslist": {
"friends": [
{
"steamid": "76561197966153504",
"relationship": "friend",
"friend_since": 1395491684
},
{
"steamid": "76561198002475521",
"relationship": "friend",
"friend_since": 1393012575
},
{
"steamid": "76561198034678588",
"relationship": "friend",
"friend_since": 1395407339
},
{
"steamid": "76561198040519811",
"relationship": "friend",
"friend_since": 1395688168
},
{
"steamid": "76561198052749663",
"relationship": "friend",
"friend_since": 1395997768
},
{
"steamid": "76561198060262946",
"relationship": "friend",
"friend_since": 1360606275
},
{
"steamid": "76561198068379313",
"relationship": "friend",
"friend_since": 1395068101
},
{
"steamid": "76561198073896175",
"relationship": "friend",
"friend_since": 1394910978
},
{
"steamid": "76561198074813543",
"relationship": "friend",
"friend_since": 1394523426
},
{
"steamid": "76561198081103330",
"relationship": "friend",
"friend_since": 1395810398
},
{
"steamid": "76561198082945313",
"relationship": "friend",
"friend_since": 1394913814
},
{
"steamid": "76561198084403469",
"relationship": "friend",
"friend_since": 1393667423
},
{
"steamid": "76561198092220408",
"relationship": "friend",
"friend_since": 1394910366
},
{
"steamid": "76561198092249389",
"relationship": "friend",
"friend_since": 1395447861
},
{
"steamid": "76561198096843914",
"relationship": "friend",
"friend_since": 1373139292
},
{
"steamid": "76561198099008433",
"relationship": "friend",
"friend_since": 1395810393
},
{
"steamid": "76561198109190777",
"relationship": "friend",
"friend_since": 1394211966
},
{
"steamid": "76561198112623128",
"relationship": "friend",
"friend_since": 1395810497
},
{
"steamid": "76561198112677589",
"relationship": "friend",
"friend_since": 1395395453
},
{
"steamid": "76561198112762255",
"relationship": "friend",
"friend_since": 1395429304
},
{
"steamid": "76561198114529800",
"relationship": "friend",
"friend_since": 1395423621
},
{
"steamid": "76561198115955698",
"relationship": "friend",
"friend_since": 1395429444
},
{
"steamid": "76561198118605481",
"relationship": "friend",
"friend_since": 1393187401
},
{
"steamid": "76561198119746587",
"relationship": "friend",
"friend_since": 1395397411
},
{
"steamid": "76561198121134047",
"relationship": "friend",
"friend_since": 1395810452
},
{
"steamid": "76561198121560711",
"relationship": "friend",
"friend_since": 1395997769
},
{
"steamid": "76561198122678853",
"relationship": "friend",
"friend_since": 1389529761
},
{
"steamid": "76561198123336629",
"relationship": "friend",
"friend_since": 1393012567
}
]
}
}
After opening Activity LIST IS EMPTY...but ArrayList> friendsList is not empty . If I minimaze and then maximaze app list is full. Where is issue?
Upvotes: 0
Views: 102
Reputation:
Set your adpater in onPostExecute()
method.
protected void onPostExecute(String file_url)
{
friendsadapter=new LazyAdapterInventory(this, friendsList);
friendslist.setAdapter(friendsadapter);
friendsadapter.notifyDataSetChanged();
}
}
also remove this
friendsadapter=new LazyAdapterInventory(this, friendsList);
friendslist.setAdapter(friendsadapter);
from onCreate().
This is because you can update your UI
elements in onPostExecute()
method.
UPDATE:
Also declare global variable for your ArrayList
before onCreate()
method.
ArrayList<HashMap<String, String>> friendsList;
and initialize it before your AsyncTask
can be executed on onCreate()
method.
friendsList = new ArrayList<HashMap<String, String>>();
Change here
for (int i =0;i<friends.length();i++){
JSONObject friend = friends.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("steamid", friend.getString("steamid"));
map.put("relationship", friend.getString("relationship"));
map.put("friend_since", friend.getString("friend_since"));
friendsList.add(map);
Upvotes: 2