如何获取选项卡式活动中的数据列表?

时间:2013-01-15 08:52:30

标签: android android-intent android-emulator android-listview android-facebook

我已经创建了一个程序,我用他们的名字,dob和个人资料图片获取我所有Facebook好友的列表,但我决定在我现有的程序中使用制表符,因此,我也为此编写了代码,但是无论什么时候我运行我的应用程序,每次我获得标签但没有得到朋友列表..

在我的情况下,我没有在TabSample类中获取FriendsList活动数据,为什么?

SplashScreen.java: -

   private final static int FACEBOOK_AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
   private final static int FRIENDS_LIST_ACTIVITY = 1;
   public void onRequestReceived(int result, String message) {
    Log.d(LOG_TAG, "onRequestReceived(" + result + ")");
    if(isFinishing())
        return;
    switch (result) {
    case FacebookRequest.COMPLETED:
        Intent intent = new Intent(this, com.chr.tatu.sample.friendslist.TabSample.class);
        intent.putExtra("FRIENDS", message);
        while (System.currentTimeMillis() - startTime < DELAY) {
            try { Thread.sleep(50); } catch (Exception e) {}
        }
        startActivityForResult(intent, FRIENDS_LIST_ACTIVITY);
        isLoadingFriends = false;
        break;
    case FacebookRequest.IO_EXCEPTION:
    case FacebookRequest.FILE_NOT_FOUND_EXCEPTION:
    case FacebookRequest.MALFORMED_URL_EXCEPTION:
    case FacebookRequest.FACEBOOK_ERROR:
        FacebookUtility.displayMessageBox(this, this.getString(R.string.friends_error));
        onButton();
        break;
    }
}

FriendsList.java: -

     public class FriendsList extends Activity  implements FacebookRequest, OnItemClickListener {
private static String LOG_TAG = "FriendsList";
private static JSONArray jsonArray;
private static ListView friendsList;
private Handler mHandler;
private Button saveGreeting;
public void onCreate(Bundle savedInstanceState) {
    mHandler = new Handler();
    super.onCreate(savedInstanceState);     
    try {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getActionBar().hide();
    } catch (Exception e) {}
    setContentView(R.layout.friends_list_screen);
    try {
        setProgressBarIndeterminateVisibility(false);

    } catch (Exception e) {}

    init();

    saveGreeting = (Button) findViewById(R.id.greeting);
    saveGreeting.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        customGreeting(v);
        }
    });
}

public void customGreeting(View v) {
    Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
    myWebLink.setData(Uri.parse("http://google.com/"));
    startActivity(myWebLink);
}

@Override
public void onDestroy() {
    super.onDestroy(); 
    exit();
}

private void exit() {
    if (friendsList != null)
        friendsList.setAdapter(null);
    friendsList = null;
    jsonArray = null;
    GetProfilePictures.clear();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
    exit();
}

