简体   繁体   English

Android正在更新ListView

[英]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. 我看过Android - 如何更新当前显示的ListView项目http://commonsware.com/Android/excerpt.pdf以及Android文档,但我仍然不明白。

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. 使用处理程序,我正在尝试更新Stock数据多列列表视图,该视图已从Web服务中填充,该服务从MySQL数据库中检索数据。 To update the listview, I am calling a servlet which returns an XML that I loop through using DOM. 为了更新列表视图,我正在调用一个servlet,它返回一个我使用DOM循环的XML。

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. 我找不到一种将新数据(从XML)应用到Listview的工作方式,尽管只需要更新第三列(贸易列)。 Also when I try to cast a View from a ListView row, I get a NullPointerException and can't figure out why. 此外,当我尝试从ListView行转换视图时,我得到一个NullPointerException并且无法找出原因。

The code I have done so far is below. 我到目前为止所做的代码如下。

java Code: java代码:

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 };

You should implement you own ListView Adapter that will provide data to the list view. 您应该实现自己的ListView适配器,它将向列表视图提供数据。 Calling notifyDataSetChanged() from adapter will force list view to fetch data from the adapter. 从适配器调用notifyDataSetChanged()将强制列表视图从适配器获取数据。 Updating list view views directly looks strange. 直接更新列表视图视图看起来很奇怪

您可以调用invalidate让listview重绘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM