动态ListView OnItemClick - 应该填充另一个活动中的值

时间:2013-03-17 09:20:35

标签: android android-listview

Android 2.3.3

我有两项活动,家庭和历史。我在历史记录中有一个动态列表视图(使用自定义适配器),我在列表视图中有数据。单击某个项目时,该项目中的值应反映在主页活动中(应填充textview和edittext)。

以下是代码......

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

     TextView txtExp = (TextView) arg1.findViewById(R.id.txtView_History_Expression);
     TextView txtRes = (TextView) arg1.findViewById(R.id.txtView_History_Result);

     String ret_Exp = txtExp.getText().toString();
     String ret_Res = txtRes.getText().toString();

     System.out.println("exp = "+ret_Exp+"   res = "+ret_Res);

     ma = new MainActivity();
     TextView ma_prev = (TextView) ma.findViewById(R.id.txtViewPrevious);
     EditText ma_Res = (EditText) ma.findViewById(R.id.edtxtInput);

     ma_prev.setText("");
     ma_prev.setText(ret_Exp);

     ma_Res.setText("");
     ma_Res.setText(ret_Res);

    }

当我使用上面的代码时,我得到一个nullpointer异常...

03-17 14:39:42.199: E/AndroidRuntime(20411): java.lang.NullPointerException
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.app.Activity.findViewById(Activity.java:1647)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at com.mobilevoiceapps.advancedvoicecalculatorfree.History.onItemClick(History.java:207)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.widget.ListView.performItemClick(ListView.java:3744)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1991)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.os.Handler.handleCallback(Handler.java:587)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.os.Looper.loop(Looper.java:130)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at android.app.ActivityThread.main(ActivityThread.java:3687)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at java.lang.reflect.Method.invoke(Method.java:507)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
03-17 14:39:42.199: E/AndroidRuntime(20411):    at dalvik.system.NativeStart.main(Native Method)

这是listview行::::

的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/txtView_History_Count"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_weight="0.20"
        android:text="Number"
        android:textColor="#FFFFFF"
        android:textSize="22dp"
        android:paddingLeft="5dp"
        android:gravity="center" />

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_weight="0.80"
        android:orientation="vertical"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/txtView_History_Result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Result"
            android:textColor="#316DA2"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/txtView_History_Expression"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Expression"
            android:textColor="#FFFFFF"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

我不认为我这样做是正确的。有人能告诉我怎么做......

1 个答案:

答案 0 :(得分:1)

ma = new MainActivity();
TextView ma_prev = (TextView) ma.findViewById(R.id.txtViewPrevious);
EditText ma_Res = (EditText) ma.findViewById(R.id.edtxtInput);

在Android中不会以这种方式运行。您应该为Intent创建MainActivity,将要传递的数据放在附加内容中Bundle并调用startActivity()以创建MainActivity。然后,您应该从Bundle MainActivity's内的传入onCreate()检索数据,并使用它填充您的观看次数。