[Anfänger Frage] ListView, jede Zeile andere Farbe

K

Karuso33

Neues Mitglied
0
Hallo,

ich habe vor kurzem einen "eigenen" ListView Adapter aufgesetzt.
Code:
        SimpleAdapter adapter = new SimpleAdapter(
        		this,
        		list,
        		R.layout.main_list_item_layout,
        		new String[] {"subject","grade","teacher"},
        		new int[] {R.id.maintvTxt1,R.id.maintvTxt2, R.id.maintvTxt3}
        		);

Um dann mithilfe eine Routine, wie etwa dieser hier
Code:
    	HashMap<String,String> temp = new HashMap<String,String>();
    	temp.put("subject","Deutsch");
    	temp.put("grade", "3-");
    	temp.put("teacher", "Silver, Grey, Black");
    	list.add(temp);
Einträge hinzuzufügen.

Hier noch meine xml Datei aus der die Felder maintvTxt1,2,3 kommen:
Code:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
 	android:weightSum="100">
    <LinearLayout
        android:layout_width="fill_parent"
        android:weightSum="100"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
		<TextView android:id="@+id/maintvTxt1"         
		  	android:textSize="16sp"         
		  	android:textStyle="bold"    
		  	android:layout_width="fill_parent"
		  	android:layout_weight="20"         
		  	android:layout_height="fill_parent"/>
		<TextView android:id="@+id/maintvTxt2"         
		  	android:textSize="20sp"
		  	android:layout_gravity="left"
		  	android:gravity="left"
		  	android:textColor="#B2B2B2"     
		  	android:paddingRight="12dp"
		 	android:layout_weight="80"      
		  	android:layout_width="fill_parent"         
		  	android:layout_height="fill_parent"/>
	</LinearLayout>
	<TextView android:id="@+id/maintvTxt3" 
		android:typeface="sans"
		android:textSize="14sp"
		android:textStyle="italic"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"/>
</LinearLayout>

So, nun meine Frage: wie kann ich diese TextView jeweils anders Farbig gestalten, spich Zeile 1 das maintvTxt1 z.B. rot und Zeile 2 z.B. gelb


Danke im Vorraus

Karuso33
 
Zuletzt bearbeitet:
Ich kann dir zwar leider nicht helfen, hab aber genau das gleiche Problem. Wäre auch über ne Lösung von jemandem dankbar.
 
Naja, ist eigentlich nicht so schwer.

Euer Adapter sollte ja eine Methode "getView()" implementieren.

Dort müsst ihr die Farbe der View eben anpassen. etwa so:

Code:
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view = (TextView)convertView;
        if(null == view) {
            view = new TextView(_context);
        }
        view.setBackgroundColor(position%2==0?Color.RED:Color.BLACK);
 
  • Danke
Reaktionen: Garnet

Ähnliche Themen

D
Antworten
17
Aufrufe
391
datNeMo
D
A
Antworten
10
Aufrufe
1.016
swa00
swa00
M
Antworten
2
Aufrufe
626
Mozart40
M
Zurück
Oben Unten