如何在ListView

时间:2015-11-06 01:17:55

标签: android listview android-studio

我已按如下方式填充了Arraylist:

JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
            String rs = "Rs. ";
            String name;

            for (int i = 0; i < result.length(); i++) {
                JSONObject jo = result.getJSONObject(i);
                String pname = jo.getString(Config.TAG_PCKG_NAME);
                String price = rs + jo.getString(Config.TAG_PCKG_PRICE);
                String Tag= "debugger2";
                Log.i(Tag, pname);
                HashMap<String, String> employees = new HashMap<>();
                employees.put(Config.TAG_PCKG_NAME, pname);
                employees.put(Config.TAG_PCKG_PRICE, price);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

现在我使用listadapter来填充listview,如下所示:

ListAdapter adapter = new SimpleAdapter(
                test.this, list, R.layout.list_item,
                new String[]{Config.TAG_PCKG_NAME, Config.TAG_PCKG_PRICE},
                new int[]{R.id.pname, R.id.price});
        listView.setAdapter(adapter);

我想现在为每个列表项都有不同的背景图像。我已经保存了图像(总共6个,因为列表中有6个项目)。我尝试做一个setbackground(R.drawable.filename)的事情,在for循环中拥有与变量i相关的所有文件名,但这不是这样做的方法。你能建议另一种方式吗? `

EDIT1: 这就是我现在实施的内容:

public class CustomAdapter extends SimpleAdapter{

    private ArrayList<HashMap<String, String>> results;
    private Context context;
    public CustomAdapter(Context context,
                           ArrayList<HashMap<String, String>> data, int resource,
                           String[] from, int[] to,int []image) {
        super(context, data, resource, from, to);
        this.results = data;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        View v = view;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);
        }
        v.setBackgroundResource(position);

        return v;
    }
}

问题:

我不知道setBackgroundResource将如何知道指向哪个数组;为此,我在customadapter减速中添加了int []图像,但仍不确定这将如何工作。

1 个答案:

答案 0 :(得分:0)

以下是实现simpleadapter的自定义适配器的最终代码

public class CustomAdapter extends SimpleAdapter{


    private ArrayList<HashMap<String, String>> results;
    private final int[] bcgImage;
    public CustomAdapter(Context context,
                           ArrayList<HashMap<String, String>> data, int resource,
                           String[] from, int[] to, int[] bcgImage) {
        super(context, data, resource, from, to);

        this.bcgImage = bcgImage;
        this.results = data;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        View v = view;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);
        }

        v.setBackgroundResource(bcgImage[position]);
        TextView name = (TextView) v.findViewById(R.id.pname);
        name.setText(results.get(position).get(Config.TAG_PCKG_NAME));
        TextView price = (TextView) v.findViewById(R.id.price);
        price.setText(results.get(position).get(Config.TAG_PCKG_PRICE));

        return v;
    }

}