1

我的列表视图出现错误,我尝试了很多方法。我没明白。我错过了什么?我应该做哪些改变?如果我在没有对话的情况下尝试使用 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)
4

1 回答 1

5

我不确定您在做什么,但您似乎正在尝试在对话框中显示列表视图,如下面的屏幕截图:

在此处输入图像描述

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

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);
    }

}

主要的.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"
/>
于 2013-01-17T05:44:48.480 回答