SimpleCursorAdapter:清空ListView

时间:2014-04-23 16:15:28

标签: android android-listfragment simplecursoradapter

我使用SimpleCursorAdapter ListFragment,但显示的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>

1 个答案:

答案 0 :(得分: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 tv_ville_secteur item_to_search.xml SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

另外

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

这个构造函数在API级别11中已弃用。此选项为   气馁,因为它会导致Cursor查询被执行   应用程序的UI线程因此可能导致响应性差或甚至   应用程序无响应错误。作为替代方案,请使用   带有CursorLoader的LoaderManager。

不应使用上述

{{1}}