Android 如何将不同的静态图像添加到我的静态列表项

Android 如何将不同的静态图像添加到我的静态列表项,android,listview,imageview,Android,Listview,Imageview,我有一个ListView示例,其中ListView中的图像是来自XML的静态图像。我想向静态列表项添加不同的静态图像。我正在尝试添加类似“items.add(newentryItem(“R.drawable.image”,“Item 1”,“This is Item 1.1”);“如何获得这样的功能?以下是我的工作代码: SectionListExampleActivity.java public class SectionListExampleActivity extends ListActi

我有一个ListView示例,其中ListView中的图像是来自XML的静态图像。我想向静态列表项添加不同的静态图像。我正在尝试添加类似“items.add(newentryItem(“R.drawable.image”,“Item 1”,“This is Item 1.1”);“如何获得这样的功能?以下是我的工作代码:

SectionListExampleActivity.java

public class SectionListExampleActivity extends ListActivity {
    /** Called when the activity is first created. */

     ArrayList<Item> items = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        items.add(new SectionItem("Category 1"));
        items.add(new EntryItem("Item 1", "This is item 1.1"));
        items.add(new EntryItem("Item 2", "This is item 1.2"));
        items.add(new EntryItem("Item 3", "This is item 1.3"));
       //"items.add(new EntryItem("R.drawable.image", "Item 1", "This is item 1.1"));"  //Is this possible ? 


        items.add(new SectionItem("Category 2"));
        items.add(new EntryItem("Item 4", "This is item 2.1"));
        items.add(new EntryItem("Item 5", "This is item 2.2"));
        items.add(new EntryItem("Item 6", "This is item 2.3"));
        items.add(new EntryItem("Item 7", "This is item 2.4"));

        items.add(new SectionItem("Category 3"));
        items.add(new EntryItem("Item 8", "This is item 3.1"));
        items.add(new EntryItem("Item 9", "This is item 3.2"));
        items.add(new EntryItem("Item 10", "This is item 3.3"));
        items.add(new EntryItem("Item 11", "This is item 3.4"));
        items.add(new EntryItem("Item 12", "This is item 3.5"));

        EntryAdapter adapter = new EntryAdapter(this, items);

        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        if(!items.get(position).isSection()){

            EntryItem item = (EntryItem)items.get(position);

            Toast.makeText(this, "You clicked " + item.title , Toast.LENGTH_SHORT).show();


        }

        super.onListItemClick(l, v, position, id);
    }
}
Item.java

public interface Item {

    public boolean isSection();

}
SectionItem.java

public class SectionItem implements Item{

    private final String title;

    public SectionItem(String title) {
        this.title = title;
    }

    public String getTitle(){
        return title;
    }

    @Override
    public boolean isSection() {
        return true;
    }

}
list_item_entry.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <ImageView
        android:id="@+id/list_item_entry_drawable"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@android:drawable/ic_menu_preferences"
        android:paddingLeft="9dp"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+id/list_item_entry_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+id/list_item_entry_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/list_item_entry_title"
            android:layout_alignLeft="@id/list_item_entry_title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:singleLine="true"
            android:textColor="?android:attr/textColorSecondary" />

    </RelativeLayout>

</LinearLayout>

list_item_section.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/list_item_section_text"
        layout="@android:layout/preference_category" />

</LinearLayout>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

您需要一个单独的可绘制项目数组,如下所示:

int[] img = { R.drawable.first, R.drawable.second, R.drawable.third,
        R.drawable.fourth};
并将它们添加到循环的列表中


希望这有帮助。

我想这将是困难的一部分。我能做我想做的事情吗?那可能吗?如何操作?@user45678您可以在if…else中检查数组中的当前位置,并相应地编码。但我不建议这样做。这将不会是优化的解决方案:)感谢神秘魔法。我想我必须为此添加位图类?我是新手。如果你不介意的话,你能给我举个例子吗?那真的很有帮助…很多regards@user45678检查教程,谢谢神秘魔法。我试过了,但在我的例子中,它不起作用,因为我多次将列表项的状态保存到共享首选项中,并多次重新绘制适配器。因此,唯一可能的情况是每次都将项目与项目一起保存在共享首选项中并检索回来。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
int[] img = { R.drawable.first, R.drawable.second, R.drawable.third,
        R.drawable.fourth};