2015-09-07 44 views
0

我对一个可能的小问题scotched。我想要获取ListFragment中联系人的电话号码列表。列表中的项目显示的数量很好,但不是内部的文本。在摆脱其他任何问题的时刻,我只在布局容器的主活动中加载片段,并使用先前已知的lookupKey启动加载器。 加载程序为电话号码(来自Log.d消息的信息)以及正确的号码返回正确的值。ListFragment ListItems出现,但不是他们的内容

这里是我的项目布局

<?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:行_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

在_CursorAdapter.bindView_中,您似乎没有将telNumber变量设置为TextView - 只从View.getTag()读取持有者引用。你是否也应该_TextView.setText(telNumber)_? – harism

回答

0

愚蠢的我!在bindView中,它应该是holder.telNumber.setText(cursor.getString(2))而不是最后的String telNumber = cursor.getString(2);

相关问题