5

我想通过我的应用程序登录到 facebook 后显示朋友列表ListView。但是我的代码不起作用。我还使用了friendsArrayAdapter 之类的类。我使用了以下代码

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendlist_screen);
    facebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);

    lv = (ListView) findViewById(R.id.friendsList);
    friendsArrayAdapter = new FriendsArrayAdapter(this, R.layout.rowlayout, friends);
    lv.setAdapter(friendsArrayAdapter);
}

FriendsRequestListener.class

public class FriendsRequestListener implements com.facebook.android.AsyncFacebookRunner.RequestListener {

    public void onComplete(final String response, Object state) {
        mSpinner.dismiss();
        try {
            // process the response here: executed in background thread
            Log.d("Facebook-Example-Friends Request", "response.length(): " + response.length());
            Log.d("Facebook-Example-Friends Request", "Response: " + response);

            final JSONObject json = new JSONObject(response);
            JSONArray d = json.getJSONArray("data");
            int l = (d != null ? d.length() : 0);
            Log.d("Facebook-Example-Friends Request", "d.length(): " + l);

            for (int i = 0; i < l; i++) {
                JSONObject o = d.getJSONObject(i);
                String n = o.getString("name");
                String id = o.getString("id");
                Friend f = new Friend();
                f.id = id;
                f.name = n;
                friends.add(f);
            }
            // Only the original owner thread can touch its views
            FrndActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    friendsArrayAdapter = new FriendsArrayAdapter(
                        FrndActivity.this, R.layout.rowlayout, friends);
                    lv.setAdapter(friendsArrayAdapter);
                    friendsArrayAdapter.notifyDataSetChanged();
                }
            });
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        }
    }

请建议如何在 ListView 中显示名称,以及我是否需要为这个东西创建任何其他类?

4

1 回答 1

5

MainActivity.java

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.util.ArrayList;

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

    import com.facebook.android.AsyncFacebookRunner;
    import com.facebook.android.AsyncFacebookRunner.RequestListener;
    import com.facebook.android.DialogError;
    import com.facebook.android.Facebook;
    import com.facebook.android.FacebookError;
    import com.facebook.android.Facebook.DialogListener;
    import com.facebook.android.Util;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

    @SuppressWarnings("deprecation")
    public class MainActivity extends Activity {

        // Your Facebook APP ID
        private static String APP_ID = "197574013719352"; // Replace your App ID here

        // Instance of Facebook Class
        public static Facebook facebook = null;
        public static AsyncFacebookRunner mAsyncRunner = null;
        String FILENAME = "AndroidSSO_data";
        private SharedPreferences mPrefs;
        String _error;

        Button button;

        ArrayList<String> friends_list;
        ListView lv;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

            setContentView(R.layout.main);

            facebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(facebook);   
            friends_list = new ArrayList<String>();
            button = (Button) findViewById(R.id.button);
            lv = (ListView) findViewById(R.id.list);

            button.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    loginToFacebook();
                }

            });

        }

        public void loginToFacebook() {
            mPrefs = getPreferences(MODE_PRIVATE);

            if (!facebook.isSessionValid()) {
                facebook.authorize(this, new String[] { "email", "publish_stream" },
                        new DialogListener() {

                            @Override
                            public void onCancel() {
                                // Function to handle cancel event
                            }

                            @Override
                            public void onComplete(Bundle values) {
                                // Function to handle complete event
                                // Edit Preferences and update facebook acess_token
                                SharedPreferences.Editor editor = mPrefs.edit();
                                editor.putString("access_token",
                                        facebook.getAccessToken());
                                editor.putLong("access_expires",
                                        facebook.getAccessExpires());
                                editor.commit();     

                                mAsyncRunner.request("me/friends", new FriendsRequestListener());

                                Toast.makeText(getApplicationContext(), "Success ", Toast.LENGTH_LONG).show();

                                button.setText("LogOut");

                            }

                            @Override
                            public void onError(DialogError error) {
                                // Function to handle error

                            }

                            @Override
                            public void onFacebookError(FacebookError fberror) {
                                // Function to handle Facebook errors

                            }

                        });
            }
        }

        private class FriendsRequestListener implements RequestListener {
            String friendData;

            //Method runs when request is complete
            public void onComplete(String response, Object state) {
                Log.v("", "FriendListRequestONComplete");
                //Create a copy of the response so i can be read in the run() method.
                friendData = response; 
                Log.v("friendData--", ""+friendData);
                //Create method to run on UI thread
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            //Parse JSON Data
                            JSONObject json;
                            json = Util.parseJson(friendData);

                            //Get the JSONArry from our response JSONObject
                            JSONArray friendArray = json.getJSONArray("data");

                            Log.v("friendArray--", ""+friendArray);

                            for(int i = 0; i< friendArray.length(); i++)
                            {
                                JSONObject frnd_obj = friendArray.getJSONObject(i);
                                friends_list.add(frnd_obj.getString("name")+"~~~"+frnd_obj.getString("id"));
                            }

  Intent ide = new Intent(MainActivity.this,Next.class);
                                ide.putStringArrayListExtra("friends", friends_list);
                                ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(ide);

                          //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1, friends_list);
                         //   lv.setAdapter(adapter);

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (FacebookError e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
            }

            @Override
            public void onIOException(IOException e, Object state) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                    Object state) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
                // TODO Auto-generated method stub

            }
            }

    }

Next.java

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class Next extends Activity{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            setContentView(R.layout.list);

            Intent i = getIntent();

            ArrayList<String> friends_data = i.getStringArrayListExtra("friends");

            ListView lv = (ListView) findViewById(R.id.list);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1, friends_data);
            lv.setAdapter(adapter);

        }

    }
于 2013-08-08T12:35:59.760 回答