如何进入listview对话框

时间:2013-01-16 12:22:26

标签: android android-layout listview dialog

我在listview上收到错误,我尝试了很多方法。我没弄明白。什么我失踪了?我应该做些什么改变?如果我尝试使用arrayadapter没有对话成功运行。 我是android新手。我非常感谢...

代码 -

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewList = Topic.this.getLayoutInflater().inflate(R.layout.list_row, null);
            dialogMarketList = new Dialog(Topic.this);
        dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialogMarketList.setContentView(viewList);      
            dialogMarketList.show();
            Display newDisplay = getWindowManager().getDefaultDisplay(); 
            int width = newDisplay.getWidth();
        lvForDialog = (ListView) viewList.findViewById(R.id.ListView);
       ArrayAdapter<String> adapter =(new ArrayAdapter<String>(Topic.this,R.layout.topic_row,R.id.childname, groupnamelist));
       lvForDialog.setAdapter(adapter);
    //   lv = (ListView) findViewById(R.id.ListView);
     //   ArrayAdapter<String>adapter=new ArrayAdapter<String>(Topic.this,android.R.layout.simple_list_item_1,groupnamelist);
     //   lv.setAdapter(adapter);
        lvForDialog.setOnItemClickListener(new OnItemClickListener()
    {
            @Override
           public void onItemClick(AdapterView<?> parent, View view, int position,long id)        
  

topic_row的布局

       <?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="match_parent"
        android:orientation="vertical" >

<ListView
    android:id="@+id/childname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

的logcat -

   450: E/AndroidRuntime(1540): FATAL EXCEPTION: main
   450: E/AndroidRuntime(1540): java.lang.IllegalStateException: ArrayAdapter requires
   the resource ID to be a TextView
   450: E/AndroidRuntime(1540):     at 
   android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
   450: E/AndroidRuntime(1540):     at
   android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
   450: E/AndroidRuntime(1540):     at 
   android.widget.AbsListView.obtainView(AbsListView.java:1430)
   450: E/AndroidRuntime(1540):     at 
   android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.widget.ListView.onMeasure(ListView.java:1127)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.view.View.measure(View.java:8313)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.view.View.measure(View.java:8313)
   01-16 17:11:09.450: E/AndroidRuntime(1540):  at 
   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
   450: E/AndroidRuntime(1540):     at 
   android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
   E/AndroidRuntime(1540):  at 
   android.view.View.measure(View.java:8313)
   450: E/AndroidRuntime(1540):     at 
   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
   450: E/AndroidRuntime(1540):     at 
   android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    E/AndroidRuntime(1540):     at 
   android.view.View.measure(View.java:8313)
   450: E/AndroidRuntime(1540):     at 
    android.view.ViewRoot.performTraversals(ViewRoot.java:839)
   450: E/AndroidRuntime(1540):     at
   android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
   450: E/AndroidRuntime(1540):     at 
   android.os.Handler.dispatchMessage(Handler.java:99)
   450: E/AndroidRuntime(1540):     at 
   android.os.Looper.loop(Looper.java:123) 
   450: E/AndroidRuntime(1540):     at 
   android.app.ActivityThread.main(ActivityThread.java:3683)
   450: E/AndroidRuntime(1540):     at 
   java.lang.reflect.Method.invokeNative(Native Method)
   450: E/AndroidRuntime(1540):     at 
   java.lang.reflect.Method.invoke(Method.java:507)
   450: E/AndroidRuntime(1540):     at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    450: E/AndroidRuntime(1540):    at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
     450: E/AndroidRuntime(1540):   at 
    dalvik.system.NativeStart.main(Native Method)
     450: E/AndroidRuntime(1540): Caused by: 
     java.lang.ClassCastException: android.widget.ListView
     450: E/AndroidRuntime(1540):   at 
     android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:343)

1 个答案:

答案 0 :(得分:5)

我不确定你是做什么的,但似乎你试图在对话框中显示listview,如下面的屏幕截图:

enter image description here

我在你发布的代码的帮助下实现了这一点,几乎没有变化,我发布的代码只是通过并让我知道:

<强> MainActivity.java

public class MainActivity extends Activity {
    Dialog dialogMarketList;
    ListView lvForDialog;
    public String[] groupnamelist = { "one", "two", "three", "one", "two",
            "three", "one", "two", "three" };
    View viewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewList = MainActivity.this.getLayoutInflater().inflate(R.layout.main,
                null);
        dialogMarketList = new Dialog(MainActivity.this);
        dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogMarketList.setContentView(viewList);
        dialogMarketList.show();
        Display newDisplay = getWindowManager().getDefaultDisplay();
        int width = newDisplay.getWidth();
        lvForDialog = (ListView) viewList.findViewById(R.id.listView);
        ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
                MainActivity.this, R.layout.topic_row, R.id.childname,
                groupnamelist));
        lvForDialog.setAdapter(adapter);
    }

}

<强> main.xml中

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

<强> topic_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/childname"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>