我有一个ListView示例,其中listview中的图像来自XML。我想为静态列表项添加不同的静态图像。我想添加类似“items.add(new EntryItem(”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);
}
}
EntryAdapter.java
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
if (title != null)
title.setText(ei.title);
if(subtitle != null)
subtitle.setText(ei.subtitle);
}
}
return v;
}
}
EntryItem.java
public class EntryItem implements Item{
public final String title;
public final String subtitle;
public EntryItem(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
}
@Override
public boolean isSection() {
return false;
}
}
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>
答案 0 :(得分:0)
您将需要一个单独的可绘制项目数组,如下所示:
int[] img = { R.drawable.first, R.drawable.second, R.drawable.third,
R.drawable.fourth};
将它们循环添加到列表中。
希望这有帮助。