为什么我得到一张空白的列表视图?! (使用simplecursoradapter)

时间:2014-02-09 05:11:19

标签: android sqlite listview simplecursoradapter

在数据库项目中,我有一个包含三个变量的表,即'_id','item'表示食品名称,'type'表示食品类型。我有三种类型的食物:'Vege Soup','Non Vege Soup'和'Chai'。

在我的MainActivity Class中,我有一个组中的三个单选按钮,用于选择项目的“类型”,然后是“查看”按钮,以获取通过单选按钮选择的类型中所有项目的ListView。 / p>

我使用SimpleCursorAdapter,它用于将游标中的列映射到XML文件中定义的TextViews或ImageViews。

预期的结果是ListView包含行信息。当前结果是一个空白的ListView。我无法理解为什么。

MainActivity类:

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>

按下“查看”​​按钮会创建打开列表视图活动的意图。

ItemListView类:

public class ItemListView extends Activity {

DBAdapter myDb;
MainActivity mA;
Intent a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_list_view);

    Bundle gotBasket = getIntent().getExtras();
    String from = gotBasket.getString("type");
    openDB();
    ListViewTypeFromDB_MainActivity(from);
}

private void openDB() {
    myDb = new DBAdapter(this);
    myDb.open();
}

private void closeDB() {
    myDb.close();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    closeDB();
}

private void ListViewTypeFromDB_MainActivity(String fromguy) {
    Cursor cursor = myDb.getRows(fromguy);
    startManagingCursor(cursor);
    String[] fromFieldNames = new String[] { DBAdapter.KEY_ITEM };
    int[] toViewIDs = new int[] { R.id.item_name };
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, 
            R.layout.item_layout,
            cursor,
            fromFieldNames,
            toViewIDs
    );
    ListView myList = (ListView) findViewById(R.id.listViewFromDB);
    myList.setAdapter(myCursorAdapter);
}
}

item_list_view.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>

这是listview每行的布局。

item_layout.xml:

<?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>

因此,正如您在ItemListView类中看到的那样,我使用了SimpleCursorAdapter。虽然它设法进入item_list_view活动,但它完全是空白的。这是我的DatabaseAdapter。

DBAdapter类:

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);
    }
}
}

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

0 个答案:

没有答案