问题
i have following code , i want image load first from drawable folder and after api. image successfully load from drawable folder using getData() method, but i don't know how to load image from url and update gridview. onPostExecute method when i update my grid below comment line crash the application that's why i comment my gridview code in method onPostExecute method please check and help me this
I am New in Android please show example
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;
}
}
}
when i check in log using below method its shows json data
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:
Add a new method in your adapter class like this
public class GridViewAdapter extends BaseAdapter{
private ArrayList<ImageItem> mItemList;
public void updateItemList(ArrayList<ImageItem> newItemList){
this.mItemList = newItemList;
notifyDataSetChanged();
}
}
and onPostExecute call this methode and set new list. So your onPost looks like this
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());
}
To load imagview using url you can use Picasso Library , for your refrence it looks like this
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
add following dependency to use picasso
compile 'com.squareup.picasso:picasso:2.5.2'
来源:https://stackoverflow.com/questions/47617365/how-to-use-gridview-in-onpostexecute-asynctask