我有2个片段,InboxFragment和FriendsFragment,当我按下FriendsFragment并选择我的一个朋友时,我想开始一个新的Activity。但我无法让它发挥作用。
UserListActivity
公共类UserListActivity扩展了Activity {
public static final String user = null;
/** The Chat list. */
private ArrayList<ParseUser> uList;
/** The user. */
public static ParseUser Username;
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
getActionBar().setDisplayHomeAsUpEnabled(false);
updateUserStatus(true);
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onDestroy()
*/
@Override
protected void onDestroy()
{
super.onDestroy();
updateUserStatus(false);
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onResume()
*/
@Override
protected void onResume()
{
super.onResume();
loadUserList();
}
/**
* Update user status.
*
* @param online
* true if user is online
*/
private void updateUserStatus(boolean online)
{
user();
user();
}
private void user() {
// TODO Auto-generated method stub
}
/**
* Load list of users.
*/
private void loadUserList()
{
final ProgressDialog dia = ProgressDialog.show(this, null,
getString(R.string.alert_loading));
ParseUser.getQuery().whereNotEqualTo("username", Username.getUsername())
.findInBackground(new FindCallback<ParseUser>() {
@SuppressWarnings("unused")
public void done(List<ParseUser> li, ParseException e)
{
dia.dismiss();
if (li != null)
{
if (li.size() == 0)
Toast.makeText(UserListActivity.this,
R.string.msg_no_user_found,
Toast.LENGTH_SHORT).show();
uList = new ArrayList<ParseUser>(li);
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new UserAdapter());
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int pos, long arg3)
{
startActivity(new Intent(UserListActivity.this,
Chat.class).putExtra(
Const.EXTRA_DATA, uList.get(pos)
.getUsername()));
}
});
}
else
{
Utils.showDialog(
UserListActivity.this,
getString(R.string.err_users) + " "
+ e.getMessage());
e.printStackTrace();
}
}
@Override
public void done(List<ParseUser> arg0,
com.parse.ParseException arg1) {
// TODO Auto-generated method stub
}
});
}
/**
* The Class UserAdapter is the adapter class for User ListView. This
* adapter shows the user name and it's only online status for each item.
*/
private class UserAdapter extends BaseAdapter
{
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
public int getCount()
{
return uList.size();
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
public ParseUser getItem(int arg0)
{
return uList.get(arg0);
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int arg0)
{
return arg0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@SuppressLint("InflateParams") public View getView(int pos, View v, ViewGroup arg2)
{
if (v == null)
v = getLayoutInflater().inflate(R.layout.chat_item, null);
ParseUser c = getItem(pos);
TextView lbl = (TextView) v;
lbl.setText(c.getUsername());
lbl.setCompoundDrawablesWithIntrinsicBounds(
c.getBoolean("online") ? R.drawable.ic_online
: R.drawable.ic_offline, 0, R.drawable.arrow, 0);
return v;
}
}
}
SectionsPagerAdapter
公共类SectionsPagerAdapter扩展了FragmentPagerAdapter {
protected Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch(position) {
case 0:
return new InboxFragment();
case 1:
return new FriendsFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
public int getIcon(int position) {
switch (position) {
case 0:
return R.drawable.ic_tab_inbox;
case 1:
return R.drawable.ic_tab_friends;
}
return R.drawable.ic_tab_inbox;
}
}
答案 0 :(得分:2)
尝试以这种方式启动Activity:
Intent intent = new Intent(getActivity(), ActivityToStart.class);
intent.putExtra(Const.EXTRA_DATA, uList.get(pos).getUsername());
getActivity().startActivity(intent);