Text not showing up in ListView when using a custom adapter
Asked Answered
D

2

0

For some reason, I can't get text to show up in my list view. When I run the following example, two blank list elements appear with no text. Also, the debugging output shows that the value of the text in the TextView is correct. Here is my main activity:

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

    private static final String TAG = "MyListActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<String> values = new ArrayList<String>();

        values.add("Test1");
        values.add("Test2");

        TimelineAdapter adapter = new TimelineAdapter(
                this.getApplicationContext(), values);
        setListAdapter(adapter);

        Log.d(TAG, "Finished onCreate");
    }

    private class TimelineAdapter extends ArrayAdapter<String> {

        ArrayList<String> items;

        public TimelineAdapter(Context context, ArrayList<String> values) {
            super(context, R.layout.row, R.id.message, values);
            this.items = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Log.d(TAG, "Entered getView");

            // For future performance optimization
            View view = convertView;

            // Inflate the view
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.row, parent, false);
                Log.d(TAG, "View inflated");
            }

            // Get current value
            String value = items.get(position);
            Log.d(TAG, "Shout: " + value);

            // Set TextView text
            if (value != null) {
                TextView message = (TextView) view.findViewById(R.id.message);

                message.setText(value);
                Log.d(TAG, (String) message.getText());
                Log.d(TAG, "Textview text set");
            }
            return view;
        }

    }
}

And here is my row.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">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:id="@+id/message"
    />

</LinearLayout>
Dethrone answered 26/2, 2012 at 4:57 Comment(0)
B
2

I dont see setContentView(..), maybe thats the problem?

Borchardt answered 26/2, 2012 at 8:28 Comment(3)
You don't need setContentView() if the Activity extends ListActivity. You can have it though, and I've tried. But it doesn't make a difference.Dethrone
I run your code. It works, when layout_height for TextView set to wrap_content or fill_parentBorchardt
Oh, wow. I used the row layout code from a tutorial, and while I was confused why the height was zero, I assumed it would get overriden later or something like that. Thanks for taking the time to run my code!Dethrone
P
1

I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).

I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.

Once I added android:orientation="vertical" everything worked fine.

Two hours of my life on this one.

Pullen answered 19/5, 2012 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.