GridView with ArrayAdapter not refreshed

Put your problem here if it does not fit any of the other categories.

GridView with ArrayAdapter not refreshed

Postby sjwright » Wed Jan 09, 2008 4:26 pm

I have a GridView that uses an ArrayAdapter to populate the data in the grid. The ArrayAdapter connects to a list of Strings. When the list is updated, I invoke the invalidate() method on the GridView to make it show the latest data in the list.

But the GridView is not redisplaying the data. I've also tried forceLayout() and postInvalidate(). I've also tried executing setAdapter() again, but that causes an exception.

I've attached a simplified example of what I'm doing. What have I done wrong?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.atest;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.GridView;
  12. import android.widget.LinearLayout;
  13. import android.widget.LinearLayout.LayoutParams;
  14.  
  15.  
  16. public class TestGrid extends Activity implements OnClickListener {
  17.  
  18.         protected ArrayList<String> nums = new ArrayList<String>(3);
  19.         protected GridView listHands = null;
  20.        
  21.         /** Called when the activity is first created. */
  22.         @Override
  23.         public void onCreate(Bundle icicle) {
  24.                 super.onCreate(icicle);
  25.  
  26.                 for (int x=0; x < 3; x++) {
  27.                         nums.add("0");
  28.                 }
  29.                
  30.                 setContentView(R.layout.gridview);
  31.                
  32.                 LinearLayout numbers = (LinearLayout) findViewById(R.id.grid);
  33.  
  34.                 // Set width, height (-1=fill parent), weight
  35.                 LayoutParams params = new LinearLayout.LayoutParams(
  36.                                 LinearLayout.LayoutParams.WRAP_CONTENT, 30, 1);
  37.  
  38.                 this.listHands = new GridView(this);
  39.                
  40.                 listHands.setId(222);
  41.  
  42.                 listHands.setNumColumns(3);
  43.                 listHands.setLayoutParams(params);
  44.                 listHands.setAdapter(new ArrayAdapter<String>(this,
  45.                                 android.R.layout.simple_list_item_1_small, this.nums));
  46.                
  47.                 numbers.addView(listHands);
  48.                
  49.                 ((Button) findViewById(R.id.subBtn)).setOnClickListener(this);
  50.                
  51.         }
  52.  
  53.         /**
  54.          * update the list after every SUBMIT click
  55.          */
  56.         public void onClick(View arg0) {
  57.                 // update the list
  58.                 long t = System.currentTimeMillis();
  59.                
  60.                 for (int x=0; x < 3; x++) {
  61.                         this.nums.set(x, String.valueOf(t + x));
  62.                 }
  63.                
  64.                 this.listHands.invalidate();
  65.         }      
  66.  
  67.  
  68.  
  69. }
  70.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Here's the XML for the screen
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout id="@+id/widget24" android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent" android:background="#ffffffff"
  4.         xmlns:android="http://schemas.android.com/apk/res/android">
  5.  
  6.         <LinearLayout id="@+id/grid"
  7.                 android:layout_width="wrap_content" android:layout_height="50px"
  8.                 android:orientation="vertical" android:scrollbars="vertical" />
  9.  
  10.         <Button id="@+id/subBtn" android:layout_width="wrap_content"
  11.                 android:layout_height="wrap_content" android:text="SUBMIT"
  12.                 android:textSize="12sp" android:layout_below="@+id/grid">
  13.         </Button>
  14.  
  15. </RelativeLayout>
  16.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Steve
sjwright
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 08, 2008 9:45 pm

Top

Postby plusminus » Fri Jan 11, 2008 7:15 pm

Hello sjwright,

possibly a Adapter is not the right thing to choose for dynamic fields.

:idea: You could do it the same way as I did it in the :src: Weather-Forecast-Tutorial, where I created Custom Views, to show Weather-Icons and Temperature-TextView. (I did put them to TableLayout).
Then you can ensure, that the Views get updated with a invalidate(); call, because you can simply override it, even a call to setText(...); should be enough to make it redraw itself.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 20 guests