Android Listview OnItemClick显示对话框

时间:2017-09-28 05:52:40

标签: java android listview

在我的项目中,我会在单击ListView的每个项目时显示一个对话框。 但是我在第三个位置后得到一个错误。我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rapor);
    raporList = new ArrayList<>();
    liste = (ListView) findViewById(R.id.list);        
    new RaporServis().execute();

    //ListView Select
    liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position,
                                long id) {
            final CharSequence[] items = {"Görev Ataması Yapıldı", "İşlem Tamamlandı"};
            View curr = parent.getChildAt((int) id);
             //I'm getting an error here.
            TextView c = (TextView) curr.findViewById(R.id.gorevNo);
            String gorevNo = c.getText().toString();
            AlertDialog.Builder builder = new AlertDialog.Builder(RaporActivity.this);
            builder.setTitle("İş Durumu");
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    DialogSecimi = items[item].toString();
                }
            });

            builder.setPositiveButton("Kaydet",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            TextView tv = (TextView) view.findViewById(R.id.isDurum);
                            tv.setText(DialogSecimi);
                            tv.setTextColor(Color.parseColor("#27ae60"));
                            if (DialogSecimi == "Görev Ataması Yapıldı") {
                                isDurumuKodu = "1";
                            } else if (DialogSecimi == "İşlem Tamamlandı") {
                                isDurumuKodu = "3";
                            }

                                Toast.makeText(RaporActivity.this, "Saved", Toast.LENGTH_SHORT).show();
                            } catch (Exception ex) {
                                Toast.makeText(RaporActivity.this, "Error ! Don't Save", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
            builder.setNegativeButton("İptal Et",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Toast.makeText(RaporActivity.this, "İptal Edildi", Toast.LENGTH_SHORT).show();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });


}

错误:

  

尝试调用虚拟方法&#39; android.view.View   android.view.View.findViewById(INT)&#39;在空对象引用上

2 个答案:

答案 0 :(得分:0)

curr为null。为什么通过textView的文本获取数据?我认为你应该通过适配器获取数据,例如,String txt = adapter.get(position).getxxxx。

因为curr的数据是由适配器设置的,所以你知道原始数据。

答案 1 :(得分:0)

编辑此部分

IContentStream contentStream = document.GetContentStream();
Stream stream = contentStream.Stream;

... do something with the stream ...

stream.Close();

//View curr = parent.getChildAt((int) id);
//I'm getting an error here.
TextView c = (TextView) view.findViewById(R.id.gorevNo);