自定义列表布局在模拟器中不起作用

时间:2015-08-06 02:00:57

标签: android

Android的新手,对不起,如果我应该包含的内容,我不会。

无论如何,我试图为列表制作自定义布局。每当我尝试运行应用程序时,它都会关闭并发送一个" AppName已崩溃"错误模式。当我使用内置的Android布局时,这并没有发生。我不确定问题是我的代码还是我的模拟器(在带有1GB RAM的Nexus 5上运行)

此处layout/row_layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textSize="50sp"
        android:textStyle="bold"
        android:padding="15dp"/>

</LinearLayout>

此处layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

</LinearLayout>

这里是onCreate中的MainActivity.java方法(我还没有编辑任何其他内容)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] favoriteTVShows = {"The Office", "The Wire", "Mr. Robot", "Parks and Rec",
            "Fool Us", "Garbage Time", "Last Week Tonight", "Silicon Valley"};
    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout,
            favoriteTVShows);
    ListView theListView = (ListView) findViewById(R.id.theListView);
    theListView.setAdapter(theAdapter);
    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                           @Override
                                           public void onItemClick(AdapterView<?> adapterView,
                                                                   View view, int position,
                                                                   long id) {
                                                String tvShowPicked = "You selected "  +
                                                        String.valueOf(adapterView
                                                                .getItemAtPosition(position));
                                                Toast.makeText(MainActivity.this, tvShowPicked,
                                                       Toast.LENGTH_SHORT).show();
                                            }
                                           });
}

1 个答案:

答案 0 :(得分:0)

您必须将适配器设置为可以包含文本的视图,这意味着不使用LinearLayout作为基础,而是使用TextView:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:textSize="50sp"
    android:textStyle="bold"
    android:padding="15dp"/>