Search code examples
androidandroid-layoutandroid-listview

Updating ListView:ArrayAdapter in a timer


I am fairly new to android but not to programming. Have a good long years of embedded linux experience. What I am findin strange is that to do very simple tasks like updating a listview on a timer, I am not able to find any documentation and the source is so interleaved in pelothra of classes that I find it very strange to spend 2-3 days googling for a very basic thing updating a list in a timer. How hard it can be, but my bad luck I am not able to find a single sample for that..... Please Help.

What I trying to do is very simple, I am writing a Activity which has a timer, the timer reads a status value from a HTTP GET and displays it in a list.

First I created a ListView then attached an ArrayAdapter with 0 elements in that. In my timer I am doing the following

//Timer run code
public void run() {
    strng = GetStatus()         
    adapter.add(strng);
    adapter.notifyDataSetChanged();
}

But it is not at all working.

Can anyone please help me with a sample.... or someone at GOOGLE please put some samples of fairly basic things.

thanks


Solution

  • I managed to get it running though, here is a small code. Please have a look modify play with it. I would be creating a blog shortly to put these samples....

    Now that it is working to very basic extent, If I would like to modify it (A) How hard / easy is to replace the string at a particular position in the adapter. Right Now I am using ArrayAdapter which does not have any replace function. Why :( ? (B) If I have to create a service and let my service update the String values[]. what is the way to do it ? I read about intents but as service and activity are in same app, cant there be global data ? (C) and a point about java/android, why do you write function definition is a new call. Isn't it hard to debug and incorrect way to code ?

    public class ScreenOne extends Activity {
    protected ListView mylist;
    protected ArrayList<String> strlist;
    protected ArrayAdapter<String> adapter;
    protected TimerTask ttask;
    protected int ctr =0;
    protected Timer timer;
    
    protected String[] values = new String[] { "XYAndroid", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_one);
    
        mylist = (ListView) findViewById(R.id.s1ListView);
        strlist = new ArrayList<String>();
    
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strlist);
    
        mylist.setAdapter(adapter);
    
    
        Timer t = new Timer();
        //Set the schedule function and rate
        t.scheduleAtFixedRate(
                new TimerTask() 
                {
    
                    @Override
                    public void run()
                    {
                        runOnUiThread(
                                new Runnable() {
    
                                    @Override
                                    public void run() {
    
                                        if(ctr  < values.length) {
                                            adapter.add(values[ctr]);
                                            ctr++;
    
                                        }
                                    }
                                });
    
                    }
                }
                ,
                0,
                500);
    }