0

我有两个活动,比如 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);
}

标题正在显示,但故事部分没有,请帮我解决这个问题,我已经尝试了几个小时。有人能告诉我为什么我的问题只得到几个答复吗?

4

1 回答 1

0

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

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

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

您确定故事字段是在数据库中编写的吗?检查您获得的故事的价值,将其写在日志或敬酒中。


旁注:您可以从类中构造意图:

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

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

于 2012-05-25T12:57:16.427 回答