没有在DIalog框上获得列表视图

时间:2014-08-02 07:39:01

标签: android listview dialog

问题是它显示我的nullpointer异常,而点击onshowContact按钮有什么不对,我无法弄明白但我没有得到带有此代码的列表视图的对话框 按钮单击联系人视图

public void showContacts(View view){
        //View CSV into an array
        try{
        Dialog dialog = new Dialog(NewMessage.this);
        dialog.setContentView(R.layout.contact_view);
        dialog.setTitle("Contacts");

        dialog.setCancelable(true);
        ListView lv = (ListView)findViewById(R.id.lv);
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        int fillarray = 0;
        if(mylist.equals(null)){
            fillarray = 0; 
        }
        else{
            fillarray = mylist.size();
        }*/
        for(int i=0;i<name.size();i++)
        {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name",name.get(i));
            map.put("number",number.get(i));
            map.put("status",status.get(i));
            mylist.add(map);
        }
        SimpleAdapter mSchedule = new SimpleAdapter(NewMessage.this, mylist, R.layout.view_csv, new String[] {"name","number","status"}, new int[] {R.id.t1,R.id.t2,R.id.t3});
        lv.setAdapter(mSchedule);
    dialog.show();
        }
        catch(Exception e){
            Toast.makeText(NewMessage.this, e.toString(), 2000).show();
        }
//Viewing Finish Here

    }

这是我的contact_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="397dp" >
    </ListView>

</LinearLayout>

这是我的view_csv.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
    android:id="@+id/t1"
    android:layout_width="90dip"
    android:layout_height="wrap_content"
    android:text="text1"/>
     <TextView 
     android:id="@+id/t2"
    android:layout_width="90dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="text2"/>
      <TextView 
     android:id="@+id/t3"
    android:layout_width="50dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="text3" />    

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的ListView正在contact_view布局下。所以你必须通过对话框初始化列表。因为您的对话框包含dialog.setContentView(R.layout.contact_view);

的布局

所以你必须初始化列表,如:

 ListView lv = (ListView) dialog.findViewById(R.id.lv);