Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Updating ListView

I have looked at Android - how to update ListView item that is currently displayed and http://commonsware.com/Android/excerpt.pdf and the Android documentation but I still don't understand.

My problem:
Using a handler, I am trying to update a Stock data multi-column listview which I have populated from a webservice which retrieves data from a MySQL database. To update the listview, I am calling a servlet which returns an XML that I loop through using DOM.

I cannot find a working way to apply the new data (from the XML) into the Listview, though only the third column (Trade Column) has to be updated. Also when I try to cast a View from a ListView row, I get a NullPointerException and can't figure out why.

The code I have done so far is below.

java Code:

private void updateUI() throws Exception
{        
    Date dt = new Date();                 
    int hours = dt.getHours();
    int minutes = dt.getMinutes();                 
    int seconds = dt.getSeconds();                 
    String curTime = hours + ":" + minutes + ":"+ seconds;

    refreshHandler.sleep(60000);

    ListView listview = (ListView) findViewById(R.id.listview); 

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse("http://10.0.0.29:8080/CI3500/FTSEXML");

    //Filter and store ALL 'update' XML elements into node array
    NodeList nodeList = doc.getElementsByTagName("update");
    View v = null;
    TextView t = null;

    Adapter adapter = listview.getAdapter();

    for (int i = 0; i < adapter.getCount(); i++) 
    {
        v = listview.getChildAt(i);
        t = (TextView)v.findViewById(R.id.item2);

        String companyCode = t.getText().toString(); //Column 1

        for(int j = 0; j < nodeList.getLength(); j++) 
        {
            if(companyCode == nodeList.item(j).getFirstChild().getNodeValue())
            {
                //TODO Update Listview Code
            }
        }
    }       

    txtStatus.setText(String.valueOf("Last Update: " + curTime));
}

The listview mapping is as follows:

// create the grid item mapping
String[] columns = new String[] {"col_1", "col_2", "col_3" };
int[] rows = new int[] { R.id.CodeColumn, R.id.NameColumn, R.id.TradeColumn };
like image 574
Mo Patel Avatar asked Dec 20 '10 01:12

Mo Patel


People also ask

How do I update Android lists?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

You should implement you own ListView Adapter that will provide data to the list view. Calling notifyDataSetChanged() from adapter will force list view to fetch data from the adapter. Updating list view views directly looks strange.

like image 167
Dmitry Ryadnenko Avatar answered Oct 22 '22 15:10

Dmitry Ryadnenko


You can call invalidate to let the listview redraw.

like image 3
Gao Yuesong Avatar answered Oct 22 '22 17:10

Gao Yuesong