从ArrayList动态添加textview到自定义适配器

时间:2016-05-27 20:29:15

标签: java android

我使用自定义适配器来显示列表项,这些列表项在标题下方的每个列表项中都有一个图像,一个标题和N个单独的字符串。

如何在每个列表项的com.google.android.flexbox.FlexboxLayout内使用城市标题实现N个TextView? N量的TextView来自ArrayList<City>

public City (String city, int id) {
        this.city = city;
        this.id = id; 
}

谢谢!

Problem visualization here

适配器:

    public class PersonAdapter extends ArrayAdapter<Person> {

    private ArrayList<City> cities;

    public PersonAdapter(Context context, ArrayList<Person> persons) {
        super(context, 0, persons);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Hole Daten für diese Position
        Person person = getItem(position);

        cities = person.getCities();

        if (convertView == null) {

            convertView = LayoutInflater.from(getContext())
    .inflate(R.layout.list_explore_people_row, parent, false);
        }

        TextView personName = (TextView) convertView.findViewById(R.id.listItemExplorePeopleName);
        ImageView profileImg = (ImageView) convertView.findViewById(R.id.listItemExplorePeopleImg);
        TextView profileCity = (TextView) convertView.findViewById(R.id.listItemExplorePeopleCityItem);

        personName.setText(person.getName());
        profileCity.setText(person.getCities().toString()); //This is my best progress currently
        profileImg.setImageBitmap(getBitmap(getContext(), person.getProfileImg()));

        return convertView;
    }

    public Bitmap getBitmap(Context ctx, String profileFilename) {
    ...code of getBitmap for image
    }
}

PersonAdapter的XML布局(R.layout.list_explore_people_row)

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:paddingBottom="@dimen/list_row_vertical_margin"
    android:paddingLeft="@dimen/list_row_horizontal_margin"
    android:paddingRight="@dimen/list_row_horizontal_margin"
    android:paddingTop="@dimen/list_row_vertical_margin">

    <ImageView
        android:id="@+id/listItemExplorePeopleImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="0dp"
        android:minHeight="100dp"
        android:minWidth="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/profileimg1" />

    <TextView
        android:id="@+id/listItemExplorePeopleName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="2dp"
        android:layout_toEndOf="@+id/listItemExplorePeopleImg"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Mona"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listItemExplorePeopleName"
        android:layout_toEndOf="@+id/listItemExplorePeopleImg"
        app:flexWrap="wrap"

        android:layout_alignParentEnd="true"
        android:id="@+id/include">

        <TextView
            android:id="@+id/listItemExplorePeopleCityItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="city one"
            android:background="@android:color/holo_green_light"></TextView>

    </com.google.android.flexbox.FlexboxLayout>

1 个答案:

答案 0 :(得分:1)

for(City city : cities) {
    TextView textView = new TextView(this);
    textView.setText(person.getCities().toString());
    convertView.addView(textView);
}

我目前不在我的主电脑附近,所以我实际上无法检查它,但这个想法大致是这个。 有关详细信息,请参阅:Android dynamically add views