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>