Question

I m using an SimpleCursorAdapter with ListFragment, but the displayed ListView is Empty (items without data):

    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>
Was it helpful?

Solution

Change this

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

to

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

cause TextView with id tv_ville_secteur is in item_to_search.xml.

Also

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

This constructor was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

Should not use the above

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)
Standard constructor.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top