带有AsyncTask的Android GridView适配器

时间:2013-02-21 11:18:58

标签: java android gridview android-asynctask android-lazyadapter

我从网络解析XML,我想在其他活动中将这些数据放到GridView中,你能帮助我吗?

我的第一项活动:

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

我的AsyncTask

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我只有在下载所有信息后才开始另一项活动

1 个答案:

答案 0 :(得分:1)

我只有在下载所有信息后才开始另一项活动 =&GT;进行如下更改:

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