【问题标题】:Custom adapter is not rendering a (populated) TextView when a second TextView is visible当第二个 TextView 可见时,自定义适配器不呈现(填充的)TextView
【发布时间】:2011-07-30 16:13:40
【问题描述】:

我扩展了 ArrayAdapter 以支持在两个 TextView 旁边呈现的图像。

布局本身使用通用 ArrayAdapter(仅定义一个 TextView 的文本)正确呈现。

http://i.stack.imgur.com/nRSKQ.jpg

使用自定义适配器(覆盖 ArrayAdapter.getView() 以设置图标和文本)时,呈现缺少“Hello World” - R.id.item_title - 文本。

http://i.stack.imgur.com/FSc5Z.jpg

hierarchyviewer: item_title - Layout/getHeight() = 0,但内容“Hello World”被定义为文本。

将 R.id.item_subtitle 设置为 View.GONE 时,将呈现 R.id.item_title(如预期的那样)。

你知道为什么 R.id.item_title 的高度为零,所以没有文本被渲染/查看吗?他们在 SimpleImageItemAdapter.getView() 中是否缺少某些东西?

developer.android.com/reference/android/widget/ArrayAdapter.html

要使用 TextViews 以外的东西进行数组显示,对于 例如,ImageViews,或者除了 toString() 之外还有一些数据 结果填充视图,覆盖 getView(int, View, ViewGroup) 到 返回你想要的视图类型。


来源

tasks_dialog.xml - 包含 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 - 由 ListView 呈现的项目,基于 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, textViewResourceId, 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;
    }
}

【问题讨论】:

    标签: android textview android-arrayadapter


    【解决方案1】:

    问题在于您的 xml 文件。将tasks_dialog_item.xml重新定义为这样;

    <?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>
    

    【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2023-03-23
    • 2015-12-10
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多