Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ListView不适用于android_Android_Listview - Fatal编程技术网

ListView不适用于android

ListView不适用于android,android,listview,Android,Listview,因此,我正在阅读Commonware的Android编程教程,书中要求我添加ListView的部分让我陷入了困境。这是我的布局的xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent"

因此,我正在阅读Commonware的Android编程教程,书中要求我添加ListView的部分让我陷入了困境。这是我的布局的xml

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

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>

这是我的活动代码

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import java.util.LinkedList;
import java.util.List;

public class LunchList extends Activity {

    List<Restaurant> model = new LinkedList<Restaurant>();
    ArrayAdapter<Restaurant> arrayAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button save = (Button) findViewById(R.id.save);

        save.setOnClickListener(onSave);

        ListView listView = (ListView) findViewById(R.id.restaurants);
        arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
        listView.setAdapter(arrayAdapter);

    }


    private View.OnClickListener onSave = new View.OnClickListener(){

        public void onClick(View view) {
            EditText nameField = (EditText) findViewById(R.id.name);
            EditText addressField = (EditText) findViewById(R.id.address);

            Restaurant restaurant = new Restaurant();
            restaurant.setName(nameField.getText().toString());
            restaurant.setAddress(addressField.getText().toString());


            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);

            switch (radioGroup.getCheckedRadioButtonId()) {

                case (R.id.take_out):
                    restaurant.setType("take_out");
                    break;

                case (R.id.sit_down):
                    restaurant.setType("sit_down");
                    break;

                case (R.id.delivery):
                    restaurant.setType("delivery");
                    break;

                case (R.id.korean):
                    restaurant.setType("korean");
                    break;

                case (R.id.chinese):
                    restaurant.setType("chinese");
                    break;

                case (R.id.japanese):
                    restaurant.setType("japanese");
                    break;

                case (R.id.italian):
                    restaurant.setType("italian");
                    break;

                case (R.id.indonesian):
                    restaurant.setType("indonesian");
                    break;

            }

            arrayAdapter.add(restaurant);
            Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
        }
    };
}
package.com.example;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.*;
导入java.util.LinkedList;
导入java.util.List;
公共课堂午餐列表扩展活动{
列表模型=新建LinkedList();
ArrayAdapter ArrayAdapter=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮保存=(按钮)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView ListView=(ListView)findViewById(R.id.restaurants);

arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,型号); setAdapter(arrayAdapter); } private View.OnClickListener onSave=new View.OnClickListener(){ 公共void onClick(视图){ EditText名称字段=(EditText)findViewById(R.id.name); EditText地址字段=(EditText)findViewById(R.id.address); 餐厅=新餐厅(); setName(nameField.getText().toString()); setAddress(addressField.getText().toString()); 放射组放射组=(放射组)findViewById(R.id.types); 开关(radioGroup.getCheckedRadioButtonId()){ 箱子(R.id.取出): 餐厅。设置类型(“外卖”); 打破 案例(R.id.坐下): 餐厅。设置类型(“坐下”); 打破 案例(R.id.交付): 餐厅。设置类型(“交付”); 打破 案例(R.id.korean): 餐厅。setType(“韩语”); 打破 案例(R.id.chinese): 餐厅。setType(“中国”); 打破 案例(R.id.japanese): 餐厅。setType(“日语”); 打破 案例(R.id.意大利语): 餐厅。setType(“意大利”); 打破 案件(R.id.indonesian): 餐厅。setType(“印尼”); 打破 } arrayAdapter.add(餐厅); Log.i(“午餐列表”,“数组适配器大小:”+arrayAdapter.getCount()); } }; }
我添加了一个日志行,以查看对象是否被添加到适配器中,并且它看起来像是被添加到适配器中。但是,UI没有显示ListView,我也没有看到列表中添加的内容

编辑XML:

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

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>

在我看来,您可以初始化模型:

List<Restaurant> model = new LinkedList<Restaurant>();
或:

notifyDataSetChanged()
ListView
知道列表内容已更改,它应该重新绘制自己


编辑:此外,您正在名为
详细信息
表格布局上方添加
列表视图
,其高度和宽度设置为
fill\u parent
,因此如果
表格布局占据整个屏幕,您可能看不到
列表视图
。尝试将
详细信息
的高度更改为
包装内容

您希望此UI的外观如何?您的列表视图显示:

             android:layout_alignParentTop="true"
              android:layout_above="@id/details"
因此,您基本上是说希望ListView位于详细信息之上。但是
details
是第一个元素,因此on可能已经出现在屏幕的顶部了


编辑:我在一篇评论中写道,但我应该修改我的答案:使用线性布局(当然是垂直方向),并为每个元素赋予1的权重(或者根据需要调整权重)。这样一来,一个元素就不能淹没另一个元素。

我认为你被否决了,因为你包含了很多不相关的代码。你可以考虑修剪它。我甚至不知道哪个部分是不相关的,因为我不知道问题出在哪里。。。上次我因为删减了太多的代码而被否决了…呵呵,是的,当ppl不留下评论说明他们为什么被否决的时候,这是很糟糕的。说真的。匿名的反对票是最糟糕的+arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,型号);setAdapter(arrayAdapter);我也会做一些调整。加上(餐厅);每当单击“保存”时,我都会看到您将模型传递给ArrayAdapter,但您没有向模型添加任何餐厅对象,因此ArrayAdapter将获得一个空列表,而不是直接添加到列表中否,而是将I添加到ArrayAdapter。arrayAdapter.add(餐厅);我改为模特。加(餐厅);arrayAdapter.notifyDataSetChanged();没有任何变化,我希望它是关于细节的,但是XML元素的位置在这种情况下重要吗?绝对重要。RelativeLayout可能有点像finnicky,我建议从上到下填充它。因此我只是将我的ListView移到TableLayout之前,并将上面的android:layout_=“@+id/details”和表布局的id更改为android:id=“@id/details”。什么都没有改变
arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();
             android:layout_alignParentTop="true"
              android:layout_above="@id/details"