0

だから私はアプリを作り始めました、そして今私の問題は私が2つのものが欲しいということです:

まず、_ID、Category、Title、Body、Timeを含むテーブルを作成しました。

ここで、カテゴリのリストを表示したいのですが、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キーワードは、各カテゴリを 1 回だけプルすることを意味します。リストで使用する場合は、_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 に答える