文本未出现在我的 ListView 中

发布于 2024-10-03 01:49:09 字数 3824 浏览 4 评论 0原文

我有一个正在填充的 ListView。我已检查数据源的内容,它包含有效数据。不幸的是,行数是正确的,但我想要显示的文本没有出现。

以下是我设置 ListView 的方法:

private void setupUI() {
        setContentView(R.layout.streamactivity);
        this.streamListView = (ListView) findViewById(R.id.streamListView);
        this.streamAdapter = new StreamAdapter(this, R.layout.streamactivityrow, messages);
        streamListView.setAdapter(this.streamAdapter);
    }

这是我的适配器:

private class StreamAdapter extends ArrayAdapter<STMessage> {

        private ArrayList<STMessage> messages;

        public StreamAdapter(Context context, int textViewResourceId, ArrayList<STMessage> messages) {
            super(context, textViewResourceId, messages);
            this.messages = messages;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.streamactivityrow, null);
            }

            STMessage aMessage = messages.get(position);

            if (aMessage != null) {
                TextView titleText = (TextView) v.findViewById(R.id.toptext);
                TextView descriptionText = (TextView) v.findViewById(R.id.bottomtext);

                if (titleText != null) {
                    titleText.setText(aMessage.getBody());
                }
                if (descriptionText != null) {
                    descriptionText.setText(aMessage.getFor_user_login());
                }
            }

            return v;
        }
    }

这是列表中每一行的 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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical" android:layout_height="0dip" android:singleLine="true" android:textSize="15sp" android:textStyle="bold" android:ellipsize="end" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        android:textSize="15sp" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dateText" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#E2E2E2"></TextView></LinearLayout>

</LinearLayout>

这是带有 ListView 的 StreamActivity 的 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"
    >


<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/streamListView"></ListView>
</LinearLayout>

I have a ListView that is being populated. I have checked the contents of the datasource and it contains valid data. Unfortunately, the number of rows is correct, but the text I want to display is not appearing.

Here is how I am setting up the ListView:

private void setupUI() {
        setContentView(R.layout.streamactivity);
        this.streamListView = (ListView) findViewById(R.id.streamListView);
        this.streamAdapter = new StreamAdapter(this, R.layout.streamactivityrow, messages);
        streamListView.setAdapter(this.streamAdapter);
    }

Here is my Adapter:

private class StreamAdapter extends ArrayAdapter<STMessage> {

        private ArrayList<STMessage> messages;

        public StreamAdapter(Context context, int textViewResourceId, ArrayList<STMessage> messages) {
            super(context, textViewResourceId, messages);
            this.messages = messages;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.streamactivityrow, null);
            }

            STMessage aMessage = messages.get(position);

            if (aMessage != null) {
                TextView titleText = (TextView) v.findViewById(R.id.toptext);
                TextView descriptionText = (TextView) v.findViewById(R.id.bottomtext);

                if (titleText != null) {
                    titleText.setText(aMessage.getBody());
                }
                if (descriptionText != null) {
                    descriptionText.setText(aMessage.getFor_user_login());
                }
            }

            return v;
        }
    }

Here is the XML for each row in the list:

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical" android:layout_height="0dip" android:singleLine="true" android:textSize="15sp" android:textStyle="bold" android:ellipsize="end" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        android:textSize="15sp" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dateText" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#E2E2E2"></TextView></LinearLayout>

</LinearLayout>

Here is the XML for the StreamActivity with the ListView:

<?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"
    >


<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/streamListView"></ListView>
</LinearLayout>

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

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

发布评论

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

评论(2

征棹 2024-10-10 01:49:09

请检查您的 XML,某些值为 0。

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"

Please review your XML, some values are 0.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
征棹 2024-10-10 01:49:09

罪魁祸首是:

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

应该是 fill_parent

The culprit was:

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

Should be fill_parent.

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