我有以下代码,我希望首先从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"
}
]