android - 每行具有动态 View 的自定义 ListActivity

标签 android listactivity

我想为 ListActivity 创建自定义行,每行包含 TitleList Of SubTitles我的问题是:创建这些SubTitles 的最佳方式是什么?我无法在 row.xml 中添加这些副标题,因为它们应该是动态的,具体取决于每一行。

我正在尝试写这个:

class Research{
        public String title;
        public ArrayList<String> subTitles;
    }
private class MyAdapter extends ArrayAdapter<Research> {

    public MyAdapter(Context context, int textViewResourceId,ArrayList<Research> items) {
        super(context, textViewResourceId, items);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Research o = items.get(position);
        if (o != null) {
            TextView tt = (TextView) v.findViewById(R.id.textView1);
            if (tt != null) {
                tt.setText(o.title);
            }
        }

        /// THE PROBLEM STARTS HERE
        for(String subTitle: o.subTitles){
            TextView subTextView = new TextView(ResearchListActivity.this);
            subTextView.setText(subTitle);
            // where to add this view ? I don't see Layout here !
            parent.addView(subTextView);

        }
        //


        return v;
    }
}

行布局:

<?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="fill_parent"
    android:orientation="vertical">
    <!-- Title -->
<TextView android:id="@+id/textView1" android:layout_width="fill_parent"
    android:text="" android:layout_height="wrap_content"
    android:textColor="#ddd" android:gravity="center" android:background="#7DB5C9"></TextView>

</LinearLayout>

清楚了吗?

谢谢。

最佳答案

View v 是 row.xml 中的外部元素,即 LinearLayout。

    LinearLayout l = (LinearLayout)v;

然后在该布局上运行 l.addView(...)。

关于android - 每行具有动态 View 的自定义 ListActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727366/

相关文章:

android - Firestore 自定义 `toObject` 反序列化

android - 我的 AlarmService 只关闭了 3 秒

android - 访问 firebase 回收器适配器中的子节点

android - 知道 IP - 下载我的应用程序的设备地址并为设备创建唯一 ID

android - 在 ListView 中(快速)滚动时,列表项( View )顺序发生意外变化

android - 日期变量自动获取到 1970 年的年份。它必须是 2015 年

android - 如何添加一个按钮来调用以编程方式给定的电话号码?

android - 具有自定义布局的 ListActivity 不调用 getview

java - 将字符串资源 ID 数组与 ListAdapter 一起使用吗?

java - Android ListActivity : onCreate is called every time after screen rotation, 所选项目重置,但视觉上仍保持选中状态