请帮助,我正在尝试从FaceBook中检索也使用应用程序的朋友列表。我收到列表并显示数据,但我的适配器不会使列表视图膨胀。所有id都是正确的,Log标签显示我需要的内容,但视图不会膨胀。以下是相关代码:
查看适配器:
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;
}
}
这是列表应该膨胀的视图:
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);
}
}
只是为了踢,这里是调用图响应的类:
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;
}
}
请帮我解决这个问题!
答案 0 :(得分:0)
您可能需要覆盖getCount,以便列表知道适配器中实际有多少个对象。否则就不会有任何问题。
答案 1 :(得分:0)
您正在使用ArrayAdapter<FaceBookFriends>
而未设置数组:
public FriendsViewAdapter(Context context, JSONArray friends) {
super(context,0);
}
相反,您应该Convert Json Array to normal Java Array或更精确List<FaceBookFriends>
并将其设置在构造函数中:
public FriendsViewAdapter(Context context, JSONArray friends) {
super(context,0,convertJSONArray2List(friends));
}