如何在onPostexecute中使用gridview AsyncTask

时间:2017-12-03 10:12:24

标签: android android-studio android-gridview asynctaskloader

我有以下代码,我希望首先从drawable文件夹和api之后加载图像。 图像使用getData()方法从可绘制文件夹成功加载但我不知道如何从网址加载图片并更新gridview onPostExecute 方法,当我在注释行下面更新我的网格时崩溃应用程序,这就是为什么我在方法 onPostExecute 方法中评论我的gridview代码请检查并帮助我

  

我是Android新手请展示示例

public class MainActivity extends AppCompatActivity  {
    private GridView gridView;
    private GridViewAdapter gridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      gridView = (GridView) findViewById(R.id.gridView);
      gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
        gridView.setAdapter(gridAdapter);
        APICaller ob = new APICaller();
        ob.execute();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ImageItem item = (ImageItem) parent.getItemAtPosition(position);

                //Create intent
                Intent intent = new Intent(MainActivity.this, abc.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("image", item.getImage());

                //Start details activity
                startActivity(intent);
            }
        });
    }

    /**
     * Prepare some dummy data for gridview
     */
    private ArrayList<ImageItem> getData() {
        final ArrayList<ImageItem> imageItems = new ArrayList<>();
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }
        return imageItems;
    }

    private ArrayList<ImageItem> getData(appmodel[] data) {
        Toast.makeText(getApplicationContext(),
                data.toString(), Toast.LENGTH_LONG).show();
        final ArrayList<ImageItem> imageItems = new ArrayList<>();
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }
        return imageItems;
    }

class APICaller extends  AsyncTask{

        protected  void onPostExecute(Object result)
        {
          String data = (String)result;       
            Gson g = new Gson();
            Type t = new TypeToken<appmodel[]>(){}.getType();
            appmodel[] appm = (appmodel[])g.fromJson(data,t);
        //  gridView = (GridView) findViewById(R.id.gridView);
     //       gridAdapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, getData());
     //     gridView.setAdapter(gridAdapter);            


        }
    @Override
    protected Object doInBackground(Object[] objects) {

        HttpClient client = new DefaultHttpClient();
        HttpGet getlish =  new HttpGet("http://test.abc.com/api/test");
        ResponseHandler<String> handler = new BasicResponseHandler();
        Object result = new Object();
        try {
            result = client.execute(getlish,handler);
        }
        catch(Exception e)
        {


        }
        return result;
    }
}
}

当我使用下面的方法检查日志时,它显示了json数据

 protected  void onPostExecute(Object result)
        {
          String data = (String)result;       
            Log.d("myTag", data);
}

JSON

  [ {
        "apptype": 1,
        "imagename": "test",
        "imageurl": "http://test.abc.com/androidimages/test/test.jpg"
        }
     ]

1 个答案:

答案 0 :(得分:0)

在您的适配器类中添加一个新方法,如

public class GridViewAdapter extends BaseAdapter{

    private ArrayList<ImageItem> mItemList;

    public void updateItemList(ArrayList<ImageItem> newItemList){
        this.mItemList = newItemList;
        notifyDataSetChanged();
    }

}

和onPostExecute调用此方法并设置新列表。所以你的onPost看起来像这样

protected  void onPostExecute(Object result)
        {
          String data = (String)result;       
            Gson g = new Gson();
            Type t = new TypeToken<appmodel[]>(){}.getType();
            appmodel[] appm = (appmodel[])g.fromJson(data,t);
            gridAdapter.updateItemList(getData());         


        }

要使用网址加载imagview,您可以使用Picasso Library,因为您的参考资料看起来像这样

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

添加以下依赖项以使用picasso

compile 'com.squareup.picasso:picasso:2.5.2'