簡體   English   中英

單個列表視圖項顏色更改

[英]Single listview item color change

我有一個3列(時間表)的listview 第一個是公共汽車離開車站的時間,第二個是公共汽車線路的描述,第三個是公共汽車離開車站的時間與系統時間之間的差。

所以我想更改通過的項目(總線)的顏色。 我知道listview有一個方法setBackgroundColor ,但它僅適用於所有項目。

有一些指定項目的方法嗎? 還是我必須更改適配器設置?

謝謝,

馬蒂亞

編輯:對我來說太復雜了:(我三周前才開始使用android。任何人都可以嘗試在此代碼中實現Bhaskar答案,所以也許我會得到它。

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

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

    /********** Display the headings ************/
    map1 = new HashMap<String, String>();
    map1.put("Polazak", "Polazak:");
    map1.put("Opis", "Napomena:");
    map1.put("Kreceza", "Krece za:");
    mylistglava.add(map1);

    try {
        adapterglava = new SimpleAdapter(this, mylistglava,
                R.layout.vrijemebus, new String[] { "Polazak", "Opis",
                        "Kreceza" }, new int[] { R.id.Polazak, R.id.Opis,
                        R.id.Kreceza });
        list_glava.setAdapter(adapterglava);
    } catch (Exception e) {
        e.printStackTrace();

    }

    /********************************************************/

    /********** Display the contents ************/

    for (int i = 0; i < bus.vrijeme.length; i++) {
        map2 = new HashMap<String, String>();
        map2.put("Polazak", bus.vrijeme[i]);
        map2.put("Krece za", kreceza[i]);
        if (i < bus.opis.length)
            map2.put("Opis", bus.opis[i]);
        else
            map2.put("Opis", " ");
        mylist.add(map2);
    }

    try {
        adapter = new SimpleAdapter(this, mylist, R.layout.vrijemebus,
                new String[] { "Polazak", "Opis", "Krece za" }, new int[] {
                        R.id.Polazak, R.id.Opis, R.id.Kreceza });
      list.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace();

    }
   list.setSelection(pozicija);

    /********************************************************/
}

再次感謝,Matija

您必須在Adapter getView()方法中執行此操作...

MyAdapter extends SimpleAdapter {
private ArrayList<Integer> coloredItems = new ArrayList<Integer>();

public MyAdapter(...) {
    super(...);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);

   //Here you can add your method/code to change the color.
    return v;
}

}

如果要為列設置顏色,還可以在listview項目xml中更改顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM