如何显示两个文本字段

时间:2012-05-25 12:51:16

标签: android

我有两个活动,比如A1和A2,它们都有两个编辑文本字段,一个用于标题,另一个用于故事。当用户在活动A1中输入文本时,在这两个文本字段中,按下保存按钮,该条目将保存在列表视图中。现在,当用户再次点击刚保存的条目时,标题和故事应显示在活动A2的标题和故事文本字段中。我在活动A2中获得了标题,但我无法理解为什么故事部分没有显示出来。

当点击该项目时,这是活动A1的代码。

static String title = "";
static String story = "";

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // here arg3 is the id of the item which is pressed
    registerForContextMenu(arg1);
    id_item_clicked = arg3;
    position_item = arg2;
    Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
    title = c.getString(c.getColumnIndex(DataHolder.KEY_TITLE));
    story = c.getString(c.getColumnIndex(DataHolder.KEY_STORY));
    Intent i = null;

    // remove this toast later
    Toast.makeText(DiaryActivity.this, "TestClick", Toast.LENGTH_SHORT).show();

    try {
        i = new Intent(A1.this,
                Class.forName("com.android.project.A2"));
        i.putExtra(ROW_ID, arg3);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    startActivity(i);
}

以下是Activity A2的代码,它在两个文本字段中显示文本:

EditText title_read, display_story_read;
private long rowId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readdata);

    // bundle receives the row id of the item clicked
    Bundle extras = getIntent().getExtras();

    rowId = extras.getLong("row_id");

    setupVaiables();

    title_read.setText(A1.title);
    display_story_read.setText(A1.story);
}

标题正在显示,但故事部分不是,请帮助我,我已经尝试了几个小时。有人可以告诉我为什么我只能回答几个问题吗?

1 个答案:

答案 0 :(得分:0)

以这种方式传递变量是不好的做法。您可以通过意图传递变量:

i.putExtra(TITLE_KEY, title);
i.putExtra(STORY_KEY, story);

或从活动A2中的数据库中获取标题和故事,而不是A1。

你确定故事领域是在DB中写的吗?检查您获得的故事的价值,将其写入日志或吐司。


旁注:您可以从班级构建意图:

Intent i = new Intent(A1.this, A2.class);

在这种情况下,您不需要try..catch阻止。

相关问题