Farbe eines selektierten Items ändern

S

spike78

Neues Mitglied
0
Hallo zusammen,

ich habe folgendes Problem. Ich habe mir eine Liste erstellt, deren Items aus einem ImageView und einem TextView bestehen. Die Hintergrundfarbe der Liste wollte ich weiß machen. Das Problem das ich nun habe, ist das man nicht mehr erkennen kann ob man ein Listitem angeklickt hat.

Ich hoffe ich versteht was ich meine, ich wüßte nicht wie ich es anders erklären kann. Ich hätte gerne den selben Effekt, als ob man einen Button drückt.

Danke und Gruß

Michael
 
Hi,

danke für den Link. Da ich aber noch ein ziemlicher Neuling bin, könntest du mir da ewas genauer helfen??

Hier mal mein bisheriger Code (layout):

Code:
[SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080][LEFT]<[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]LinearLayout[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]xmlns:android[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"http://schemas.android.com/apk/res/android"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_width[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fill_parent"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_height[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fill_parent"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:orientation[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"horizontal"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]>[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
[/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]<[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]ImageView[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:id[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"@+id/iv_main_icon"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_width[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"wrap_content"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_height[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"wrap_content"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:src[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"@drawable/icon"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]/>[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]<[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]LinearLayout[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 
[/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:orientation[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"vertical"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_width[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fill_parent"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_height[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fill_parent"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]>[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
[/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]<[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]TextView[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:id[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"@+id/tx_main_inhalt"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_width[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"wrap_content"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_height[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"wrap_content"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]android:layout_weight[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"1"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#7f007f][SIZE=2][COLOR=#7f007f]style[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][I][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"@style/TextNormal"[/LEFT]
[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]/>[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]</[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]LinearLayout[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]>[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/LEFT]
[/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]</[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f7f][SIZE=2][COLOR=#3f7f7f]LinearLayout[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]>
[/COLOR][/SIZE][/COLOR][/SIZE]
 
du benutzt aber ein ListView zum Anzeigen der Items?
 
Ja. Wenn ich die Hintergrundfarbe nicht ändere, habe ich den Klick-Effekt. Wenn ich die aber z.B. Weiß mache, ist er weg.

hier noch meine Java Klasse.

public​
class KochbuchActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(
new MyCustomAdapter(this,
R.layout.
main,
getResources().getStringArray(R.array.
inhalt)));
}

public class MyCustomAdapter extends ArrayAdapter<String> {

private View row;

public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

//return super.getView(position, convertView, parent);

LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.main, parent, false);

//Startseite

TextView label_startseite=(TextView)​
row.findViewById(R.id.tx_main_inhalt);
label_startseite.setText(getResources().getStringArray(R.array.
inhalt)[position]);


//Image

ImageView icon=(ImageView)​
row.findViewById(R.id.iv_main_icon);

if (getResources().getStringArray(R.array.inhalt)[position].equals("Fleisch")){
icon.setImageResource(R.drawable.
fleisch);
}
else if(getResources().getStringArray(R.array.inhalt)[position].equals("Suppen und Eintöpfe")){
icon.setImageResource(R.drawable.
eintopf);
}
else if(getResources().getStringArray(R.array.inhalt)[position].equals("Lyonergerichte")){
icon.setImageResource(R.drawable.
lyoner);
}
else if(getResources().getStringArray(R.array.inhalt)[position].equals("Kartoffelgerichte")){
icon.setImageResource(R.drawable.
kartoffeln);
}
else if(getResources().getStringArray(R.array.inhalt)[position].equals("Salate")){
icon.setImageResource(R.drawable.
salat);
}
else if(getResources().getStringArray(R.array.inhalt)[position].equals("Süßes")){
icon.setImageResource(R.drawable.
suesses);
}
return row;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);

String selection = l.getItemAtPosition(position).toString();
Toast.makeText(​
this, selection, Toast.LENGTH_LONG).show();
}

}
 
es wurde dir ja schon gesagt, dass du statedrawables nehmen musst.
auch wenn du anfänger bist, wird kein weg daran vorbei führen, sich damit zu beschäftigen.

es gibt da viele beispiele, die sehr leicht verständlich sind.
 
Hi,

bitte nicht falsch verstehen. Ich beschäftige mich schon seit einiger Zeit recht intensiv damit. hab auch schon einige Beispiele abgearbeitet. Nur das Thema Listen macht mir noch recht viele Probleme...

Bisher konnte ich auch jedes Problem lösen ohne einen Post nur damit wollte es nicht klappen.

Werde mir statedrawables ansehen.

Danke und Gruß

Michael
 
naja grade zu den ganzen problemen mit listen gibt es mittlerweile zirka 10 000 000 000 beispiele und lösungen :D

wenn du dich selbst recht gut einlesen kannst, dann sollten die statedrawables auch kein problem sein. ist nicht sonderlich kompliziert :D
 

Ähnliche Themen

D
Antworten
14
Aufrufe
1.715
chrs267
chrs267
J
Antworten
5
Aufrufe
930
swa00
swa00
C
  • Chicken Wing
Antworten
4
Aufrufe
856
Chicken Wing
C
Zurück
Oben Unten