Text not showing up in ListView when using a custom adapter

回眸只為那壹抹淺笑 提交于 2019-12-11 15:38:31

问题


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>

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/9450662/text-not-showing-up-in-listview-when-using-a-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!