我已经使用ViewPager创建了一个TabLayout,我希望我的标签片段中的TextViews显示当前联系人的名字和姓氏。
到目前为止,这是我的代码:
在活动中:
Bundle bundle = new Bundle();
bundle.putParcelable("currentContactUri", mCurrentContactUri);
ContactDetailFragment1 fragment1 = new ContactDetailFragment1();
fragment1.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.pager, fragment1).commit();
在我的片段中:
public final class ContactDetailFragment1 extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int CONTACT_LOADER = 0;
private Uri mCurrentContactUri;
private TextView mFirstNameTextView;
private TextView mLastNameTextView;
private long mCurrentContactId;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_contact_detail_fragment1, container, false);
mFirstNameTextView = rootView.findViewById(R.id.contact_detail_firstname);
mLastNameTextView = rootView.findViewById(R.id.contact_detail_lastname);
if (getArguments() != null) {
mCurrentContactUri = getArguments().getParcelable("currentContactUri");
mCurrentContactId = ContentUris.parseId(mCurrentContactUri);
}
getLoaderManager().initLoader(CONTACT_LOADER, null, this);
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
ContactContract.ContactEntry._ID,
ContactContract.ContactEntry.COLUMN_FIRSTNAME,
ContactContract.ContactEntry.COLUMN_LASTNAME};
String selection = ContactContract.ContactEntry._ID + " = ?";
String[] selectionArgs = new String[]{String.valueOf(mCurrentContactId)};
return new CursorLoader(getActivity().getApplicationContext(),
ContactContract.ContactEntry.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor == null || cursor.getCount() < 1) {
return;
}
if (cursor.moveToFirst()) {
int firstnameColumnIndex = cursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_FIRSTNAME);
int lastnameColumnIndex = cursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_LASTNAME);
String firstname = cursor.getString(firstnameColumnIndex);
String lastname = cursor.getString(lastnameColumnIndex);
mFirstNameTextView.setText(firstname);
mLastNameTextView.setText(lastname);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mFirstNameTextView.setText("");
mLastNameTextView.setText("");
}
}
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/first_name">
</TextView>
<TextView
android:id="@+id/contact_detail_firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="firstname goes here">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/last_name">
</TextView>
<TextView
android:id="@+id/contact_detail_lastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="lastname goes here">
</TextView>
</LinearLayout>
我没有收到错误或任何内容,但我在xml布局中为TextViews设置的示例文本没有改变,甚至是 firstname 和 lastname 包含正确的值。