无法将alertDialog项设置为listview项

时间:2015-08-06 23:26:50

标签: android listview android-listview alertdialog android-alertdialog

每个listview项目中的2个textview,第1个用于listItem,第2个用于选择。我真正需要的是在每个列表视图中设置第二个textView与选择的选项相同并保存其值。当我选择“ItemSelected 5”并选择“选择3”时,它会在列表视图中给我:

ItemSelected 5
Choice 3

它的值是0.75。 我的问题是当我选择“ItemSelected 6”并选择“选择4”时,它被强制关闭。这是LogCat

08-06 23:13:18.742: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-06 23:13:18.742: E/AndroidRuntime(860): java.lang.NullPointerException
08-06 23:13:18.742: E/AndroidRuntime(860):  at id.app.diagnosagangguankehamilan.GejalaPilih$1$1.onClick(GejalaPilih.java:59) //Here is refer to (*_*)
08-06 23:13:18.742: E/AndroidRuntime(860):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.widget.AbsListView$1.run(AbsListView.java:3463)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.os.Handler.handleCallback(Handler.java:730)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.os.Looper.loop(Looper.java:137)
08-06 23:13:18.742: E/AndroidRuntime(860):  at android.app.ActivityThread.main(ActivityThread.java:5103)
08-06 23:13:18.742: E/AndroidRuntime(860):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 23:13:18.742: E/AndroidRuntime(860):  at java.lang.reflect.Method.invoke(Method.java:525)
08-06 23:13:18.742: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-06 23:13:18.742: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-06 23:13:18.742: E/AndroidRuntime(860):  at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

final Double[] value = new Double[25];

    //After OnCreate

        String[] listItem = {“ItemSelected 1”,”ItemSelected 2”,”ItemSelected 3”,”ItemSelected 4”,”ItemSelected 5”,”ItemSelected 6”,…,”ItemSelected 30”};

        final ListView listView = (ListView)findViewById(R.id.listViewItemSelected); 
        ArrayAdapter<String> adapter = new GejalaPilihAdapter(this, listItem);
        listView.setAdapter(adapter);

        listView.setOnItemSelectedClickListener(new OnItemSelectedClickListener() {

            @Override
            public void onItemSelectedClick(AdapterView<?> arg0, View arg1, final int position,long arg3) {


                final CharSequence[] choice = {"Choice 1","Choice 2","Choice 3","Choice 4"};

                AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);
                alert.setIcon(R.drawable.ic_launcher);
                alert.setTitle("Select Choice");
                alert.setSingleChoiceItemSelecteds(choice, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int itemSelected) {

                        TextView textView = (TextView) listView.getChildAt(position).findViewById(R.id.textViewChoice); //(*_*)
                        textView.setText(choice[itemSelected]);                     

                        switch (itemSelected) {
                        case 0:
                            value[position] = 0.25;
                            break;
                        case 1:
                            value[position] = 0.5;
                            break;
                        case 2:
                            value[position] = 0.75;
                            break;
                        case 3:
                            value[position] = 1.0;
                            break;
                        default:
                            value[position] = 0.0;
                            break;
                        }

                        Log.d("","Choice["+position+"] = "+choice[itemSelected]);

                        dialog.dismiss();

                    }
                });
                AlertDialog al = alert.create();
                al.show();
            }

        });

1 个答案:

答案 0 :(得分:1)

您不需要listView.getChildAt(position),因为当您点击列表视图项时,它已经带来了它的视图。使用View arg1方法检查onItemSelectedClick()参数。你可以把参数作为最终的

 public void onItemSelectedClick(AdapterView<?> arg0, final View arg1, final int position,long arg3) {

并使用它来获取textview

TextView textView = (TextView) arg1.findViewById(R.id.textViewChoice);