Android 在同一ListView/GridView中调用多个JSON数组
更新:感谢Hariharan先生,现在我可以将两个JSON数组添加到一个ListView中(我已经更新了我的代码) 现在我需要将未收集的邮票更改为黑白颜色,我有一个代码可以更改黑白颜色,但是如果我实现了这一点,它会将所有邮票图像更改为黑白颜色 我应该怎么做才能只对未收集的邮票实施B/W: 这是更改图像颜色的功能:Android 在同一ListView/GridView中调用多个JSON数组,android,json,gridview,android-listview,android-json,Android,Json,Gridview,Android Listview,Android Json,更新:感谢Hariharan先生,现在我可以将两个JSON数组添加到一个ListView中(我已经更新了我的代码) 现在我需要将未收集的邮票更改为黑白颜色,我有一个代码可以更改黑白颜色,但是如果我实现了这一点,它会将所有邮票图像更改为黑白颜色 我应该怎么做才能只对未收集的邮票实施B/W: 这是更改图像颜色的功能: public Bitmap toGrayscale(Bitmap bmpOriginal) { int width, height;
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
这是ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView tvStampName;
ImageView stampImage;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.single_stamp_layout, parent, false);
// Get the position
resultp = data.get(position);
tvStampName = (TextView) itemView.findViewById(R.id.tvStampName);
stampImage = (ImageView) itemView.findViewById(R.id.stampImage);
tvStampName.setText(resultp.get(StampsActivity.TAG_STAMP_NAME));
// Passes stampImage images URL into ImageLoader.class
imageLoader.DisplayImage(TAG_STAMP_IMAGE_URL+resultp.get(StampsActivity.TAG_STAMP_IMAGE), stampImage);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
stampImage.startAnimation(myFadeInAnimation);
return itemView;
}
}
我想将JSONArray的“collectedStamp”和“unCollectedStamp”合并到同一个列表中
这是更新的代码感谢Hariharan先生
当我试图调用分配给'collectedStamp'
JSONArray的'arraylistCollected'
时,我得到了这个错误
No value for collectedStamp
当我成功地从“unCollectedStamp”JSONArray获取数据时
但是如果我将'collectedStamp'
的for循环代码移到unCollectedStamp
之前,我会得到一个错误
No value for unCollectedStamp
试试这个
您对这两个for循环使用相同的jsonobject
。你应该分开给
for (int i = 0; i < jsonArrayUncollected.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsject = jsonArrayUncollected.getJSONObject(i);
// Retrive JSON Objects
map.put(TAG_STAMPS_ID, jsject .getString(TAG_STAMPS_ID));
map.put(TAG_STAMP_IMAGE, jsject .getString(TAG_STAMP_IMAGE));
map.put(TAG_STAMP_NAME, jsject .getString(TAG_STAMP_NAME));
map.put(TAG_MESSAGE, jsject .getString(TAG_MESSAGE));
// Set the JSON Objects into the array
arraylistUncollected.add(map);
}
// Locate COLLECTED STAMPS array name in JSON
jsonArrayCollected = jsonobject.getJSONArray(TAG_COLLECTED_STAMPS);
for (int i = 0; i < jsonArrayCollected.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsject= jsonArrayCollected.getJSONObject(i);
// Retrive JSON Objects
map.put(TAG_STAMPS_ID, jsject.getString(TAG_STAMPS_ID));
map.put(TAG_STAMP_IMAGE, jsject.getString(TAG_STAMP_IMAGE));
map.put(TAG_STAMP_NAME, jsject.getString(TAG_STAMP_NAME));
map.put(TAG_MESSAGE, jsject.getString(TAG_MESSAGE));
// Set the JSON Objects into the array
arraylistCollected.add(map);
}
for(int i=0;i
编辑
HashMap<String,ArrayList<HashMap<String, String>>> result = new HashMap<String,ArrayList<HashMap<String, String>>>();
result.put("Uncollected",arraylistUncollected);
result.put("Collected",arraylistCollected);
HashMap result=newhashmap();
结果。put(“未收集”,已收集);
结果。放置(“已收集”,arraylistCollected);
谢谢你,它很管用。现在我不想把两个数组都保存到一个列表中,我该怎么做呢?适配器需要一个arrayList,目前我只能传递一个我有“JSONArrayOnCollected”或“jsonArrayCollected”的arrayList。有没有办法将两个ArrayList组合成一个数组list@ahmadssb或者只使用一个ArrayList在一个数组中添加所有元素不要使用两个。最后一件事(对不起),如果我想通过“id”(在我的帖子中检查JSON结果)使用“result”数组列表,我想根据其属性对图像进行排序ID@ahmadssb告诉我,朋友。
No value for unCollectedStamp
for (int i = 0; i < jsonArrayUncollected.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsject = jsonArrayUncollected.getJSONObject(i);
// Retrive JSON Objects
map.put(TAG_STAMPS_ID, jsject .getString(TAG_STAMPS_ID));
map.put(TAG_STAMP_IMAGE, jsject .getString(TAG_STAMP_IMAGE));
map.put(TAG_STAMP_NAME, jsject .getString(TAG_STAMP_NAME));
map.put(TAG_MESSAGE, jsject .getString(TAG_MESSAGE));
// Set the JSON Objects into the array
arraylistUncollected.add(map);
}
// Locate COLLECTED STAMPS array name in JSON
jsonArrayCollected = jsonobject.getJSONArray(TAG_COLLECTED_STAMPS);
for (int i = 0; i < jsonArrayCollected.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsject= jsonArrayCollected.getJSONObject(i);
// Retrive JSON Objects
map.put(TAG_STAMPS_ID, jsject.getString(TAG_STAMPS_ID));
map.put(TAG_STAMP_IMAGE, jsject.getString(TAG_STAMP_IMAGE));
map.put(TAG_STAMP_NAME, jsject.getString(TAG_STAMP_NAME));
map.put(TAG_MESSAGE, jsject.getString(TAG_MESSAGE));
// Set the JSON Objects into the array
arraylistCollected.add(map);
}
HashMap<String,ArrayList<HashMap<String, String>>> result = new HashMap<String,ArrayList<HashMap<String, String>>>();
result.put("Uncollected",arraylistUncollected);
result.put("Collected",arraylistCollected);