gpt4 book ai didi

android - ListView 问题 - 如何将图像从 drawable 放入 listView

转载 作者:行者123 更新时间:2023-11-29 22:21:22 26 4
gpt4 key购买 nike

我正在使用网络服务获取我需要的地方的数据。在 listView 中,我需要显示图像、地点名称、地点类型的图像和地址。我正在使用简单适配器。我知道如何填充文本,但不知道如何从可绘制文件夹中放置图像。第一张图片将按某种顺序排列(例如图片 A,第二张图片 B,第三张图片 C),第二张图片将取决于地点的类型(如果该地点是面包店,那么图片将是面包)等等。我转发到 HashMaps 的 SimpleAdapter ArrayList...任何人都可以给我一些想法或一些代码吗???

最佳答案

我想这个例子会对你有所帮助。

//主类

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Alertslistview extends Activity
{

private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;

public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}

public int getCount()
{
return alerts.length;
}

public Object getItem(int position)
{
return position;
}

public long getItemId(int position)
{
return position;
}

public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.alertslist, null);
holder = new ViewHolder();
holder.img =(ImageView) convertView.findViewById(R.id.img);
holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);

convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

holder.img.setImageResource(imgid[position]);
holder.text2.setText(alerts[position]);

return convertView;
}

static class ViewHolder
{
ImageView img;
TextView text2;
}}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(new EfficientAdapter(this)); }

private static final String[] alerts= {"Service Request EBH123H has been reassigned to Mohammed","Appointment Request for May 29th declined","New Survey - Metro Rail Survey available on your device"};

static final Integer[] imgid = {R.drawable.alert,R.drawable.alert,R.drawable.alert};
}

关于android - ListView 问题 - 如何将图像从 drawable 放入 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7093612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com