Я пытаюсь установить другой ImageView в каждую строку моего списка. Все изображения хранятся в папке с моими ресурсами, поэтому я сохранил путь из папки активов в XML-файле. В моем коде ниже я устанавливаю заголовок и описание каждой строки в списке, но я не знаю, как установить конкретное изображение.Android - Установить различные ImageView для каждого элемента в списке
Ниже мой код
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Bundle getbundle = getArguments();
KEY_CATEGORY= getbundle.getString("id");
View v = inflater.inflate(R.layout.main, container, false);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = getXml("Images.xml"); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_MUSCLE, "Rs." + parser.getValue(e, KEY_MUSCLE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_MUSCLE, KEY_FILE }, new int[] {
R.id.name, R.id.desciption, R.id.cost, R.id.tvfilepath });
setListAdapter(adapter);
ListView lv = (ListView)v.findViewById(android.R.id.list);
return v;
}
и вот XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#dc6800"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<!-- Description label -->
<TextView
android:id="@+id/desciption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<!-- Linear layout for cost and price Cost: Rs.100 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Cost Label -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:gravity="left"
android:textStyle="bold"
android:text="Cost: " >
</TextView>
<!-- Price Label -->
<TextView
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"
android:gravity="left">
</TextView>
<TextView
android:id="@+id/tvfilepath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Посмотрите на этом уроке http://www.androidhive.info/2012/ 02/android-custom-listview-with-image-and-text/ – nedaRM
Попробовали ли вы мой ответ? конечно, это поможет вам. –
[попробуйте это] (http://androidcustomviews.com/portfolio/android-universal-image-loader/). Может быть, вы получите помощь. –