android listview imageview,Android ImageView from ListView

这篇博客讲述了如何在Android应用中从自定义ListView传递图片URL到SingleMenuItemActivity,以便在点击列表项时展示图片。博客内容涉及到XML解析、ListView适配器以及Intent的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm trying to pass KEY_THUMB_URL to SingleMenuItemActivity.class to show Image onclick listview. I have marked those area //Not Working . Please correct my codings, I'm pretty new to android.

CustomizedListView.class

public class CustomizedListView extends Activity {

// All static variables

static final String URL = "http://api.androidhive.info/music/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);

ArrayList> songsList = new ArrayList>();

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

for (int i = 0; i < nl.getLength(); i++) {

// creating new HashMap

HashMap map = new HashMap();

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) {

String title = ((TextView) view.findViewById(R.id.title)).getText().toString();

String description = ((TextView) view.findViewById(R.id.artist)).getText().toString();

// Starting new intent

Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);

in.putExtra(KEY_TITLE, title);

in.putExtra(KEY_THUMB_URL, R.id.list_image); //Modified as told by Nandeesh

in.putExtra(KEY_ARTIST, description);

startActivity(in); }

});

}

}

SingleMenuItemActivity.class

public class SingleMenuItemActivity extends Activity {

static final String KEY_TITLE = "title";

static final String KEY_ARTIST = "artist";

static final String KEY_THUMB_URL = "thumb_url";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.single_list_item);

Intent in = getIntent();

String title = in.getStringExtra(KEY_TITLE);

String description = in.getStringExtra(KEY_ARTIST);

// int thumb_image = in.getIntExtra(KEY_THUMB_URL); // ERROR : The method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)

TextView lblName = (TextView) findViewById(R.id.name_label);

TextView lblDesc = (TextView) findViewById(R.id.description_label);

// ImageView thumb = (ImageView) findViewById(R.id.imageView1);

lblName.setText(title);

// thumb.setImageResource(thumb_image); //modified as told by Nandeesh

lblDesc.setText(description);

}

}

LazyAdapter.class

public class LazyAdapter extends BaseAdapter {

private Activity activity;

private ArrayList> data;

private static LayoutInflater inflater=null;

public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList> d) {

activity = a;

data=d;

inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

imageLoader=new ImageLoader(activity.getApplicationContext());

}

public int getCount() {

return data.size();

}

public Object getItem(int position) {

return position;

}

public long getItemId(int position) {

return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

View vi=convertView;

if(convertView==null)

vi = inflater.inflate(R.layout.list_row, null);

TextView title = (TextView)vi.findViewById(R.id.title); // title

TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name

TextView duration = (TextView)vi.findViewById(R.id.duration); // duration

ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

HashMap song = new HashMap();

song = data.get(position);

// Setting all values in listview

title.setText(song.get(CustomizedListView.KEY_TITLE));

artist.setText(song.get(CustomizedListView.KEY_ARTIST));

duration.setText(song.get(CustomizedListView.KEY_DURATION));

imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);

return vi;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值