請幫助,我試圖從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;
}
}
請幫我找出這個問題!
我明白這一點了這麼多,我在構造函數中設置轉換方法,就像你展示的那樣,然後創建方法就像你鏈接的方法一樣,但是現在我在'listView.setAdapter(adapter);'得到一個空指針異常,並且看起來該適配器不是得到一個rray。 Log.v顯示正在進行轉換,並將數據顯示在logcat中,但未被轉換。我知道我失去的將是愚蠢而明顯的,但我正在掙扎。 –
我想通了。再次感謝您的幫助! –
現在我卡住了。在數組列表中,作爲一個包含2個鍵值對的數組,例如:[{「id」:「10207092696420407」,「name」:「Gina Schinler」}],我需要訪問這些鍵值對並將它們插入到我的視圖中。當我嘗試這個時,視圖充滿了兩個鍵值對,就好像它們都是一個字符串。 –