Hi I'm a new android developer. I try to change some element in my item once on click, this is my code
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageView img = findViewById(R.id.imageView2);
img.setImageResource(R.drawable.validate);
TextView txtNumTicket, txtCaisse;
txtNumTicket = findViewById(R.id.ref);
txtCaisse = findViewById(R.id.textView);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
Toast.makeText(getApplicationContext(), "You have selected item no."
+ (i + 1) + "", Toast.LENGTH_SHORT).show();
}
});
I wanna return to the initial state (change image to R.id.novalidate and color textview) when position equal 6, How can I do it ?
UPDATED after comment:
if (yourListView.getAdapter().getCount() == 6) {
img.setImageResource(R.drawable.novalidate);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
}
2ndUPDATE: Based on this answer , here's how you can get a view in a specific position of your list
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
So you could get the view you wanted by calling:
View view = getViewByPosition (number_of_the_view_you_want_to_change, name_of_your_listview);
Then bind the textviews/imageviews of that item (view.findViewById(...)) and set the colours and text as described above
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.