0

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 ?

IZM
  • 3
  • 1
  • Note that if you are using `findViewById()` in Activity context, then it would return a view from the main view, e.g. the one you declare with `setContentView` and **not** the clicked item view. If you want to edit the clicked item, you would need to modify your `Adapter` class, because the views in the `ListView` are reused through the second parameter in the [getView](https://developer.android.com/reference/android/widget/Adapter.html#getView(int,%20android.view.View,%20android.view.ViewGroup)) method – Nikita Kurtin Feb 06 '19 at 16:25
  • It work good for me, but I can't edit it programmatic without using setOnItemClickListener, just I know her position and i wanna make the change secondly – IZM Feb 06 '19 at 16:32

1 Answers1

0

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

Nikos Hidalgo
  • 3,094
  • 9
  • 17
  • 30
  • If i Wanna make a method for that ? I wanna call it in other context – IZM Feb 06 '19 at 16:26
  • @IZM Do you want a method that just resets the views or a method that resets the views every time the item in position 6 is clicked? In the former case just wrap the code inside the check in a method and call it at any point after you bind the views. In the latter, you don't need to call a method elsewhere as the onclicklistener will always trigger when you click on the list, no matter where you are in your code. – Nikos Hidalgo Feb 06 '19 at 16:32
  • I want a method to reset the views whenever the elements in the list equals 6. Since the list is dynamic – IZM Feb 06 '19 at 16:37
  • @IZM My misunderstanding, sorry. So which view do you want to check? Is it the txtNumTicket? Every time this has its text set to "6" you want the views to reset? – Nikos Hidalgo Feb 06 '19 at 16:41
  • I appreciate your help sir. So each time an item is added to the list. when i have 6 items in my listview, i want to access eg item with position 1 and change img, txtNumTicket, txtCaisse. – IZM Feb 06 '19 at 16:55
  • @IZM I still am not sure I understand. What is this 6? Is it the number 6 as a text in your list somewhere? Is it the total number of list items? Something else altogether? – Nikos Hidalgo Feb 06 '19 at 16:58
  • 6 is items number. – IZM Feb 06 '19 at 17:04
  • if total item is 6 – IZM Feb 06 '19 at 17:14
  • if (yourListView.getAdapter().getCount() == 6) I wanna access in just one item and modify apply this (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));).How can I modify into just one item ? – IZM Feb 07 '19 at 11:45
  • Thank you Nikos Hidalgo – IZM Feb 07 '19 at 13:49
  • @IZM glad I could help – Nikos Hidalgo Feb 07 '19 at 14:19