Question

I am writing an app in which i am fetching List of Facebook friends with their profile picture, name and dob

but some of my friends have not given their Birthday Dates, so in place of Birthday i am getting null by default, but now in place of null i want to show: Birthday Not Mentioned.

see below screen shot of my existing app:

Like: Vikas Rana has given DOB on facebook and AJay November has not given DOB on Facebook

      > VikY
        January 1

      > AJamber
        Null

Here, in place Null i want to show : Birthday Not Mentioned

FriendsList.java:

       public View getView(int position, View convertView, ViewGroup parent) {


        JSONObject jsonObject = null;
        try {
            jsonObject = jsonArray.getJSONObject(position);
        } catch (JSONException e) {

        }

        FriendItem friendItem;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listfb_friends, null);
            friendItem = new FriendItem();

            convertView.setTag(friendItem);
        } else {
            friendItem = (FriendItem) convertView.getTag();
        }

        friendItem.friendPicture = (ImageView) convertView
                .findViewById(R.id.picture);
        friendItem.friendName = (TextView) convertView
                .findViewById(R.id.name);
        friendItem.friendDob = (TextView) convertView
                .findViewById(R.id.dob);
        friendItem.friendLayout = (RelativeLayout) convertView
                .findViewById(R.id.friend_item);

        try {
            String uid = jsonObject.getString("uid");
            String url = jsonObject.getString("pic_square");
            friendItem.friendPicture.setImageBitmap(picturesGatherer
                    .getPicture(uid, url));
        } catch (JSONException e) {

            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

        try {
            friendItem.friendName.setText(jsonObject.getString("name"));
            if(!friendItem.friendDob.equals(""))
            {
            friendItem.friendDob.setText(jsonObject.getString("birthday"));
            }
            else 
            {
            friendItem.friendDob.setText("Birthday Not Mentioned");
            }
        } catch (JSONException e) {

            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

        listofshit.put(position, friendItem);
        return convertView;
    }

and if i am writing code something like this:

 try {
            friendItem.friendName.setText(jsonObject.getString("name"));
            friendItem.friendDob.setText(jsonObject.getString("birthday"));
        } catch (JSONException e) {

            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }
        finally
        {
            friendItem.friendDob.setText("Birthday Not Mentioned");
        }

so i am getting Birthday Not Mentioned for all facebook friends Birthdays, even for them also those have mentioned.

Was it helpful?

Solution

try this one.

    try{
    if(jsonObject.getString("birthday")!= null && !jsonObject.getString("birthday").equals("")){
          friendItem.friendDob.setText(jsonObject.getString("birthday"));
    }else{
         friendItem.friendDob.setText("Birthday Not Mentioned");
    }
}catch(JSONException e){
    e.printStackTrace();
}

OTHER TIPS

Where you are getting NullPointerException use try-catch over it.

You might have done it I think. Now just set the text "Birthday Not Mentioned" over the textview/edittext (whatever you are using to show d.o.b) by writing code in catch block.


Just use:-

try {
       friendItem.friendName.setText(jsonObject.getString("name"));
       friendItem.friendDob.setText(jsonObject.getString("birthday"));
    } 
catch (Exception e) {    
       friendItem.friendName.setText("Name");  // try it for now.
       friendItem.friendDob.setText("Birthday Not Mentioned"); // Have to be here.
    }

Just try with this code:

            try 
        {
            friendItem.friendName.setText(jsonObject.getString("name"));
            friendItem.friendDob.setText(jsonObject.getString("birthday"));

            if(!(jsonObject.getString("birthday").equalsIgnoreCase("null")))
            {
            friendItem.friendDob.setText(jsonObject.getString("birthday"));
            }
            else
            {
            friendItem.friendDob.setText("Birthday Not Mentioned");
            }

        } catch (JSONException e) 
        {
            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }
        listofshit.put(position, friendItem);
        return convertView;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top