0

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

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

我应该怎么做才能仅对 UncollectedStamps 实施 B/W:

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

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 'collectedStamp' 和 'unCollectedStamp'

这是更新的代码 感谢 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'分配给'collectedStamp'JSONArray 时,我收到此错误

No value for collectedStamp 

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

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

No value for unCollectedStamp
4

1 回答 1

1

尝试这个..

jsonobject您对两个 for 循环都使用相同的。你应该分开给。

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);
于 2014-04-13T13:27:11.060 回答