0

Sorry for the confusing Title (there is no why to explain my problem in few words)

This is my situation:

I'm developing an app to collect Stamps I have JSON that contains 2 JSONArray for "collectedStamp" and "unCollectedStamp" those 2 JSONArrays stored into 2 ArrayLists separately "arraylistCollected" and "arraylistUncollected"

and also I have 1 GridView to to display all Stamps that Collected and Uncollected

so to put those 2 ArrayLists into ListViewAdapter I had to Combine both "arraylistCollected" and "arraylistUncollected" into 1 Arraylist named "arraylistCombined"

Now All Stamps displays Correctly in 1 GridView BUT I want to Detect the Collected Stamps and Uncollected Stamps so the user know witch stamps has been collected and which not. for Example make collected stamps Colourful and UnCollected stamps Black and White , Or

I don't know how to detect the Elements of these 2 arrays "arraylistCollected" and "arraylistUncollected" so later I can do what ever with any of their images.

What is your Opinion or Idea to solve this issue ?

This JSON resul

{
    "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"
        }
    ]
}

This is Code to Download JSON

        @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);
            }
Log.d("jsonArrayUncollected Size:", ""+arraylistUncollected.size());


            // 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);
            }
            Log.d("jsonArrayCollected Size:", ""+arraylistCollected.size());


                arraylistCombined = new ArrayList<HashMap<String, String>>();
                arraylistCombined.addAll(arraylistCollected);
                arraylistCombined.addAll(arraylistUncollected);

                Log.d("arraylistCombined Size:", ""+arraylistCombined.size());


            } 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, arraylistCombined);

                // Set the adapter to the ListView
                //listview.setAdapter(adapter);
                gridView.setAdapter(adapter);
                // Close the progressdialog
                mProgressDialog.dismiss();
            }
        }

This is 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;
    }
}
ahmadssb
  • 95
  • 2
  • 13

1 Answers1

0

while putting the values in the hash map add one more value in this way :

map.put(TAG_TYPE, "COLLECTED");

for uncollected :

map.put(TAG_TYPE, "UNCOLLECTED");

then you can easily differentiate it

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Jaspreet Chhabra
  • 1,431
  • 15
  • 23
  • OK I understand what you did here But where should I write the if condition inside ListView? I need to detect this line on both collected and not uncollected ----- map.put(TAG_STAMP_IMAGE, jsonobject.getString(TAG_STAMP_IMAGE)); – ahmadssb Apr 15 '14 at 08:08
  • and inside ListViewAdapter the code that handle the stamp image is the 4th line from Buttom ---- imageLoader.DisplayImage(URL, ImageView) – ahmadssb Apr 15 '14 at 08:12
  • In base adapter you can add the code resultp.get(StampsActivity.TAG_TYPE) – Jaspreet Chhabra Apr 15 '14 at 08:15