Thread not updating ListView

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

Thread not updating ListView

Postby williamzanelli » Wed May 06, 2009 1:11 am

Hi guys,

I;m trying to update my view dynamically using threads.. unfrtunately its not working as it should.

I;m following this http://developer.android.com/guide/appe ... #threading and this the_pizza_timer_-_threading-drawing_on_canvas-t126.html

Here's my code

Code: Select all
public class MainThread extends Activity {

     Thread myRefreshThread = null;
   
     static MyViewClass myView = null ;
   
     /* The Handler that receives the messages
      * sent out by myRefreshThread every second */
     
     
     Handler myViewUpdateHandler = new Handler(){
         
        public void handleMessage(Message msg) {
       
if (msg == someVar){
        myView.setDataList(this.getData());
        myView.invalidate();
        super.handleMessage(msg);
        }
        }   
     };
       
 
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
               
        this.myView = new myView (this);
        this.myView.setDataList(MainThread.getData());
        setContentView(this.myView);
       
        this.myRefreshThread = new Thread(new secondCountDownRunner());
        this.myRefreshThread.start();
    }
   
     
     class secondCountDownRunner implements Runnable{
          // @Override
          public void run() {
               while(!Thread.currentThread().isInterrupted()){
                    Message m = new Message();
                   
                    MainThread.this.myViewUpdateHandler.sendMessage(m);
                    try {
                         Thread.sleep(10000);
                    } catch (InterruptedException e) {
                         Thread.currentThread().interrupt();
                    }
               }
          }
    }
     
     public static List getData()
     {
....
....
    returns data;
     }
     


MyView class is as follow

Code: Select all

public class MyView extends ListView {

   List data = null;
   String tag;
   ArrayAdapter<String> adapter;
   TextView title;
   ListView itemlist;

   public MyView (Context context) {
      super(context);
      
      this.title = new TextView(context);
      this.itemlist = new ListView(context);

      if (data == null) {
         
         title.setText("No Data Available...");

      }

      title.setText("Some text...");
      

      List xyz = (ArrayList) getDataList();

      Log.i(tag, "item szie! [" + xyz.size() + "]");

      ArrayAdapter adapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_list_item_1, xyz);

      this.setAdapter(adapter);
      
      

   }

   public List getDataList() {
      return data;
   }

   public void setDataList(List mList) {
      this.data = mList;
   }



Now the ListView displays the list., but if it changes, it doesnt update it for some reason... any ideas...?

Thanks in advance for your thoughs

Will
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Top

Postby nmc » Wed May 06, 2009 6:22 am

Try replacing
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myView.invalidate();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

by
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myView.adapter.notifyDataSetChanged();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby williamzanelli » Thu May 07, 2009 12:12 am

NMC, thanks for the reply.

I tried your suggestion, the line below

Code: Select all
myView.adapter.notifyDataSetChanged();


throws a NullPointer Exception.

Any ideas why??

Thanks for your help thus far!!

Will
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Postby nmc » Thu May 07, 2009 10:14 am

The LogCat hasnt any usefull information for you?
Since this function is known to work, there must be something with the data you provide.

My approach on this is to extend BaseAdapter with a data-holder-class and then do a list.setAdapter(myadapter) once, followed by subsequent calls to myadapter.notifyDataSetChanged();

Maybe you should try this, too?
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby williamzanelli » Thu May 07, 2009 4:07 pm

Hey NMC,

Thanks for your response.

The logCat says "NullPointerException" at this line myView.adapter.notifyDataSetChanged();

I'm going to attempt your method.

Do you have Sample code?

The reason I've refrained from your method thus far has been becase my class is the view, it extends ListView - I;m not entirely sure where to place the ListView if i extend by BaseAdapter.

Thanks for your help

Will
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Postby nmc » Fri May 08, 2009 9:50 am

Nullpointer:
did you set a break point to that line to determine wether 'myview' and 'adapter' have plausible values?

Sample code:
sdk1.0samplesApiDemossrccomexampleandroidapisviewlist4.java is an example of extending BaseAdapter for a ListView
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Postby williamzanelli » Fri May 08, 2009 6:09 pm

Thanks for the messages NMC.

I still havent got my head round this one.

I tried your appraoch, but its alwasy saying i have a NULL POINTER. I think its down to me missing some static value.

I re-tried my approach, as listed above, i relaised I missed something, I didnt addView.. so I did this.. it works now, as in in the logcat I can see the data being retrieved, howvere I still get a blank screen.

What I'm trying todo is.. retrieve some external data, which is initially XML, converted to string and stored into a List, I want to check every 10 minutes or so to see if the data has been updated, then uodate my view data.

Sounds simple enough.. been stuck on it for weeks...! :(

Thanks for your help.

Will
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests