当第二个 TextView 可见时,自定义适配器不会渲染(填充的)TextView

发布于 2024-11-27 06:26:28 字数 6163 浏览 0 评论 0原文

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

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

https://i.sstatic.net/nRSKQ.jpg

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

https://i.sstatic.net/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

要使用 TextView 以外的内容进行数组显示,例如 实例、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;
    }
}

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).

https://i.sstatic.net/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.

https://i.sstatic.net/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, 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2024-12-04 06:26:28

问题出在你的 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文