我可以在下一个活动中将字符串值从 MulitiColumn listView 存储到 Simple ListView 吗?

Posted

技术标签:

【中文标题】我可以在下一个活动中将字符串值从 MulitiColumn listView 存储到 Simple ListView 吗?【英文标题】:Can I Store The String Values From MulitiColumn listView to Simple ListView in the Next Activity? 【发布时间】:2012-07-28 20:47:15 【问题描述】:

如何在 Next Activity 中从具有两列的自定义 ListView 获取值到 Simple ListView。我有 select1 和 select2 String[] 变量。Myval 是全局变量。我有以下例外 *

logcat 中的异常:

    07-31 10:37:55.470: E/androidRuntime(974): FATAL EXCEPTION: main
    07-31 10:37:55.470: E/AndroidRuntime(974): java.lang.NullPointerException
    07-31 10:37:55.470: E/AndroidRuntime(974):  at                android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
     07-31 10:37:55.470: E/AndroidRuntime(974):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.ListView.fillDown(ListView.java:672)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.ListView.fillFromTop(ListView.java:732)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.ListView.layoutChildren(ListView.java:1625)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.View.layout(View.java:11278)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.View.layout(View.java:11278)
     07-31 10:37:55.470: E/AndroidRuntime(974):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.View.layout(View.java:11278)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.View.layout(View.java:11278)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.View.layout(View.java:11278)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.os.Looper.loop(Looper.java:137)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at java.lang.reflect.Method.invoke(Method.java:511)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-31 10:37:55.470: E/AndroidRuntime(974):  at dalvik.system.NativeStart.main(Native Method)

从 Spinner 获取值:

     public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id)               


  String Text = parent.getSelectedItem().toString();
    if(Text.equals("----SELECT----")) 

           
    else if(Text.equals("BEVERAGES"))
        select1=new String[Beverage.length];
        select2=new String[P1.length];
        System.out.println("Length of Beverage:" + select1.length);
        System.out.println("Length of P1:" + select2.length);
        select1 = Beverage;
        select2 = P1;
       l1.setAdapter(new EfficientAdapter(HomeActivity.this));
       return;

       

从自定义 ListView 中获取值:

                  l1.setOnItemClickListener(new OnItemClickListener()  

                   public void onItemClick(AdapterView<?> parent, View view, 
                   int position, long id)  

                   // When clicked, show a toast with the TextView text 
                   Toast.makeText(HomeActivity.this, "SELECTED :: " + "item "+select1[position] + " " + "price"+ select2[position], 
                   Toast.LENGTH_SHORT).show(); 
           String ss=(String)parent.getItemAtPosition(position);
           GlobalClass.myval.add(ss);

【问题讨论】:

您还可以使用意图通过 putExtra 传递值。并使用 Bundles 获取该值。 您可以在第二个活动中创建适配器并将其设置为简单的 ListView。 【参考方案1】:

在您的代码中替换以下内容,它可能会为您提供一些 l1.setAdapter(new EfficientAdapter(HomeActivity.this)); 用。。。来代替 : l1.setAdapter(new EfficientAdapter(HomeActivity.this),arraytosendtoadapter); //将您的高效适配器更改为具有 String[] 以便接收您发送的内容

【讨论】:

以上是关于我可以在下一个活动中将字符串值从 MulitiColumn listView 存储到 Simple ListView 吗?的主要内容,如果未能解决你的问题,请参考以下文章

在android中将值从一个窗口传递到另一个窗口

使用 Struts 2 在项目中将查询字符串中的键值从一个页面传递到另一个页面

无法在Android中将变量值从一个活动发送到另一个活动

在 C# 中将十六进制值从 SQL Server 转换为日期时间

使用jQuery在多个gridview行中将值从一个文本框复制到另一个文本框

在Django中将计算值从视图保存到模型