【问题标题】:Android - Passing database and id through activitiesAndroid - 通过活动传递数据库和ID
【发布时间】:2013-04-24 09:04:47
【问题描述】:

我有 2 个 android 活动

我有房间列表的地方,即房间列表。

其他房间内容。显示所选房间的所有内容的位置。

我想要的只是将房间 id(r_id) 通过房间列表类传递到房间概览类中,我可以在其中从数据库中检索其所有内容。

我一直在试图找出我哪里出错了,我认为它在第 94 - 101 行附近:

            selectedRoom = (String) ((TextView) view).getText();
            Cursor c2 = sampleDB.rawQuery("SELECT * FROM tbl_roomDesc WHERE roomName =?", new String[] {selectedRoom});

            if (c2 != null ) {
                if  (c2.moveToFirst()) {
                    do {
                        r_id = c2.getString(c2.getColumnIndex("r_id")); 
                    }while (c2.moveToNext());
                }
            }
            c2.close();

谁能帮我弄清楚我在这里做错了什么?

Here is the source for the room list

Here is the source for the room overview

提前致谢!

【问题讨论】:

标签: android database android-activity


【解决方案1】:

要在Intents 之间传递数据,请使用putExtras() 方法。

例子:

Intent i = new Intent(...);
i.putExtras("roomid", "10");
startActivity(i);

【讨论】:

    【解决方案2】:
         final int roomId = r_id;
         next = (ImageButton) findViewById(R.id.next);
         next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
    
                CreateBuilding.val = "Retrieve";
                sampleDB.close();
                Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class);
                intent2.putExtra("roomId", roomId);
                setResult(RESULT_OK, intent2);
                finish();
                startActivityForResult(intent2, 0);
            }
        });
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (resultCode == Activity.RESULT_OK) {
              if (data != null) {
                int roomId = data. getIntExtra("roomId", 0);
              }
           }
        }
    

    如果我没有误解,您将 roomId 放入 Intent 并在 onActivityResult 中检索它

    【讨论】:

    • 如何在onActivityResult中检索roomId?
    • 我很难理解 :( 首先,onActivityResult 放在哪里?
    • 我找不到你从哪里开始 RoomOverview
    • 这很清楚。我的问题是你从哪里开始 RoomOverview?
    • 虽然这不是一个坏主意,但在实践中,通过意图将某些内容发送到另一个活动并使用onActivityResult 来检索该操作的结果更有意义。我同意 SavTheCoder 的答案
    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多