从ListAdapter更改图像

时间:2014-12-29 20:43:09

标签: android listview

我想使用ListAdapter

从res / drawable文件夹中调用图像

截至目前,我只能检索并以文字形式显示信息。

我的适配器如下

 runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ListagemHospitaisActivity.this, ListaHospitais,
                            R.layout.item_hospital, new String[] { TAG_PID,
                            TAG_NOME, TAG_LOTACAO},
                            new int[] { R.id.pid, R.id.nome, R.id.lotacao });

                    // updating listview
                    setListAdapter(adapter);

                }
            });

TAG_LOTACAO返回我想要显示的图像的确切名称。

我想改变的ImageView就是这个

 <ImageView
        android:id="@+id/imglotacao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/l_9_1"
        />

当我尝试下面的方法时,我遇到了一些问题。

private List<Hospitais> ListaHospitais; // or what type this list is

你是指ListaHospitais吗?

除此之外,<Hospitais>发出错误,并表示要创建Class。我做错了什么?

listData也发出错误,要求创建本地变量

此处它也显示错误,因为它无法解决方法。

Hospitais item = (Hospitais) ListaHospitais.get(position);
holder.pid.setText(item.getPid());
holder.nome.setText(item.getNome());

1 个答案:

答案 0 :(得分:2)

您应该创建自己的自定义适配器,如下所示:

public class HospitaisAdapter extends BaseAdapter {

    private List<Hospitais> ListaHospitais; // or what type this list is
    private Context context;

    private LayoutInflater layoutInflater;

    public HospitaisAdapter (Context context, List<Hospitais> ListaHospitais) {
        this.ListaHospitais= ListaHospitais;
        this.context=context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return ListaHospitais.size();
    }

    @Override
    public Object getItem(int position) {
        return ListaHospitais.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row, null);
         /*the row layout should be the layout for a row from your listview,
        containing your  imageView and nome as well as pid data.
        I will suppose pid and nome are Strings and are  displayed in a TextView */

            holder = new ViewHolder();
            holder.pid= (TextView) convertView.findViewById(R.id.pid);
            holder.nome= (TextView) convertView.findViewById(R.id.nome);
            holder.image= (ImageView) convertView.findViewById(R.id.imglotacao);

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

        Hospitais item = (Hospitais) ListaHospitais.get(position);

        holder.pid.setText(item.getPid());
        holder.nome.setText(item.getNome());

        holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.image_name));


        return convertView;
    }

    static class ViewHolder {
        TextView pid;
        TextView nome;
        ImageView image;

    }
}

而不是使用这样的适配器:

listView.setAdapter(new HospitaisAdapter(this,ListaHospitais));

希望这有帮助。