【问题标题】:How to send contents of Listview to another Activity?如何将 Listview 的内容发送到另一个 Activity?
【发布时间】:2013-06-06 22:13:31
【问题描述】:

我有一个RelativeLayout,里面有两个TextView,布局看起来像一个按钮。用户按下“按钮”,我将其中一个 TextViews 发送到生成的 TextView 和我放入列表中的第二个 TextView。这都是在同一个活动上完成的。用户可以继续按下“按钮”,它将在 ListView 中填充许多项目。我的问题是如何将生成的 TextView 和 ListView 中填充的内容发送到新活动。我可以很好地发送生成的 TextView 的内容,但无法将 ListView 的内容发送到新的 Activity。

这是第一个活动

public class MenuView1Activity extends ListActivity {

private double overallTotalproduct;
public static TextView resultTextView;

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

private Button whateverButton;
       TextView inputPrice;
       RelativeLayout lay1;
@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menuview);

        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
                setListAdapter(adapter);

whateverButton = (Button)findViewById(R.id.whateverButton);
inputPrice= (TextView)findViewById(R.id.inputPrice);
lay1= (RelativeLayout)findViewById(R.id.lay1);


//Total Box
    final TextView textViewtotalproduct = (TextView) findViewById(R.id.inputPrice);

final TextView textView1 = (TextView) findViewById(R.id.m1aa);
    final String stringm1aa = textView1.getText().toString();
    final double intm1aa = Double.parseDouble(stringm1aa);
    final TextView textView1a = (TextView) findViewById(R.id.m1a);
    final String stringm1a = textView1a.getText().toString();

lay1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            listItems.add("$"+intm1aa +"-"+ stringm1a);
            adapter.notifyDataSetChanged();
            resultTextView.setText(stringm1a);
            overallTotalproduct =  intm1aa + overallTotalproduct;
            textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
        }
    });

public void onwhateverPress(View v) {
Intent whateverIntent = new Intent(this, WhateverActivity.class);

if (whateverResult.iswhateveranicewhatever()) {

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



startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putStringArrayListExtra("list", (ArrayList<String>) listItems));
finish();

第二个

    Intent id11 = getIntent();
    if (id11.getCharSequenceExtra("list") != null) {
    final TextView setmsg = (TextView)findViewById(R.id.saleNotes);
    setmsg.setText(id11.getCharSequenceExtra("list"));              

    }

【问题讨论】:

  • 你能解释一下“将 ListView 的内容发送到新的 Activity 没有得到任何结果”吗?
  • 我没有收到任何错误,但来自 ListView 的数据也没有显示在第二个活动中。
  • 你说的是listItems吗?
  • 是的,listItems 没有出现在下一个活动中

标签: android android-intent android-listview


【解决方案1】:

您可以使用 ListView.getAdapter() 从 ListView 获取所有更新的项目,然后使用 Intent.putStringArrayListExtra 将 ArrayList 从一个 Activity 发送到另一个:

startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putStringArrayListExtra("list", (ArrayList<String>)getListView().getAdapter()));
finish();

【讨论】:

  • 我在 .putStringArrayListExtra("list", (ArrayList)getListView().getAdapter()));
  • 实际错误是 06-06 21:49:27.811: E/AndroidRuntime(11505): java.lang.RuntimeException: 传递结果失败ResultInfo{who=null, request=100, result=13274384 , data=Intent { (has extras) }} 到活动 {com.xxx.xxx/com.xxx.xxx.MenuView1Activity}: java.lang.ClassCastException: android.widget.ArrayAdapter
猜你喜欢
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多