在同一个ListView / GridView中调用多个JSON数组

时间:2014-04-13 13:24:19

标签: android json gridview android-listview android-json

更新:感谢Hariharan先生,我现在可以将两个JSON数组合并到一个ListView中(我已将其更新为我的代码)

现在我需要将未收集的标记更改为黑白颜色我有这个代码可以改变黑白颜色但是如果我实现它会将所有标记图像更改为黑/白色

我该怎么办才能将B / W仅用于UncollectedStamps:

这是改变图像颜色的功能:

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;
    }
}

这个JSON结果

{
    "status": "1",
    "tappedStamp": "15",
    "message": "stamp message",
    "stampimage": "stamp3.png",
    "stampname": "stamp3",
    "collectedStamp": [
        {
            "id": "14",
            "stampname": "stamp2 ",
            "message": "stamp2 message",
            "stampimage": "stamp2.png"
        },
        {
            "id": "15",
            "stampname": "stamp3",
            "message": "stamp message",
            "stampimage": "stamp3.png"
        }
    ],
    "unCollectedStamp": [
        {
            "id": "12",
            "stampname": "Testing MKH Stamp",
            "message": "Testing MKH Stamp",
            "stampimage": "award.png"
        },
        {
            "id": "13",
            "stampname": "stamp1",
            "message": "stamp1 Message",
            "stampimage": "stamp1.png"
        },
        {
            "id": "16",
            "stampname": "Testing MKH Stamp",
            "message": "Testing MKH Stamp",
            "stampimage": "award.png"
        }
    ]
}

我想将JSONArrays的gatherStamp'和'unCollectedStamp'组合在同一个List

这是更新的代码 感谢Hariharan先生

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylistCollected = new ArrayList<HashMap<String, String>>();
        arraylistUncollected = new ArrayList<HashMap<String, String>>();



        this.sharedPref = getSharedPreferences(getString(R.string.key_storage),0);
        this.eventID = sharedPref.getString("eventid", null);
        this.kioskid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        this.tagid = sharedPref.getString("tagID", null);
        this.accesstoken = sharedPref.getString("accesstoken", null);


        try {
            // building parameters for Logout
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair(TAG_ACCESSTOKEN, accesstoken));
            param.add(new BasicNameValuePair(TAG_KIOSK_ID, kioskid));
            Log.d("request!", "Starting");


        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONParser.makeHttpRequest(STAMPS_LIST_URL,"POST",param);
        Log.d("Loding Stamps Attemp", jsonobject.toString());

        // get tapped Stamp Image Url 
        tappedStampImageUrl = jsonobject.getString(TAG_STAMP_IMAGE);

        // Locate UNCOLLECTED STAMPS array name in JSON
        jsonArrayUncollected = jsonobject.getJSONArray(TAG_UNCOLLECTED_STAMPS);

        for (int i = 0; i < jsonArrayUncollected.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject jsonobject = jsonArrayUncollected.getJSONObject(i);

            // Retrive JSON Objects
            map.put(TAG_STAMPS_ID, jsonobject.getString(TAG_STAMPS_ID));
            map.put(TAG_STAMP_IMAGE, jsonobject.getString(TAG_STAMP_IMAGE));
            map.put(TAG_STAMP_NAME, jsonobject.getString(TAG_STAMP_NAME));
            map.put(TAG_MESSAGE, jsonobject.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 jsonobject = jsonArrayCollected.getJSONObject(i);

            // Retrive JSON Objects
            map.put(TAG_STAMPS_ID, jsonobject.getString(TAG_STAMPS_ID));
            map.put(TAG_STAMP_IMAGE, jsonobject.getString(TAG_STAMP_IMAGE));
            map.put(TAG_STAMP_NAME, jsonobject.getString(TAG_STAMP_NAME));
            map.put(TAG_MESSAGE, jsonobject.getString(TAG_MESSAGE));

            // Set the JSON Objects into the array
            arraylistCollected.add(map);
        }


        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

@Override
        protected void onPostExecute(Void args) {

            imageLoader.DisplayImage(TAG_IMAGE_URL+tappedStampImageUrl, tappedStampImage);
            Animation myFadeInAnimation = AnimationUtils.loadAnimation(StampsActivity.this, R.anim.fadein);
            tappedStampImage.startAnimation(myFadeInAnimation);
            // Locate the listview in listview_main.xml
            //listview = (ListView) findViewById(R.id.listview);
            gridView = (GridView) findViewById(R.id.gridView1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(StampsActivity.this, arraylistUncollected);
            // Set the adapter to the ListView
            //listview.setAdapter(adapter);
            gridView.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

在这部分

adapter = new ListViewAdapter(StampsActivity.this, arraylistCollected);

当我尝试拨打分配给'arraylistCollected' JSONArray的'collectedStamp'时,我收到此错误

No value for collectedStamp 

当我从'unCollectedStamp'JSONArray成功获取数据时

但如果我在'collectedStamp'之前移动unCollectedStamp的for循环代码,我会收到错误消息

No value for unCollectedStamp

1 个答案:

答案 0 :(得分:1)

试试这个..

你对两个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);
    }

修改

HashMap<String,ArrayList<HashMap<String, String>>> result = new  HashMap<String,ArrayList<HashMap<String, String>>>();
result.put("Uncollected",arraylistUncollected);
result.put("Collected",arraylistCollected);