在followimg類中,我使用散列表導入了一個拇指圖像,該圖像保存爲一個字符串。但是,我希望將此圖像通過一個意圖傳遞給一個列表視圖,即一個詳細視圖。如何在詳細的活動中將s_thumb字符串添加到 imageview? 這裏是主要的活動,設置了底部的意圖:如何將圖像從Hashmap url添加到列表視圖項目中?
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://padihamcars.com/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
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_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = songsList.get(position);
String s_thumb=resultp.get(KEY_THUMB_URL);
String s_title=resultp.get(KEY_TITLE);
Intent intent = new Intent(CustomizedListView.this, SingleListItem_Attractions.class);
intent.putExtra("s_id", s_thumb);
intent.putExtra("s_title", s_title);
//other fields you want to send
startActivity(intent);
}
});
}
這裏是我詳細的活動(單項):
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;
public class SingleListItem_Attractions extends Activity{
Button button;
Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
ImageView imageView = (InageView) findViewById(R.id.imageView1);
Intent i2 = getIntent();
String text2 =i2.getStringExtra("s_thumb");
imageView.setImageBitmap(text2);
TextView texts = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
String text1 =i.getStringExtra("s_title");
texts.setText(text1);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, CustomizedListView.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, CustomizedListView.class);
startActivity(intent);
}
});
}
}
的問題在於ImageView的努力是由一個字符串設置即text2。 從text2中的url字符串設置imageview的正確方法是什麼? 在此先感謝!
如果你有一個圖像的網址,你可以使用這些'ImageLoaders' - [Glide](https://github.com/bumptech/glide)或[畢加索](http://square.github。 IO /畢加索/)。 – Wizard