0

所以我开始构建一个应用程序,我现在的问题是我想要两件事:

首先,我建立了一个包含_ID、类别、标题、正文和时间的表格。

现在我只想显示一个类别列表,但只有 1 次,所以如果将一行插入到表中,它会显示多次。我正在使用列表...我不知道,希望你们知道方法。

enter code here  private RemindersDbAdapter mDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reminder_list);
    mDbHelper = new RemindersDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());

}


private void fillData() {
    Cursor remindersCursor = mDbHelper.fetchAllReminders();
    startManagingCursor(remindersCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{RemindersDbAdapter.KEY_CAT};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter reminders = 
            new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
    setListAdapter(reminders);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu); 
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_insert: 
        createReminder();
        return true; 
    case R.id.menu_settings: 
        Intent i = new Intent(this, TaskPreferences.class); 
        startActivity(i); 
        return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater mi = getMenuInflater(); 
    mi.inflate(R.menu.list_menu_item_longpress, menu); 
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_delete:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deleteReminder(info.id);
        fillData();
        return true;
    }
    return super.onContextItemSelected(item);
}

private void createReminder() {
    Intent i = new Intent(this, ReminderEditActivity.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, ReminderEditActivity.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT); 
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}<i/>

和我的 DbAdapter

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "reminders";
private static final int DATABASE_VERSION = 4;

public static final String KEY_CAT = "category";
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME = "reminder_date_time"; 
public static final String KEY_ROWID = "_id";


private static final String TAG = "ReminderDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

/**
 * Database creation SQL statement
 */
private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " ("
                + KEY_ROWID + " integer primary key autoincrement, "
                + KEY_CAT + " text not null, "
                + KEY_TITLE + " text not null, " 
                + KEY_BODY + " text not null, " 
                + KEY_DATE_TIME + " text not null);"; 





private final Context mCtx;

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

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

/**
 * Constructor - takes the context to allow the database to be
 * opened/created
 * 
 * @param ctx the Context within which to work
 */
public RemindersDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

谢谢你能给我的任何建议。

4

1 回答 1

0

看起来你有你的数据库。要返回类别列表,您需要在数据库助手类中创建一个新方法:

public Cursor fetchCategoryList() {
String query = "SELECT DISTINCT " + KEY_CAT + " AS _id FROM " + DATABASE_TABLE;
return mDb.rawQuery(query, null);
}

DISTINCT关键字意味着它只会拉每个类别一次。如果你想在列表中使用它,你需要将它作为 _id 列拉出来,这就是它的AS _id一部分。 _id如果您打算使用光标填充列表,则需要它,但它不必是整数(尽管它确实应该是)。

然后将其用于列表,就像创建其他列表一样:

Cursor categoryCursor = mDbHelper.fetchCategoryList();
startManagingCursor(categoryCursor);

String[] from = new String[]{"_id"};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter categories = 
        new SimpleCursorAdapter(this, R.layout.reminder_row, categoryCursor, from, to);
setListAdapter(categories);
于 2012-05-16T03:52:33.160 回答