private void init() {

        friendsList = (ListView) findViewById(R.id.friends_list);
        friendsList.setOnItemClickListener(this);
        friendsList.setAdapter(new FriendListAdapter(this));
        friendsList.setTextFilterEnabled(true);

        friendsList.setDivider(null); 
        friendsList.setDividerHeight(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.d(LOG_TAG, "onCreateOptionsMenu()"); 
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}
/**
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(LOG_TAG, "onOptionsItemSelected()");
    switch (item.getItemId()) {
        case R.id.menu_refresh:
            FacebookUtility.logout(this);
            setProgressBarInDVisibility(true);
            return true;
        case R.id.retry_button:
        GetProfilePictures.clear();
        init();
        return true;                
        default:
            return super.onOptionsItemSelected(item); 

    }
}
**/

public void onRequestReceived(int result, String message) {
    Log.d(LOG_TAG, "onRequestReceived(" + result + ")");
    if(isFinishing())
        return;
  //    setProgressBarInDVisibility(false);
    switch (result) {
    case FacebookRequest.COMPLETED:
        FacebookUtility.clearSession(this);
        finish();
        break;
    case FacebookRequest.IO_EXCEPTION:
    case FacebookRequest.FILE_NOT_FOUND_EXCEPTION:
    case FacebookRequest.MALFORMED_URL_EXCEPTION:
    case FacebookRequest.FACEBOOK_ERROR:
        FacebookUtility.displayMessageBox(this, this.getString(R.string.logout_failed));
        break;
    }
}

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//  Log.d(LOG_TAG, "onItemClick()");

     try {
            final long friendId;
            friendId = jsonArray.getJSONObject(position).getLong("uid");
            String name = jsonArray.getJSONObject(position).getString("name");
            new AlertDialog.Builder(this).setTitle(R.string.post_on_wall_title)
                    .setMessage(String.format(getString(R.string.post_on_wall), name))
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Bundle params = new Bundle();

                            params.putString("to", String.valueOf(friendId));
                            params.putString("caption", getString(R.string.app_name));
                            params.putString("description", getString(R.string.app_desc));
                            params.putString("picture", FacebookUtility.HACK_ICON_URL);
                            params.putString("name", getString(R.string.app_action));
                            FacebookUtility.facebook.dialog(FriendsList.this, "feed", params,
                                    (DialogListener) new PostDialogListener());
                        }

                    }).setNegativeButton(R.string.no, null).show();
        } catch (JSONException e) {
            showToast("Error: " + e.getMessage());
        }
    }

    public class PostDialogListener extends BaseDialogListener {
        @Override
        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                showToast("Message posted on the wall.");
            } else {
                showToast("No message posted on the wall.");
            }
        }
    }

    public void showToast(final String msg) {

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast toast = Toast.makeText(FriendsList.this, msg, Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }


public class FriendListAdapter extends BaseAdapter implements SectionIndexer {
    private LayoutInflater mInflater;

    private GetProfilePictures picturesGatherer = null;
    FriendsList friendsList;
    private String[] sections;

    Hashtable<Integer, FriendItem> listofshit = null;

    public FriendListAdapter(FriendsList friendsList) {
        Log.d(LOG_TAG, "FriendListAdapter()");
        this.friendsList = friendsList;
        sections = new String[getCount()];
        listofshit = new Hashtable<Integer, FriendItem>();
        for (int i = 0; i < getCount(); i++) {
            try {

                sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0);
                sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(1);

                } catch (JSONException e) {
                sections[i] = "";
                Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            }
        }
        if (picturesGatherer == null) {
            picturesGatherer = new GetProfilePictures();
        }
        picturesGatherer.setAdapterForListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());
    }

    public int getCount() {
        Log.d(LOG_TAG, "getCount()");
        if (jsonArray == null)
            return 0;
        return jsonArray.length();
    }

    public Object getItem(int position) {
        Log.d(LOG_TAG, "getItem()");
        return listofshit.get(position);
    }

    public long getItemId(int position) {
        Log.d(LOG_TAG, "getItemId()");
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(LOG_TAG, "getView(" + position + ")");

        JSONObject jsonObject = null;
        try {
            jsonObject = jsonArray.getJSONObject(position);
        } catch (JSONException e) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
        }

        FriendItem friendItem;

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

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

        friendItem.friendPicture = (ImageView) convertView.findViewById(R.id.picture_square);
        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) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

        try {
            friendItem.friendName.setText(jsonObject.getString("name"));
            friendItem.friendDob.setText(jsonObject.getString("birthday"));
        } catch (JSONException e) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

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

    public int getPositionForSection(int position) {
        return position;
    }

    public int getSectionForPosition(int position) {
        return position;
    }

    public Object[] getSections() {
        return sections;
    }
}

class FriendItem {
    TextView friendDob;
    int id;
    ImageView friendPicture;
    TextView friendName;
    RelativeLayout friendLayout;

}
}

TabSample.java: -

   public class TabSample extends TabActivity {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabmain);
    setTabs() ;

}
private void setTabs()
{
    addTab("All", R.drawable.tab_menu, FriendsList.class);
    addTab("Current Month", R.drawable.tab_offers, FriendsList.class);

    addTab("Current Week", R.drawable.tab_location, FriendsList.class);
    addTab("Today", R.drawable.tab_reservation, FriendsList.class);
}

private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
}
   }

请告诉我在哪里添加此代码:

Bundle extras = getIntent().getExtras();
        String response = extras.getString("FRIENDS");

        Log.d(LOG_TAG, "onCreate()" + response);

        try {
            jsonArray = new JSONArray(response);
        } catch (JSONException e) {
            FacebookUtility.displayMessageBox(this, this.getString(R.string.json_failed));
        }

以及任何其他代码需要从 FriendsList 类移至 TabSample

0 个答案:

没有答案