2015-11-06 45 views
0

我已填充一個ArrayList如下:如何爲每個項目不同的背景中的ListView

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 []圖像,但仍不確定這將如何工作。

+0

定製適配器最終代碼,您應該看一看這一個響應http://stackoverflow.com/a/8166802/5215998 – Kassisdion

+0

創建SimpleAdapter'的'子類,重寫'getView()',鏈接到超類實現以獲取所有現有行爲,修改適合的背景並返回修改後的行'View'。然後,在你的'ListView'中使用'SimpleAdapter'的子類。 – CommonsWare

+0

聲明像這樣的圖像Integer [] imgid = {R.drawable.pic1,R.drawable.pic2};並使用此語句在自定義適配器中獲取圖像layout.setBackgroundResource(imgid [position]); // layout =你的list_item的父級佈局 – Nisarg

回答

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; 
    } 

} 
相關問題