gpt4 book ai didi

java - 为什么gridview不显示任何项目?仅在恢复 Activity 后才显示

转载 作者:行者123 更新时间:2023-12-02 06:00:33 25 4
gpt4 key购买 nike

我将项目添加到 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;
}


}

这是我的主要 Activity

 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 后为空...但 ArrayList>friendsList 不为空。如果我最小化然后最大化应用程序列表已满。问题出在哪里?

最佳答案

onPostExecute() 方法中设置您的适配器。

 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()。

这是因为您可以在 onPostExecute() 方法中更新您的 UI 元素。

更新:

还在 onCreate() 方法之前为您的 ArrayList 声明全局变量。

  ArrayList<HashMap<String, String>> friendsList;

并在 onCreate() 方法上执行 AsyncTask 之前对其进行初始化。

 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);

关于java - 为什么gridview不显示任何项目?仅在恢复 Activity 后才显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22710936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com