Refresh ImageView onClick()

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Refresh ImageView onClick()

Postby Exeter » Thu Jan 14, 2010 4:33 am

Hi,

Below is a snippet from a Gridview that gets populated with Icons.

onClick updates the underlying database & repopulates the Integer Array.

*** Noob Questions ***
1. How do I refresh the ImageView to show the changes?
2.
a. The GridView containing the ImageView will have 10-100 icons. (ie Refreshing the entire GridView is expensive.)
b. Is it possible to limit refresh to the affected ImageView only?


Cheers,


Exeter

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public View getView(final int position, View convertView, ViewGroup parent) {
  5.  
  6.       ImageView imageview;
  7.  
  8.  
  9.  
  10.          /** Called when the activity is first created. */    
  11.  
  12.         if (convertView == null) {  // if it's not recycled, initialize some attributes
  13.  
  14.  
  15.  
  16.  
  17.  
  18.             imageview = new ImageView(mContext);
  19.  
  20.             imageview.setLayoutParams(new GridView.LayoutParams(30, 30));
  21.  
  22.             imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
  23.  
  24.             imageview.setPadding(0, 0, 0, 0);
  25.  
  26.             imageview.setEnabled(true);
  27.  
  28.  
  29.  
  30.  
  31.  
  32.         imageview.setOnClickListener(new View.OnClickListener() {
  33.  
  34.  
  35.  
  36.               //@Override
  37.  
  38.               public void onClick(View view){
  39.  
  40.                   IconProvider iconprovider = new IconProvider();
  41.  
  42.                   iconprovider.open();
  43.  
  44.                             Log.d(TAG, "onClick uri = " + uri );
  45.  
  46.  
  47.  
  48.          
  49.  
  50.                             //Update Row Via Content Provider
  51.  
  52.                             //=============================
  53.  
  54.                             ContentValues editedValues = putValues();
  55.  
  56.                             iconprovider.update(uri, editedValues, null, null);
  57.  
  58.  
  59.  
  60.                              //Repopulate Integer[] Array Via Content Provider
  61.  
  62.                             //=============================
  63.  
  64.                            Integer[] mThumbIds = getThumbIds(uri);
  65.  
  66.  
  67.  
  68.                                
  69.  
  70.                         //Isolate UI to redraw only current ImageView
  71.  
  72.                         //=============================
  73.  
  74.                         int rectTop = view.getPaddingTop() - view.getPaddingTop();
  75.  
  76.                         int rectBottom  = view.getBottom() - view.getPaddingBottom();
  77.  
  78.                         int rectLeft = view.getLeft() - view.getPaddingLeft();
  79.  
  80.                         int rectRight = view.getRight() - view.getPaddingRight();
  81.  
  82.                             Rect rect = new Rect(rectTop ,rectBottom,rectLeft, rectRight );
  83.  
  84.                             view.getFocusedRect(rect);
  85.  
  86.           Log.d(TAG, "Rect |" + rect.top + "," + rect.bottom + "," + rect.left + "," + rect.right);
  87.  
  88.                             view.invalidate(rect);
  89.  
  90. ;
  91.  
  92.                 }
  93.  
  94.  
  95.  
  96.                           iconprovider.close();
  97.  
  98.                     }
  99.  
  100.                 });
  101.  
  102.           }
  103.  
  104.           else {
  105.  
  106.  
  107.  
  108.             imageview = (ImageView) convertView;
  109.  
  110.           }
  111.  
  112.         imageview.setImageResource(mThumbIds[position]);
  113.  
  114.         return  imageview;
  115.  
  116.  
  117.  
  118.     }
  119.  
  120.  
  121.  
  122.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Top

Postby icobasco » Fri Jan 15, 2010 10:10 am

Hi,

you can do that (without refreshing the whole grid) e.g. using selectors.

e.g.: you have a grid of icons which can represent buttons (with pressed/not_pressed status) each.
In the grid cell you put an ImageView with a selector as background, so the press and click events are passed from the grid to the imageview.
So the imageview decides by itself which drawable to show following the events.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Selector

Postby Exeter » Fri Jan 15, 2010 8:52 pm

Hi icobasco,

Thx for getting back to me. I'll look into it.


Cheers,

Exeter
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Postby icobasco » Fri Jan 15, 2010 8:56 pm

Hi Exeter,
you are welcome.

I hadn't the time to show you an example with code, sorry.

If you have any further question, feel free to ask.
I'm not sure to be able to answer to everyone of your doubts :P ehehhe... but I'll try.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Selector

Postby Exeter » Fri Jan 15, 2010 9:49 pm

icobasco.


Will do thx. FYI I found this code snippet last night while reading up on my problem. I'm busy trying to adapt it into a GridView solution - I will definitely have some questions (esp when it comes to XML layouts). :)

Image Toggler
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.  public void onClick(View v, ViewHolder viewHolder) {  
  5.  
  6.                      // we toggle the enabled state and also switch the icon  
  7.  
  8.                      MyViewHolder mvh = (MyViewHolder) viewHolder;  
  9.  
  10.                      MyData mo = (MyData) mvh.data;  
  11.  
  12.                      mo.enable = !mo.enable; // toggle  
  13.  
  14.                      ImageView icon = (ImageView) v;  
  15.  
  16.                      icon.setImageBitmap(  
  17.  
  18.                              mo.enable ? ClickableListItemActivity.this.mIconEnabled  
  19.  
  20.                                      : ClickableListItemActivity.this.mIconDisabled);
  21.  
  22.                  }  
  23.  
  24.              });  
  25.  
  26.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Full Code for Clickable ListView Items
http://www.androidsnippets.org/snippets/125/


Thanks Again,


Exeter
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Postby icobasco » Sat Jan 16, 2010 9:37 am

I think it could be another way to do the same thing... but I'm not sure cause I already used the selector way and this one is new for me.
Thank to have shared it here :)
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests