我对一个可能是微不足道的问题感到沮丧。我想获取ListFragment中联系人的电话号码列表。列表中的项目显示的数字很好,但不是内部的文本。目前为了摆脱任何其他问题,我只在主要活动中加载片段在布局容器中,并使用先前已知的lookupKey启动加载器。 加载程序返回正确的电话号码值(来自Log.d消息的信息)以及正确的数字。
这是我的项目布局
<?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:background="#AAAAAA"
android:orientation="vertical">
<TextView
android:id="@+id/telNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000BB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
我的适配器
package com.zoraldia.android.zcontacts;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created on 07/09/15.
*/
public class PhoneAdapter extends CursorAdapter {
private LayoutInflater mInflater; // Stores the layout inflater
public PhoneAdapter(Context context) {
super(context, null, 0);
mInflater = LayoutInflater.from(context);
}
private class ViewHolder {
//TextView telType;
TextView telNumber;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View itemLayout =
mInflater.inflate(R.layout.tel_list_item, parent, false);
final ViewHolder holder = new ViewHolder();
// holder.telType = (TextView) itemLayout.findViewById(R.id.telType);
holder.telNumber = (TextView) itemLayout.findViewById(R.id.telNumber);
itemLayout.setTag(holder);
return itemLayout;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
// final String telType = cursor.getString(3);
final String telNumber = cursor.getString(2);
}
@Override
public Cursor swapCursor(Cursor newCursor) {
return super.swapCursor(newCursor);
}
/**
* An override of getCount that simplifies accessing the Cursor. If the Cursor is null,
* getCount returns zero. As a result, no test for Cursor == null is needed.
*/
@Override
public int getCount() {
if (getCursor() == null) {
return 0;
}
return super.getCount();
}
}
我的ListFragment
package com.zoraldia.android.zcontacts;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
/**
* Created on 07/09/15.
*/
public class PhoneListViewFragment extends ListFragment implements
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
// Defines a tag for identifying log entries
private static final String TAG = "PhoneListFragment";
private static final String msg = "PhoneListFragment";
private String[] mSelectionArguments = {"0r7-555555"};
private PhoneAdapter phoneAdapter;
public PhoneListViewFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
phoneAdapter = new PhoneAdapter(getActivity());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(phoneAdapter);
getLoaderManager().initLoader(ContactsQueryTel.QUERY_ID, null, this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri contentUri = ContactsQueryTel.CONTENT_URI;
String[] selectionArgs = mSelectionArguments;
return new CursorLoader(getActivity(),
contentUri,
ContactsQueryTel.PROJECTION,
ContactsQueryTel.SELECTION,
selectionArgs,
ContactsQueryTel.SORT_ORDER
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
//The following is just for log
String type;
String number;
if (data.moveToFirst()) {
do {
// type = Integer.toString(data.getInt(3));
number = data.getString(2);
Log.d(msg, " Row _data 2: " + data.getString(1) + " number=" + data.getString(2));
} while (data.moveToNext());
}
phoneAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
phoneAdapter.swapCursor(null);
}
public interface ContactsQueryTel {
int QUERY_ID = 1;
Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI;
String[] PROJECTION = {
ContactsContract.Data._ID,
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.DATA1,//Number, String int
};
// Defines the selection clause
String SELECTION = ContactsContract.Data.LOOKUP_KEY +
" = ? AND " + ContactsContract.Data.MIMETYPE + " = " +
"'" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
// Defines the array to hold the search criteria
String[] mSelectionArgs = {"0r1-3B454D313345514B4339314B"};
String SORT_ORDER = ContactsContract.Data.MIMETYPE;
}
}
并最终返回日志
09-07 15:47:51.660 29989-29989 / com.example.android.fragments D / PhoneListFragment:Row _data 2:vnd.android.cursor.item / phone_v2 number = 080-8080 09-07 15:47:51.660 29989-29989 / com.example.android.fragments D / PhoneListFragment:Row _data 2:vnd.android.cursor.item / phone_v2 number = 999-070
希望有人可以提供帮助。
答案 0 :(得分:0)
我的愚蠢!在bindView中,它应该是 holder.telNumber.setText(cursor.getString(2))而不是final String telNumber = cursor.getString(2);