问题
In my project I want to load url Images and display them in gridview activity. but how to load those image to the gridview adapter?
so I am trying to understand the concept of grid/list view
adapter and as I understand .in a basic example ,it contain 3 activities:
1-Main activity
Useally it contains the definition of gridview
layout , the definition of the method GridViewAdapter
(third activity) ,setting the adapter in the grid view. and the data or the array image(second activity) that to be send in the GridViewAdapter
method.
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);
}
ok my problem is what are the ways to send the image in the gridview adapter?
in this example they used this way , but I dont need TypedArray for my project , actually side question when do I need to use it ?
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;
}
in another example they used resource
mItems = new ArrayList<GridViewItem>();
Resources resources = getResources();
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.aim), getString(R.string.aim)));
:
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.youtube), getString(R.string.youtube)));
in another example they used static images
Ok I do understand they are getting the image from drawable , but what should I do to display them in my activity
-- Ill continue my explanation incase there is something wrong I did
2-Imageitem its the another activity where it store the image no need to go into it .
3-GridViewAdapter
ok here we create the constructor of the class , and we create the getview method , before getting into it , should I add getcount()
method or getitem()
The convertview is used to get the old view, and the holder to hold the image I guess.
ofcourse for now ImageItem
is empty because I didnt define it in the main activity. so how to do that ?
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();
public GridViewAdapter(Context context, int layoutResourceId, ArrayList data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
Picasso.
with(mContext).
load(ImageItem .Getimage())
.placeholder(R.drawable.ic_launcher)
.fit()
.into(holder.imageView);
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
edt
//Downloading data asynchronously
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
try {
// Create Apache HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
int statusCode = httpResponse.getStatusLine().getStatusCode();
// 200 represents HTTP OK
if (statusCode == 200) {
String response = streamToString(httpResponse.getEntity().getContent());
parseResult(response);
result = 1; // Successful
} else {
result = 0; //"Failed
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
// Download complete. Let us update UI
if (result == 1) {
mGridAdapter.setGridData(mGridImages);
mGridAdapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, "No Connection found,Check your Connection!", Toast.LENGTH_SHORT).show();
}
mProgressBar.setVisibility(View.GONE);
}
}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Close stream
if (null != stream) {
stream.close();
}
return result;
}
/**
* Parsing the feed results and get the list
* @param result
*/
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("result");
GridImages item;
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String title = post.optString("name");
String image=post.optString("path");
item = new GridImages();
item.Settitle(title);
item.Setimage(image);
// JSONArray attachments = post.getJSONArray("attachments");
// if (null != attachments && attachments.length() > 0) {
// JSONObject attachment = attachments.getJSONObject(0);
// if (attachment != null)
// item.Setimage(attachment.getString("url"));
//}
mGridImages.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
回答1:
You Should not use bitmap inside the adapter because when it is in listview or gridview it will consume lot of memory which listview cannot recycle at once. So try using picasso to load the bitmap or image from url.
来源:https://stackoverflow.com/questions/32811785/concept-of-grid-list-view-how-to-send-image-to-imageitem-class