【问题标题】:Android LazyAdapter - displaying same data on two TextViews and an ImageViewAndroid LazyAdapter - 在两个 TextView 和一个 ImageView 上显示相同的数据
【发布时间】:2013-03-27 21:30:22
【问题描述】:

在我的 ListView 中有 2 个 TextView 和一个 ImageView。
我正在使用 LazyAdapter 根据this link 从 JSON 文件加载图像。

我已成功加载数据,但我面临的问题是,每个列表项都在两个 TextView 中显示图像的链接,而图像本身却正确显示。

这是适配器:
公共类 LazyAdapter 扩展 BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.item, null);

    TextView name = (TextView)vi.findViewById(R.id.name);
    TextView price = (TextView)vi.findViewById(R.id.price); 
    ImageView image=(ImageView)vi.findViewById(R.id.image); 

    HashMap<String, String> smart = new HashMap<String, String>();
    smart = data.get(position);

    id.setText(smart.get(SmActivity.KEY_ID));
    name.setText(smart.get(SmActivity.KEY_NAME));

    price.setText(smart.get(SmActivity.KEY_PRICE));
    imageLoader.DisplayImage(smart.get(SmActivity.KEY_THUMB), image);
    return vi;
}

}

这是活动:
公共类 SmActivity 扩展 ListActivity {

static String url = "url-php to get json";

public static String KEY_PRICE, KEY_NAME, KEY_THUMB;

ListView list;  
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayList<HashMap<String, String>> smList = new ArrayList<HashMap<String, String>>();

    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJson(url);

    try {
        JSONArray smJArray = json.getJSONArray("sm");
        for(int i = 0; i < smJArray.length(); i++){

        HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject jsonObj = smJArray.getJSONObject(i);

            map.put(KEY_NAME, jsonObj.getString("product_name"));
            map.put(KEY_PRICE, jsonObj.getString("price")+"€");
            map.put(KEY_THUMB, jsonObj.getString("image_main"));
            smList.add(map);       
        }   
    } 
    catch (JSONException e) {

        e.printStackTrace();
    }
    list = (ListView)getListView().findViewById(android.R.id.list);
    adapter=new LazyAdapter(this, smList);        
    list.setAdapter(adapter);
}

}

我可以理解 ListView 在所有项目中显示我正在使用的最后一个 map.put(...) 的值。
例如,如果 HashMap 中最后一个 put 是 map.put(KEY_PRICE, jsonObj.getString("price")+"€");
我不仅在价格上得到价格值,而且在名称 TextView 上也得到价格值。当然,我没有得到任何图像。

如有任何帮助,我将不胜感激。
提前致谢。

【问题讨论】:

  • 我建议验证放入哈希图中的数据(通过日志记录),然后在拉出时再次检查。一旦您可以证明您获取的地图中实际存在的数据,我们就可以缩小问题范围。
  • 感谢您的回复@Jon O。我对日志不熟悉,所以我不确定我是否正确使用它。但我测试了Log.d(KEY_THUMB, "testing ...");,没有显示任何日志错误。不使用其他键和 smList.toString()。
  • 我不确定这是否有帮助,但在 .xml 文件中,当我使用 android:id="@+id/list" 和活动 list = (ListView)findViewById(R.id.list); 时,应用程序意外终止。当我使用android:id="@android:id/list"(ListView)findViewById(android.R.id.list); 的行为就像我之前写的一样。

标签: android listview


【解决方案1】:

好的。我得到了它。我替换了:

public static String KEY_PRICE, KEY_NAME, KEY_THUMB;


static final String KEY_NAME = "product_name";
static final String KEY_PRICE = "price";

map.put("product_name", jsonObj.getString(KEY_NAME));
map.put("price", jsonObj.getString(KEY_PRICE));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多