我米使用具有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>
請問這個回報'db.getAllVillesAsCursor()' – Raghunandan
公開名單 getAllVilles() –
Abdellah
那麼你誤會構造的http://developer.android。 com/reference/android/widget/SimpleCursorAdapter.html – Raghunandan