Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我会得到一个空白的列表视图?!(使用SimpleCrsorAdapter)_Android_Sqlite_Listview_Simplecursoradapter - Fatal编程技术网

Android 为什么我会得到一个空白的列表视图?!(使用SimpleCrsorAdapter)

Android 为什么我会得到一个空白的列表视图?!(使用SimpleCrsorAdapter),android,sqlite,listview,simplecursoradapter,Android,Sqlite,Listview,Simplecursoradapter,在一个数据库项目中,我有一个包含三个变量的表,即'\u id',item'表示食品名称,type'表示食品类型。我有三种食物:“蔬菜汤”、“非蔬菜汤”和“柴” 在我的MainActivity类中,我在一个组中有三个单选按钮,选择项目的“类型”,然后是一个“查看”按钮,从通过单选按钮选择的类型中获取所有项目的列表视图 我使用SimpleCursorAdapter,它用于将光标中的列映射到XML文件中定义的TextView或ImageView 预期结果是ListView包含行信息。当前结果是一个空白

在一个数据库项目中,我有一个包含三个变量的表,即'\u id',item'表示食品名称,type'表示食品类型。我有三种食物:“蔬菜汤”、“非蔬菜汤”和“柴”

在我的MainActivity类中,我在一个组中有三个单选按钮,选择项目的“类型”,然后是一个“查看”按钮,从通过单选按钮选择的类型中获取所有项目的列表视图

我使用SimpleCursorAdapter,它用于将光标中的列映射到XML文件中定义的TextView或ImageView

预期结果是ListView包含行信息。当前结果是一个空白的ListView。我不明白为什么

主要活动类别:

public class MainActivity extends Activity implements OnClickListener,
    OnCheckedChangeListener {

Button dinnerAdd, dinnerView;
RadioGroup dishType;
RadioButton vegeSoupView, nonVegeSoupView, chaiView;
Intent i, v;
String type;
DBAdapter myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
}

private void initialize() {
    dinnerView = (Button) findViewById(R.id.bView);
    dishType = (RadioGroup) findViewById(R.id.rgDishType);
    vegeSoupView = (RadioButton) findViewById(R.id.rDtVegeSoup);
    nonVegeSoupView = (RadioButton) findViewById(R.id.rDtNonVegeSoup);
    chaiView = (RadioButton) findViewById(R.id.rDtChai);
    dinnerAdd.setOnClickListener(this);
    dinnerView.setOnClickListener(this);
    dishType.setOnCheckedChangeListener(this);
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    switch (view.getId()) {
    case R.id.bAddDish:
        i = new Intent("com.example.databaselist.ADDITEM");
        startActivity(i);
        break;
    case R.id.bView:
        didItWork = true;
            if (vegeSoupView.isChecked()) {
                type = "Vege Soup";
            } else {
                if (nonVegeSoupView.isChecked()) {
                    type = "Non Vege Soup";
                } else {
                    if (chaiView.isChecked()) {
                        type = "Chai";
                    }
                }
            }
            makeIntent();
            startActivity(v);
                break;
    }

}

public void makeIntent(){
    Bundle basket = new Bundle();
    basket.putString("type", type);
    v = new Intent(MainActivity.this, ItemListView.class);
    v.putExtras(basket);
}
}
activity_main.xml:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/rgDishType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:layout_below="@id/bRandomise">

    <RadioButton
        android:id="@+id/rDtVegeSoup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Vege Soup" />

    <RadioButton
        android:id="@+id/rDtNonVegeSoup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Non Vege Soup" 
        android:textSize="15dp"/>

    <RadioButton
        android:id="@+id/rDtChai"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chai" />
</RadioGroup>

<Button
    android:id="@+id/bView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/rgDishType"
    android:layout_centerHorizontal="true"
    android:text="View" />

 <Button
    android:id="@+id/bAddDish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/bView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="Add Dish" />

</RelativeLayout>
项目列表视图.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="vertical" >

<ListView
    android:id="@+id/listViewFromDB"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

那么,SimpleCorsOrAdapter是否有问题,或者它可能是DBAdapter类中的问题?

如果您提供一个简化的测试用例,您可能会得到更多答案,这是获得此空白列表视图而不是完整源文件的最小测试用例。好的。我已经把代码简化了。希望没问题。
<?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="vertical" >

<ListView
    android:id="@+id/listViewFromDB"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

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


<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="NAME!"
    android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>
private static final String TAG = "DBAdapter";

public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
// TODO: Setup your fields here:
public static final String KEY_ITEM = "item";
public static final String KEY_TYPE = "type";


// TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
public static final int COL_ITEM = 1;
public static final int COL_TYPE= 2;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_ITEM,
    KEY_TYPE};
public static final String[] KEY_ITEM_ONLY = new String[] {KEY_ITEM};

public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";
public static final int DATABASE_VERSION = 3;   

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_ITEM + " string not null unique, "
        + KEY_TYPE + " string not null"
        + ");";
private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;

/////////////////////////////////////////////////////////////////////
//  Public methods:
/////////////////////////////////////////////////////////////////////

public DBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

public void close() {
    myDBHelper.close();
}

public long insertRow(String item, String type) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ITEM, item);
    initialValues.put(KEY_TYPE, type);
    // Insert it into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}

public Cursor getRows(String type){
    String where = KEY_TYPE + "='" + type + "'";
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

/////////////////////////////////////////////////////////////////////
//  Private Helper Classes:
/////////////////////////////////////////////////////////////////////


private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + 
        oldVersion + " to " + newVersion + ", which will destroy all old             
            data!");    
        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
}
}