2014-04-23 139 views
0

我米使用具有ListFragment一个SimpleCursorAdapter,但所显示的ListView是空的(项目,而不数据):SimpleCursorAdapter:空的ListView

public class MyFragment extends ListFragment { 

...

private SimpleCursorAdapter dataAdapter; 
    // The desired columns to be bound 
    String[] columns = new String[]{"villename"}; 
    int to [] = {R.id.tv_ville_secteur}; 

...

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_recherhcer, container, false); 
     // LOADING CITYS AND SECTEURS 
     DatabaseHelper db = new DatabaseHelper(getActivity().getApplicationContext()); 

//  MatrixCursor matrixCursor= new MatrixCursor(columns); 
//  
     Ville[] villes = new Ville[] {new Ville("0","Rabat"),new Ville("1","Casa")}; 
//  matrixCursor.addRow(villes); 
//  
     db.addVille(villes[0]); 
     db.addVille(villes[1]); 
     db.getAllVilles(); 
     // create the adapter using the cursor pointing to the desired data 
     //as well as the layout information 
     dataAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.fragment_recherhcer,db.getAllVillesAsCursor(),columns,to); 


     // Assign adapter to ListView 
     setListAdapter(dataAdapter); 
     return rootView; 
    } 

...

item_to_search.xml

<?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="horizontal" 
    android:layout_gravity="center_vertical" 
    android:weightSum="14"> 

    <TextView 
     android:id="@+id/tv_ville_secteur" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_weight="13" 
     android:gravity="center_vertical" 
     android:paddingTop="4dp" 
     android:text="TextView" 
     android:textColor="@android:color/holo_blue_bright" 
     android:textSize="22dp" /> 

    <CheckBox 
     android:id="@+id/cb_ville_secteur" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:text="" /> 

</LinearLayout> 
+0

请问这个回报'db.getAllVillesAsCursor()' – Raghunandan

+0

公开名单 getAllVilles() – Abdellah

+0

那么你误会构造的http://developer.android。 com/reference/android/widget/SimpleCursorAdapter.html – Raghunandan

回答

1

更改此

dataAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.fragment_recherhcer,db.getAllVillesAsCursor(),columns,to); 

dataAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.item_to_search,db.getAllVillesAsCursor(),columns,to); 

原因TextView ID为tv_ville_secteuritem_to_search.xml

而且

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 

这个构造函数是在API级别11推荐使用此选项 气馁,因为它导致 应用程序的UI线程上执行光标查询,从而可能会导致反应不佳或即使 应用程序没有响应错误。作为替代方法,使用带有CursorLoader的LoaderManager。

不应该使用上述

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) 
Standard constructor. 
+0

它的工作:d谢谢 – Abdellah

+1

@AbdellahSELASSI但千万注意,构造函数已不再 – Raghunandan

+0

@AbdellahSELASSI检查文档HTTP: //developer.android.com/reference/android/widget/CursorAdapter.html – Raghunandan

相关问题