2014-04-23 100 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

相關問題