0

What I've done:


Hello Guys, I've got a GrindView which i filled over my Database. Now the Images shows up there and are scrollable. But when I click on it, they don't keep selected. To see which picture is selected I builded a Toastmessage which shows me what picture i clicked on with the onIemClickListner.

Question:


I'd like to let the Clicked Image, selected. So that you click on time on a Image in the GridView and afterwards it keeps selected. How can i realize that? I would be happy if there's a tutorial or a codesample, to see how it works.

The Code


This is what i tried that only the actual image gets keeps selected with the colors but it dosn't work the right way, sometimes it just changes..

//Hier wird gemerkt welches bild
        gridview.setOnItemClickListener(new OnItemClickListener() {

        boolean color = false;
            View old;
            View v;

            public void onItemClick(AdapterView<?> parent, View vv, int position, long id) {
                Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show();

                v = vv;

                //gridview.setSelection(position);

               if (color == false){
                v.setBackgroundColor(0xFF00FF00);
                old = v;
                color = true;

               }

               else {
                   old.setBackgroundColor(0x00000000);
                   v.setBackgroundColor(0xFF00FF00);

                   color = false;

               }
            }
        });

Thx for you anwser in advance

safari

safari
  • 7,565
  • 18
  • 56
  • 82

2 Answers2

1

After you call setAdapter()

do it like this

setSelection(setSelected, true)

  • ok it worked, but how can i set a background color when it keeps selected... (+1 btw for the first working part, if that with the color works to you get a green tick) – safari Oct 05 '11 at 12:28
  • you want to change the color of selected image ?? –  Oct 05 '11 at 12:29
  • in your onItemClickListener()...do .setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { view.setBackgroundColor(color); } –  Oct 05 '11 at 12:32
  • oh dear its that easy? damnt! – safari Oct 05 '11 at 12:37
  • so now it works if i click on it the color changes, but it keeps there even i select an other one, any help? – safari Oct 05 '11 at 12:44
0

Following is the working code.

            if (color == false){
                vv.setBackgroundColor(getResources().getColor(R.color.green));
                old = vv;
                color = true;
            } else {
                old.setBackgroundColor(getResources().getColor(R.color.white));
                vv.setBackgroundColor(getResources().getColor(R.color.green));
                old=vv;
            }
Nishant Mendiratta
  • 760
  • 13
  • 25