gpt4 book ai didi

java - Android GridView 适配器与 AsyncTask

转载 作者:行者123 更新时间:2023-12-01 14:49:45 25 4
gpt4 key购买 nike

我从网络解析 XML,我想将此数据放入其他 Activity 中的 GridView,你能帮我吗?

我的第一个 Activity :

public class HomeScreenActivity extends Activity {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
Intent intent = new Intent(this, TopArtistsScreen.class);
intent.putExtra("username", username);
new DownloadXmlTask(this, HomeScreenActivity.this)
.execute("URL");
startActivity(intent);
}

我的异步任务

public class DownloadXmlTask extends AsyncTask<String, Void, List<Artist>> {
@Override
protected void onPostExecute(List<Artist> result) {
progressDialog.dismiss();
gridView.setAdapter(new TopArtistsImageAdapter(context, result));
}

TopArtistsImageAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView==null) {
view = inflater.inflate(R.layout.items, null);
}
TextView textView = (TextView) view.findViewById(R.id.grid_item_name);
ImageView imageView = (ImageView) view.findViewById(R.id.grid_item_image);
textView.setText(artistsList.get(position).getName());
imageLoader.DisplayImage(artistsList.get(position).getImage(), imageView);
return view;
}

PS 我应该在下载所有信息后才开始另一个 Activity

最佳答案

只有在下载完所有信息后我才能开始另一个 Activity => 进行如下更改:

1) 仅在单击按钮时执行 AsyncTask

new DownloadXmlTask(this, HomeScreenActivity.this).execute("URL");

2) 在 AsyncTask 的 onPostExecute() 方法中启动 Activity:

Intent intent = new Intent(HomeScreenActivity.this, TopArtistsScreen.class);
intent.putExtra("username", username);
startActivity(intent);

关于java - Android GridView 适配器与 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15000909/

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