【问题标题】:Problems with putExtra and putStringArrayListputExtra 和 putStringArrayList 的问题
【发布时间】:2013-06-07 07:03:06
【问题描述】:

我正在尝试将一些数据从一项活动发送到另一项活动,它可以正常工作,但不像我想要的那样工作。 问题1-事情变得混乱。在列表项的下一个活动部分将转到不正确的 textView 并部分转到正确的 textview。 问题 2- 我只能在新活动中列出 1 项,但我希望能够发送多个列表项。我认为问题在于将不同类型的 putExtra 请求组合到同一个地方,就像我在这里做的那样。

.putExtra("inputPrice",(CharSequence)pick) .putStringArrayListExtra("list", listItems)

Ant 帮助将不胜感激。

将数据发送到下一个活动

final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();

final TextView uplane =(TextView)findViewById(R.id.inputPrice);                     
String pick = uplane.getText().toString();

final TextView daplane =(TextView)findViewById(R.id.date);                  
String watch = daplane.getText().toString();

 startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();

这是下一个活动

        Intent is = getIntent();
    if (is.getCharSequenceExtra("Card Number") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
        setmsg.setText(is.getCharSequenceExtra("Card Number"));             
    }
        Intent it = getIntent();
    if (it.getCharSequenceExtra("date") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleTime);
        setmsg.setText(it.getCharSequenceExtra("date"));                
    }
    Intent id1 = getIntent();
    if (id1.getCharSequenceExtra("inputPrice") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleName);
        setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
    }
    ArrayList<String> al= new ArrayList<String>();
    al = getIntent().getExtras().getStringArrayList("list");
    saleNotes= (TextView) findViewById(R.id.saleNotes); 
    saleNotes.setText(al.get(0));

【问题讨论】:

  • 请发布您的错误日志
  • 没有错误。 “列表”正像它应该的那样被发送到 saleNotes,但只有一个列表实例。列表显示在“saleRccn”的新活动中
  • 这两个答案很有帮助,但我仍然无法获取 ArrayList
  • 见下面我的回答。显示的原因列表是您通过 getExtras 正确使用它,而您没有将它用于其他变量。此外,您只是试图通过 al.get(0) 访问数组的第一个元素,这就是为什么只显示第一个项目。

标签: android android-intent arraylist charsequence


【解决方案1】:

好的,有几件事:

首先,您不需要将字符串转换为CharSequence。

第二件事,

定义意图,添加您的附加功能,然后才调用 startActivity,如下所示:

Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);

第三,在检索意图时,首先创建一个捆绑包,如下所示:

Bundle extras = getIntent().getExtras();
String date = extras.getString("date");

编辑:

以下是将整个数组列表转换为单个字符串并将其添加到文本视图的方法。

String listString = "";

for (String s : al)
{
    listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}

System.out.println(listString);
saleNotes.setText(listString);

希望这会有所帮助!

【讨论】:

  • 你知道如何获取arraylist吗?我在将它传送到 TextView 时遇到了很多麻烦。
  • @SmulianJulian 请参阅上面的编辑,以便从您的数组中制作一个长字符串!
  • 太棒了...工作得很好!
【解决方案2】:

试试这个,不要使用CharSequence 只是输入字符串值

startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);

然后变成这样

  Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
    final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
    setmsg.setText(is.getStringExtra("Card Number"));             
}

【讨论】:

  • 是的!这就是我一直想问的
猜你喜欢
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
相关资源
最近更新 更多