0
我正在構建一個應用程序,它將一些圖像和信息顯示到列表視圖中。 這裏是我的主要活動,無法將圖像從url添加到列表視圖
// All static variables
static final String URL = "http://starkgot.host56.com/first.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_DATE = "date";
static final String KEY_DESCRIPTION = "description";
static final String KEY_DIRECTOR = "director";
static final String KEY_CAST = "cast";
static final String KEY_video = "video";
static final String KEY_image = "image";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageview12=(ImageView)findviewbyid(R.id.item_icon);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// 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_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_DIRECTOR, parser.getValue(e, KEY_DIRECTOR));
map.put(KEY_CAST, parser.getValue(e, KEY_CAST));
map.put(KEY_video, parser.getValue(e, KEY_video));
map.put(KEY_image, parser.getValue(e, KEY_image));
// adding HashList to ArrayList
menuItems.add(map);
}
Picasso.with(this).load(KEY_image).into(imageview12)
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item, new String[] { KEY_NAME, KEY_DATE,
KEY_DESCRIPTION, KEY_DIRECTOR, KEY_CAST, KEY_video,KEY_image}, new int[] { R.id.name, R.id.date,
R.id.description, R.id.director, R.id.cast, R.id.video,R.id.image});//image not getting loaded here!!!
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String date = ((TextView) view.findViewById(R.id.date))
.getText().toString();
String description = ((TextView) view
.findViewById(R.id.description)).getText().toString();
String director = ((TextView) view.findViewById(R.id.director))
.getText().toString();
String cast = ((TextView) view.findViewById(R.id.cast))
.getText().toString();
String video = ((TextView) view.findViewById(R.id.video))
.getText().toString();
String image = ((TextView) view.findViewById(R.id.image))
.getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_DATE, date);
in.putExtra(KEY_DESCRIPTION, description);
in.putExtra(KEY_DIRECTOR, director);
in.putExtra(KEY_CAST, cast);
in.putExtra(KEY_video, video);
in.putExtra(KEY_image, image);
startActivity(in);
}
});
}
}
XML解析處理得當,並在列表中點擊也得當每個項目後顯示活動的第二類。 如何在我的列表視圖中添加圖像因子,就像名稱描述等其他因素一樣。
據我所知,你想通過一個圖像,因爲你傳遞名稱到下一個活動,我說得對嗎?如果我不知道,那麼你需要將URL路徑傳遞給圖像,就像你將其他所有附加信息傳遞給該圖像一樣,然後在另一個活動中,當接收到圖像URL時,將其重新分配到列表中,完成後通常使用適配器來處理列表視圖的所有內容。請從Vogella「http://www.vogella.com/tutorials/AndroidListView/article.html」中查看本教程,該教程解釋了很多關於列表視圖以及如何使用它們的信息。 – 2015-02-10 19:46:17
不,這不是我想要的,我想在此活動本身的列表視圖上顯示圖像。 – 2015-02-11 08:22:38
那麼,在你的活動中,存儲你想要顯示的信息,可能是一個帶有URL的ArrayList,或者類似的東西,然後用它作爲適配器來填充你的列表。 – 2015-02-11 14:25:44