在由阵列Adapapter填充后,然后动态地将复合项目添加到Java中的ListView



在此合适的示例中,字符串类型的项目在listView被适配器填充后动态添加:

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();
        }
    });
}
}

但是,当填充列表视图的项目复合时,我无法使其起作用(我得到了一个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>

好吧,在listView被ArrayAdapter填充后,以编程方式添加视频ITEM的代码可能是:

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();

如何正确执行?任何想法?谢谢

为什么会初始化这样的数组?

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

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

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

相关内容

最新更新