数据库Quandry

时间:2010-09-02 17:21:03

标签: android

试图在整个地方找到我做错的答案。我希望用户在调用类中选择一个按钮,打开一个显示数据库内容的被调用listactivity,让用户单击一个条目,将该条目复制到新数据库中,将rowid从新数据库发回调用类并让调用类将新数据库条目中的标题分配给已推送的原始按钮。

这是调用类

static final private int CHOOSE_MONDAY = 0; 
static final private int CHOOSE_TUESDAY = 0;
 private int ButtonPushed = 0; 
private NotesDbAdapter mDbHelper; 
private MenuDbAdapter menuDbHelper; 
private Long mRowId;
 String menuTitle; 
String menuProtein; 
String menuBody;

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

    setContentView(R.layout.plan_menu);

    Toast.makeText(this, "Choose a day to pick a meal for!", Toast.LENGTH_LONG).show();
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    menuDbHelper = new MenuDbAdapter(this);
    menuDbHelper.open();



}

public void mButtonHandler(View target)
{
    switch(target.getId())
    {
    case R.id.monday:
        // Create new intent object and tell it to call the ColorPicker class
        Intent question = new Intent(this, PlanMenuList.class); 
        // Start ColorPicker as a new activity and wait for the result 
        startActivityForResult(question, CHOOSE_MONDAY);
        break;
    case R.id.tuesday:
        // Create new intent object and tell it to call the ColorPicker class
        Intent question1 = new Intent(this, PlanMenuList.class);    
        // Start ColorPicker as a new activity and wait for the result 
        startActivityForResult(question1, CHOOSE_TUESDAY);
        break;
    }

然后这是被调用的类,我试图将用户的选择复制到新数据库,然后将id发送回调用类。

public class PlanMenuList extends ListActivity {

private NotesDbAdapter mDbHelper;
private MenuDbAdapter menuDbHelper;
private List<Data>data;
String menuTitle;
String menuProtein;
String menuBody;
private Long mRowId;




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes_list);
    mDbHelper = new NotesDbAdapter(this);
    menuDbHelper = new MenuDbAdapter(this);
    mDbHelper.open();
    menuDbHelper.open();
    fillData(); 
}

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

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

    // 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 notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);

}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        menuTitle=(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        menuProtein=(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_PROTEIN)));
        menuBody=(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
    }
}





protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    mDbHelper.fetchNote(id);
    mRowId = id;
    //populateFields();
    menuDbHelper.createMenu("Monday", menuTitle, menuProtein, menuBody);
    Intent answer = new Intent();
    answer.putExtra("MenuDbAdapter.KEY_ROWID", mRowId);
    setResult(RESULT_OK, answer);
    finish();



}
}

我已经乱搞这件事了好几天似乎无法做到我想做的事 - 任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:0)

您可以发布onActivityResult实施吗? 当我将数据传递回活动时,我使用了Bundles。例如:

Intent answer = new Intent();
Bundle extras = new Bundle();
extras.putLong("MenuDbAdapter.KEY_ROWID", mRowId);
answer.putExtras(extras);

然后,在活动结果处理程序中,您将调用Intent.getExtras并从那里提取您的值。

编辑:以下是android开发指南中的一些示例: http://developer.android.com/guide/appendix/faq/commontasks.html(搜索onActivityResult) http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html

答案 1 :(得分:0)

protected void onListItemClick(ListView l, View v, int position, long id) {

变量id与数据库行号不同,那就是问题所在。我将创建一个自定义适配器并将_id(行号)存储为视图中的标记。检索OnItemClickListener中的标记并执行数据库查询。

自定义适配器代码: 私有类Cursy扩展了CursorAdapter {

    public Cursy(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        viewHolder holder = (viewHolder) view.getTag();

        holder.tv.setText(cursor.getString(cursor
                .getColumnIndex(NotesDbAdapter.KEY_TITLE)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        viewHolder holder = new viewHolder();
        holder._id = ursor.getString(cursor.getColumnIndex(NotesDbAdapter._ID));

        View v = getLayoutInflater().inflate(
                R.layout.list_layout, null);

        holder.tv = (TextView) v
                .findViewById(R.id.tv);

        v.setTag(holder);
        return v;
    }

}

OnItemClickListener:

mListView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            viewHolder holder = (viewHolder) view.getTag();

            // use holder._id and do the db queries
        }
    });

答案 2 :(得分:0)

首先,我想您要确保CHOOSE_MONDAYCHOOSE_TUESDAY常量是不同的值,以便稍后区分它们。

其次,您要将错误的行ID发送回原始活动。假设您的createMenu()方法基于SQLiteDatabase.insert(),它应该在插入后返回行ID(如果出现问题,则返回-1)。您可以将其用作行ID:

    mRowId = menuDbHelper.createMenu("Monday", menuTitle, menuProtein, menuBody);