Android人口ListView

时间:2016-03-03 16:28:41

标签: android listview

我的数据库中的ListView上没有显示任何内容。这是我的代码。

这是来自收集数据的数据库连接。

public Cursor getData(String hlth_cond) {
    SQLiteDatabase db = this.getWritableDatabase();
    String StringQuery = "";

    if(hlth_cond == "Highblood"){
        StringQuery = "SELECT RECIPE_NAME FROM TBL_NAME";
    }
    else if (hlth_cond == "Arthritis"){
        StringQuery = "SELECT RECIPE_NAME FROM TBL_NAME_Arth";
    }
    else if (hlth_cond == "Diabetic"){
        StringQuery = "SELECT RECIPE_NAME FROM TBL_NAME_Dia";
    }
    Cursor res;
    return res = db.rawQuery(StringQuery,null);

}

此代码来自我的主要活动

public void showList(){
    String toPass="";

    if(Cb_Ar.isChecked()){
        toPass = Cb_Ar.toString();
    }else if (Cb_Db.isChecked()){
        toPass = Cb_Db.toString();
    }else if (Cb_Hb.isChecked()){
        toPass = Cb_Hb.toString();
    }

    Cursor cursor = myDb.getData(toPass);
    String[] fromDb = new String[] {DatabaseHelper.RECIPE_NAME};
    int[] toView = new int[] {R.id.textViewMenuSuggest};
    SimpleCursorAdapter myCursor;
    myCursor = new SimpleCursorAdapter(getBaseContext(),R.layout.menushow,cursor,fromDb,toView,0);
    ListView myList = (ListView) findViewById(R.id.listView);
    myList.setAdapter(myCursor);
}

2 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Menu to cook: "
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foregroundGravity="center"
    android:id="@+id/listView"
    android:layout_below="@+id/textView4"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"/>

答案 1 :(得分:0)

做这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

    <TextView android:text="@string/app_name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

    <ListView android:id="@+id/selectView"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="wrap_content"/>

</LinearLayout>