Question

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>
Was it helpful?

Solution 2

The culprit was:

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

Should be fill_parent.

OTHER TIPS

Please review your XML, some values are 0.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top