我将项目添加到ArrayList,然后将其添加到适配器。 这是我的适配器
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;
}
}
这是我的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);
}
}
这里是
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();
}
}
性反应:
{
"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
}
]
}
}
打开Activity LIST IS EMPTY ...但是ArrayList&gt; friendsList不为空。如果我最小化然后最大化应用列表已满。问题在哪里?
答案 0 :(得分:2)
使用onPostExecute()
方法设置您的adpater。
protected void onPostExecute(String file_url)
{
friendsadapter=new LazyAdapterInventory(this, friendsList);
friendslist.setAdapter(friendsadapter);
friendsadapter.notifyDataSetChanged();
}
}
也删除此
friendsadapter=new LazyAdapterInventory(this, friendsList);
friendslist.setAdapter(friendsadapter);
来自onCreate()的。
这是因为您可以使用UI
方法更新onPostExecute()
元素。
的更新:强> 的
在ArrayList
方法之前为onCreate()
声明全局变量。
ArrayList<HashMap<String, String>> friendsList;
并在AsyncTask
方法执行onCreate()
之前对其进行初始化。
friendsList = new ArrayList<HashMap<String, String>>();
在此更改
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);