0

Please help, I am trying to retrieve a list of friends from FaceBook that also use an app. I am getting the list and the Data is showing up, but my adapter does not inflate the list view. All the id's are correct, and Log tags show me what I need, but the view will not inflate. Here is the relevant code:

View Adapter:

package com.example.sca.ihavebeen;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tom Schinler on 9/10/2015.
 */
public class FriendsViewAdapter extends ArrayAdapter<FaceBookFriends> {
    public FriendsViewAdapter(Context context, JSONArray friends) {
        super(context,0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        FaceBookFriends friend = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.fb_friends_layout, parent);
        }
        TextView fbName = (TextView)convertView.findViewById(R.id.fbName);
        ImageView fbPic = (ImageView)convertView.findViewById(R.id.fbPic); // Not yet ready to handle this
        fbName.setText(friend.name);


        return convertView;
    }
}

Here is the view the list should inflate on:

package com.example.sca.ihavebeen;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GameStart extends AppCompatActivity {

    JSONArray mArrayofFriends;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);



        mArrayofFriends = FaceBookFriends.getFriendsList();


        Log.v("maybe ", String.valueOf(mArrayofFriends));

        populateFbFriendsList();

    }

    private void populateFbFriendsList() {

        FriendsViewAdapter adapter = new FriendsViewAdapter(this, mArrayofFriends);

        ListView listView = (ListView) findViewById(R.id.fbFriendsListView);
        listView.setAdapter(adapter);
    }




}

and just for kicks, here is the class calling the Graph Response:

package com.example.sca.ihavebeen;

import android.util.Log;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;

import org.json.JSONArray;

/**
 * Created by Tom Schinler on 8/4/2015.
 */
public class FaceBookFriends {




    public static JSONArray mFriendsList;
    public String name;
    public String id;




    public static JSONArray getFaceBookFriends() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        GraphRequest request = GraphRequest.newMyFriendsRequest(
                accessToken,
                new GraphRequest.GraphJSONArrayCallback() {
                    @Override
                    public void onCompleted(JSONArray array, GraphResponse response) {
                        // Insert your code here

                        mFriendsList = array;
                        Log.v("Array: ", String.valueOf(mFriendsList));
                        Log.v("Facebook response: ", String.valueOf(response));

                    }
                });

        request.executeAsync();




        return mFriendsList;

    }

    public static JSONArray getFriendsList() {
        return mFriendsList;
    }

    public FaceBookFriends(String name, String id){
        this.name = name;
        this.id = id;
    }

}

Please help me figure out this issue!

T.Schinler
  • 25
  • 6

2 Answers2

0

You might need to override getCount in order for your list to know how many objects are actually in the adapter. Otherwise it won't lay anything out.

Chris Thoma
  • 499
  • 2
  • 8
0

You're using an ArrayAdapter<FaceBookFriends> without setting an array:

public FriendsViewAdapter(Context context, JSONArray friends) {
    super(context,0);
}

Instead you should Convert Json Array to normal Java Array or more precise List<FaceBookFriends> and set it inside the constructor:

public FriendsViewAdapter(Context context, JSONArray friends) {
    super(context,0,convertJSONArray2List(friends));
}
Community
  • 1
  • 1
tynn
  • 38,113
  • 8
  • 108
  • 143
  • I appreciate this so much, I set the conversion method inside the constructor, like you showed, then created the method just like the one you linked to, but now i get a null pointer exception at `listView.setAdapter(adapter);` and it appears that the adapter is not getting the array. a Log.v show that the conversion is happening, and displaying the data in logcat, but not being converted. I know what I am missing is going to be silly and obvious, but I am struggling. – T.Schinler Sep 14 '15 at 15:15
  • I figured it out. Thank you again so much for your help! – T.Schinler Sep 14 '15 at 15:50
  • And now I'm stuck. Inside the array list, as an array with 2 key value pairs ie: [{"id":"10207092696420407","name":"Gina Schinler"}], I need to access those key value pairs and insert them into my views. When I try this, the view inflates with both key value pairs as if it were all one string. – T.Schinler Sep 14 '15 at 16:49
  • So, I switched from converting the JSONArray to a Java Array and instead converted it to a hasmap, and all is working now. Thanks again! – T.Schinler Sep 15 '15 at 14:41