在我的Android应用程序中。我已经为客户端服务器请求编写了一个asynctask,在doInBackground部分我已经获得了对Android应用程序的服务器响应为json。代码如下:
String status=sb.toString();
JSONObject jsonResponse1;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse1 = new JSONObject(status);
/***** Returns the value mapped by name if it exists and is a JSONArray. Returns null otherwise.*******/
JSONArray jsonMainNode=jsonResponse1.optJSONArray("Android");
/*********** Process each JSON Node ************/
int lengthJsonArr = jsonMainNode.length();
Log.d("Json Array Length",String.valueOf(lengthJsonArr));
for(int j1=0;j1<lengthJsonArr;j1++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(j1);
/******* Fetch node values **********/
String index=jsonChildNode.optString("index").toString();
String imagename=jsonChildNode.optString("imagename").toString();
byte[] decodedString = Base64.decode(imagename, Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imagearraylist.add(decodedByte);
//This arraylist is an Bitmap arraylist and contains the Bitmaps of two images received from the server..
}
}
catch(Exception ex)
{
System.out.print(ex);
}
这是在asynctask的doinBackground部分。现在在onpostexecute部分,我编写了以下代码。
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(Gallery.this, "Loading complete", Toast.LENGTH_LONG).show();
pd.dismiss();
gridView.setAdapter(new ImageAdapter2(getApplicationContext(),imagearraylist));
}
ImageAdapter2是我项目中我的类的名称..我将展示ImageAdapter2.class的代码
package com.example.mygallery;
//skipping the import section
public class ImageAdapter2 extends BaseAdapter{
private Context mContext;
ArrayList<Bitmap>mImageArray;
public ImageAdapter2(Context c,ArrayList<Bitmap> imgArray) {
// TODO Auto-generated constructor stub
mContext=c;
mImageArray=imgArray;
Log.d("Entered into imageadapter2","Success");
System.out.println("Arr len: "+imgArray.size());
//Here I get output as 2 in the logcat.Upto this portion the code is executing fine..
//After this the getView function is not executing. What is the error in this?? Can someone point it out...
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = null ;
int size=mImageArray.size();
System.out.println("sze: "+size);
Log.d("Size of Image bitmap array",String.valueOf(size));
if (convertView == null )
{
i = new ImageView(mContext);
for(int i1=0;i1<size;i1++)
{
i.setImageBitmap(mImageArray.get(i1));
}
}
else
i = (ImageView) convertView;
return i;
}
}
当我尝试使用imageview在gridview中显示图像时。它没有在我的活动上显示任何图像。虽然位图arraylist中有Bitmaps ..可能有人指出此代码中的错误..提前谢谢......
答案 0 :(得分:1)
getCount()返回0.它应该返回mImageArray.size()。
进一步从getView()中删除for循环。必须始终完成setImageBitmap()。在返回之前做它;
答案 1 :(得分:0)
/******* Fetch node values **********/
String index=jsonChildNode.optString("index").toString();
String imagename=jsonChildNode.optString("imagename").toString();
byte[] decodedString = Base64.decode(imagename, Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imagearraylist.add(decodedByte);
这部分看起来不太好。你从json获得imagename作为字符串&amp;试图将其转换为位图。 相反,尝试这种方法: - 从JSON获取图像名称或URL(在此休息调用中) - 然后,在后台线程/异步任务/服务中从URL获取图像。
您可以找到做{@ {}}
的最佳方式或者,如果你想提前一步,请参考毕加索 http://developer.android.com/training/displaying-bitmaps/index.html 这是我之前提到的一个项目。