我正在使用网络服务来获取我需要的地方的数据。在listView中,我需要显示图像,地方名称,地点类型和地址的图像。我正在使用Simple适配器。我知道如何填充文本,但不知道如何从文件夹drawable中放置图像。第一个图像将以某种顺序(如图像A,第二个图像B,第三个图像C),第二个图像将取决于一个地方的类型(如果该地方是面包店而不是图像将是面包)等等。我转发到了HashMaps的SimpleAdapter ArrayList ......有人可以给我一些想法或一些代码???
答案 0 :(得分:2)
我认为这个例子可以帮助你。
//主类
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};
}
答案 1 :(得分:0)
你必须从BaseAdapter创建一个扩展类,并将代码放在getview方法中,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.imagelayout, null);
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
iv.setImageResource(icons[position]);
} else {
v = convertView;
}
return v;
}
之前,创建包含ImageView的ImageLayout.xml,之后使用此类调整listview。
抱歉我的英文不好!