在通过ArrayAdapter填充之后,然后将复合项动态添加到Java中的ListView

时间:2017-12-16 09:36:07

标签: android listview

在这个合适的示例中,在ListView由适配器填充后动态添加String类型的项目:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get reference of widgets from XML layout
    final ListView lv = (ListView) findViewById(R.id.lv);
    final Button btn = (Button) findViewById(R.id.btn);

    // Initializing a new String Array
    String[] fruits = new String[] {
            "Cape Gooseberry",
            "Capuli cherry"
    };

    // Create a List from String Array elements
    final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

    // Create an ArrayAdapter from List
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, fruits_list);

    // DataBind ListView with items from ArrayAdapter
    lv.setAdapter(arrayAdapter);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Add new Items to List
            fruits_list.add("Loquat");
            fruits_list.add("Pear");
            /*
                notifyDataSetChanged ()
                    Notifies the attached observers that the underlying
                    data has been changed and any View reflecting the
                    data set should refresh itself.
             */
            arrayAdapter.notifyDataSetChanged();
        }
    });
}
}

但是当填充ListView的项目复杂化时,我无法使其工作(我得到一个IndexOutOfBoundsException)。要添加到ListView的非简单项的示例是:

public class VideoItem {

private String title;
private String description;
private String thumbnailURL;
private String id;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getThumbnailURL() {
    return thumbnailURL;
}

public void setThumbnailURL(String thumbnailURL) {
    this.thumbnailURL = thumbnailURL;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
}

它的xml文件是:

<?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:padding="16dp"
android:background="?android:attr/activatedBackgroundIndicator">

<ImageView
    android:id="@+id/video_thumbnail"
    android:layout_width="128dp"
    android:layout_height="128dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="20dp"/>

<TextView
    android:id="@+id/video_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/video_thumbnail"
    android:layout_alignParentTop="true"
    android:layout_marginTop="5dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/video_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/video_thumbnail"
    android:layout_below="@+id/video_title"
    android:textSize="15sp" />

</RelativeLayout>

嗯,在ListAdapter填充ListView之后以编程方式添加VideoItem的代码可能是:

VideoItem objVideoItem = new VideoItem();
objVideoItem.setTitle("A Title");
objVideoItem.setDescription("This is a Description");
objVideoItem.setThumbnailURL("/storage/emulated/0/Cores.png");
searchResultsGloVideoItemList.add(objVideoItem);
youtubeGloVideoItemAdapter.notifyDataSetChanged();

如何正确完成?任何的想法?感谢

1 个答案:

答案 0 :(得分:0)

为什么要像这样初始化数组?

    final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

使用以下代码替换代码中的上述行,然后重试。

    final List<String> fruits_list =Arrays.asList(fruits);