Custom adapter is not rendering a (populated) TextView when a second TextView is visible
I extended ArrayAdapter to support an image rendered aside two TextViews.
The layout itself is rendered correct with a generic ArrayAdapter (which only defines the text of one TextView).
http://i.stack.imgur.com/nRSKQ.jpg
When using the custom adapter (which overwrites ArrayAdapter.getView() to set icon and texts), the rendering is missing the "Hello World" - R.id.item_title - text.
http://i.stack.imgur.com/FSc5Z.jpg
hierarchyviewer: item_title - Layout/getHeight() = 0, but content "Hello World" is defined as text.
When setting R.id.item_subtitle to View.GONE, R.id.item_title will be rendered (as expected).
Do you know for what reason R.id.item_title has a height of zero, so no text is rendered/viewable? Is their something missing at SimpleImageItemAdapter.getView()?
developer.android.com/reference/android/widget/ArrayAdapter.html
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.
Source
tasks_dialog.xml - contains ListView
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tasks_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
tasks_dialog_item.xml - items rendered by ListView, based on developer.android.com/resources/articles/layout-tricks-efficiency.html
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView
android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Simple application that shows how to use RelativeLayout"
android:ellipsize="marquee"
android:singleLine="true"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@id/item_subtitle"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="My Application" android:minHeight="6dp"/>
</RelativeLayout>
TestActivity.java
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tasks_dialog);
ListAdapter adapter;
// ArrayAdapter - works perfect. See screenshot #1
{
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.tasks_dialog_item, R.id.item_title);
arrayAdapter.add("Hello World");
arrayAdapter.add("Lorem Ipsum");
adapter = arrayAdapter;
}
// Custom adapter - unexpected result: missing TextView `item_title`. See screenshot #2
{
ContentValues[] customItems = new ContentValues[2];
customItems[0] = new ContentValues(); // do not overwrite item - use default texts for testing
customItems[1] = new ContentValues(); // do not overwrite item - use default texts for testing
adapter = new SimpleImageItemAdapter(this, R.layout.tasks_dialog_item, customItems);
}
// render ListView with adapter or customAdapter
ListView menuList = (ListView) findViewById(R.id.tasks_dialog);
menuList.setAdapter(adapter);
}
}
SimpleImageItemAdapter.java
/**
* Simple adapter renders an icon and up to two text views based on ContentValue encoded data.
*
* Usage/variables:
*
* > ContentValues.put("title", "Hello World");
* > ContentValues.put("subtitle", "»Hello World« is a dummy text");
* > ContentValues.put("icon", R.drawable.fancyIcon);
*/
public class SimpleImageItemAdapter extends ArrayAdapter<ContentValues> {
protected ContentValues[] mItems;
public SimpleImageItemAdapter (Context context, int textViewResourceId, ContentValues[] items) {
super(context, te开发者_Python百科xtViewResourceId, items);
mItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// load layout
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.tasks_dialog_item, null);
}
ContentValues item = mItems[position];
// render elements
if (item != null) {
if (item.getAsString("title") != null) {
TextView titleView = (TextView) convertView.findViewById(R.id.item_title);
titleView.setText((String) item.get("title"));
}
if (item.getAsString("subtitle") != null) {
TextView subTitleView = (TextView) convertView.findViewById(R.id.item_subtitle);
subTitleView.setText((String) item.get("subtitle"));
}
if (item.getAsInteger("icon") != null) {
ImageView imageView = (ImageView) convertView.findViewById(R.id.item_icon);
imageView.setImageDrawable(convertView.getResources().getDrawable(item.getAsInteger("icon")));
}
}
return convertView;
}
}
Problem is with your xml file. Redefine tasks_dialog_item.xml as this;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout android:orientation="vertical"
android:layout_toRightOf="@id/item_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Application" />
<TextView android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Simple application that shows how to use RelativeLayout"
android:ellipsize="marquee"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
精彩评论