在我的项目中,我想加载 url 图像并在 gridview 活动中显示它们。但如何将这些图像加载到 gridview 适配器?
所以我试图理解
grid/list view
适配器的概念,据我了解。在一个基本示例中,它包含3个活动:
1-主要活动
通常它包含
gridview
布局的定义,方法GridViewAdapter
(第三个活动)的定义,在网格视图中设置适配器。以及要在 GridViewAdapter
方法中发送的数据或数组图像(第二个活动)。
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);
}
好的,我的问题是在 gridview 适配器中发送图像的方法是什么?
在这个示例中他们使用了这种方式,但我的项目不需要TypedArray,实际上我什么时候需要使用它?
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;
}
在另一个示例中他们使用了资源
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)));
在另一个示例他们使用静态图像
好的,我知道他们正在从可绘制的图像中获取图像,但是我应该如何做才能在我的活动中显示它们
-- 如果我做错了什么,我会继续我的解释
2-Imageitem它是存储图像的另一个活动,无需进入其中。
3-GridViewAdapter
好的,这里我们创建了类的构造函数,并且创建了 getview 方法,在进入之前,我应该添加
getcount()
方法还是 getitem()
我猜,convertview 用于获取旧视图,支架用于保存图像。
当然现在 ImageItem
是空的,因为我没有在主要活动中定义它。那么该怎么做呢?
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;
}
}
编辑
//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();
}
}
您不应该在适配器内使用位图,因为当它在列表视图或网格视图中时,它将消耗大量内存,而列表视图无法立即回收。所以尝试使用 picasso 从 url 加载位图或图像。