当前位置: 代码迷 >> java >> Android自定义适配器部分工作
  详细解决方案

Android自定义适配器部分工作

热度:35   发布时间:2023-07-31 11:44:16.0

我有一个android应用,在主屏幕上可以使用网络服务,并在列表视图中显示结果。

问题:

  1. 仅拇指更新。 如果我使用简单的适配器,则一切正常。 如果使用自定义适配器,则仅更新拇指;

  2. 滚动时,每个列表视图都会更新。 我希望此列表视图在打开应用时更新一次。

MainActivity.java

// Lista de imóveis
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for(Integer i = 0, n = Referencias.size(); n > i; ++i)
{
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("referencia", "Imóvel " + Referencias.get(i));

    hm.put("valor", Valor.get(i).toString());
    hm.put("dormitorio", Dormitorio.get(i).toString());
    hm.put("vagas", Vagas.get(i).toString());
    hm.put("banheiro", Banheiro.get(i).toString());
    hm.put("subcategoria", Subcategoria.get(i).toString());
    hm.put("bairro", Bairro.get(i).toString());
    hm.put("img", Imagem.get(i).toString());
    aList.add(hm);
}

// Keys used in Hashmap
String[] from = {"referencia", "valor", "dormitorio", "vagas", "banheiro", "subcategoria", "bairro", "img"};

// Ids of views in listview_layout
int[] to = {R.id.referencia, R.id.valor, R.id.num_beds, R.id.num_garagens, R.id.num_baths, R.id.tipo, R.id.bairro, R.id.thumb};

MainAdapter CustomAdapter = new MainAdapter(MainActivity.this, aList, R.layout.list_main_imoveis, from, to);

ListView listView = (ListView)findViewById(R.id.listViewImoveis);
listView.setAdapter(CustomAdapter);

MainAdapter.java

package br.com.gtaimoveis.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.com.gtaimoveis.R;
import br.com.gtaimoveis.util.DownloadTask;

public class MainAdapter extends SimpleAdapter {
    private Context mContext;
    public LayoutInflater inflater = null;

    public MainAdapter(Context context,
                       List<? extends Map<String, ?>> data, int resource, String[] from,
                       int[] to) {
        super(context, data, resource, from, to);
        mContext = context;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.list_main_imoveis, null);

        HashMap<String, String> data = (HashMap<String, String>) getItem(position);


        Log.d(">>>>>", data.toString()); // << Data fine here

        new DownloadTask((ImageView) vi.findViewById(R.id.thumb))
            .execute((String) data.get("img").toString());

        return vi;
    }
}

谁能看到我要去哪里错了?

几乎现在我已经解决了问题1.在getView方法中,我更新了所有信息。

TextView Valor = (TextView) vi.findViewById(R.id.valor);
 Valor .setText(data.get("valor")); 

问题2尚未解决。

  相关解决方案