ListView 将不再显示

发布于 2024-09-07 18:11:10 字数 3547 浏览 2 评论 0原文

我的应用程序使用以下 main.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:id="@+id/toplinear">
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:id="@+id/linear">
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Previous"
            android:id="@+id/previous"
            android:layout_weight="1"
            />
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Next"
            android:id="@+id/next"
            android:layout_toRightOf="@id/previous"
            android:layout_weight="1"
            />
    </LinearLayout>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/android:list"
        android:layout_weight="10"
        />

</LinearLayout>

我想在顶部有 2 个按钮,上一个和下一个,以及下面带有自定义适配器的列表视图。几天前,我只显示了 ListView,浏览了按钮。现在我似乎被困在另一边,我的列表视图不显示,而我的按钮显示。

我的活动在 ListActivity 上扩展,并使用此代码用自定义适配器填充它。

ListView lv = getListView();
lv.setAdapter(new MyCustomAdapter(this, R.layout.custom_list, nameResults));

    public class MyCustomAdapter extends ArrayAdapter<String> {
        private String[] nameResults;
        private Context context;

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] names) {
            super(context, textViewResourceId, names);
            this.context = context;
            nameResults = names;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_list, parent, false);
            }

            TextView label = (TextView) row.findViewById(R.id.Name);
            label.setText(nameResults[position]);
            TextView descr = (TextView) row.findViewById(R.id.Description);
            descr.setText(linkedResults.get(nameResults[position]));

            return row;
        }
    }

我尝试使用“lv.addHeaderView(previous);”但这只给了我关于 LayoutParams 的模糊 ClassCastExceptions 。

我认为我的问题目前在我的 main.xml 中,因为 Eclipse 中的“布局”选项卡也不会显示 ListView。它知道它在那里,因为它显示在轮廓选项卡中,但当我选择它时,视觉表示不会给它一个红色轮廓。

为了完整起见,我的 custom_list (又名行)布局 xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff"
>
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Name"
    android:textColor="#000000"
    android:textStyle="bold"
  />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Description"
    android:textColor="#000000"
  />

</LinearLayout>

I use the following main.xml for my app.

<?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:id="@+id/toplinear">
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:id="@+id/linear">
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Previous"
            android:id="@+id/previous"
            android:layout_weight="1"
            />
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Next"
            android:id="@+id/next"
            android:layout_toRightOf="@id/previous"
            android:layout_weight="1"
            />
    </LinearLayout>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/android:list"
        android:layout_weight="10"
        />

</LinearLayout>

I want to have 2 buttons at the top, previous and next and my listview with custom adapter below it. Few days ago I had only my ListView showing, going over the buttons. Now I seem to be stuck on the other side, where my listview doesn't show while my buttons do.

My activity extends on ListActivity and uses this code to fill it with a customadapter.

ListView lv = getListView();
lv.setAdapter(new MyCustomAdapter(this, R.layout.custom_list, nameResults));

    public class MyCustomAdapter extends ArrayAdapter<String> {
        private String[] nameResults;
        private Context context;

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] names) {
            super(context, textViewResourceId, names);
            this.context = context;
            nameResults = names;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_list, parent, false);
            }

            TextView label = (TextView) row.findViewById(R.id.Name);
            label.setText(nameResults[position]);
            TextView descr = (TextView) row.findViewById(R.id.Description);
            descr.setText(linkedResults.get(nameResults[position]));

            return row;
        }
    }

I tried using "lv.addHeaderView(previous);" but that only gave me vague ClassCastExceptions about LayoutParams.

I think my problem is currently in my main.xml, as the Layout tab in Eclipse of it won't show the ListView either. It knows its there as it shows up in the outline tab, but the visual representation doesn't give it a red outline when I select it.

For completeness, my custom_list (aka row) layout xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff"
>
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Name"
    android:textColor="#000000"
    android:textStyle="bold"
  />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Description"
    android:textColor="#000000"
  />

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-09-14 18:11:10

我稍微修改了你的 main.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"
    android:id="@+id/toplinear">
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:id="@+id/linear">
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Previous"
            android:id="@+id/previous"
            android:layout_weight="1"
            />
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Next"
            android:id="@+id/next"
            android:layout_toRightOf="@id/previous"
            android:layout_weight="1"
            />
    </LinearLayout>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/android:list"
        />

</LinearLayout>

I'm modified your main.xml a bit - added orientation and took off the layout_weight for the listview - hopefully this fixes that problem.

<?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"
    android:id="@+id/toplinear">
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:id="@+id/linear">
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Previous"
            android:id="@+id/previous"
            android:layout_weight="1"
            />
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Next"
            android:id="@+id/next"
            android:layout_toRightOf="@id/previous"
            android:layout_weight="1"
            />
    </LinearLayout>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/android:list"
        />

</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文