How to get List of Data in Tabbed Actvity?

拜拜、爱过 提交于 2019-12-23 15:29:07

问题


I have made a program in which i am fetching list of all my Facebook Friends with their name, dob and profile picture, but i have decided to use tabs in my existing program therefore, i have also written code for that, but whenever i run my app, every time i am getting tabs but not getting list of friends..

In my case i am not getting FriendsList Activity data in TabSample Class, why?

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);
}
   }

Please tell me where i have to add this code :

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));
        }

and any other code need to move from FriendsList class to TabSample Class

来源:https://stackoverflow.com/questions/14334068/how-to-get-list-of-data-in-tabbed-actvity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!