我有一个viewpager/滑动标签。我希望能够从手机上已经存在的通讯录中获取 4 个联系人并将它们保存在应用程序中以备后用。我有代码来做一个活动的联系人选择器,但我的联系人页面是一个片段,我还没有让它工作。
任何想法或建议将不胜感激!
它连接到的主要活动的代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2Activity extends ActionBarActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
mViewPager.setOnPageChangeListener(this);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab = actionBar.newTab().setText("Contacts").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("GPS").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Message").setTabListener(this);
actionBar.addTab(tab);
}
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new MainMenu_Contacts();
case 1:
return new MainMenu_GPS();
case 2:
return new MainMenu_Message();
default:
return null;
}
}
public int getCount() {
return 3;
}
}
//implements on pager selected
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
getSupportActionBar().setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
//implements tab listener
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
主要活动的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
联系人片段代码:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainMenu_Contacts extends Fragment implements OnClickListener {
private Button mBtnContacts;
private final int PICK = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu__contacts);
mBtnContacts = (Button) findViewById(R.id.xBtnContacts);
mBtnContacts.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Opening Contacts Window as a Window
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// calling OnActivityResult with intenet And Some conatct for Identifie
startActivityForResult(intent, PICK);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndexOrThrow(Contacts.People.NAME));
// TODO Whatever you want to do with the selected contact
// name.
}
}
break;
}
}
联系人 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/xBtnContacts"
android:text="Pick Contacts" />
</LinearLayout>