java Android 更新 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4486107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Updating ListView
提问by Mo Patel
I have looked at Android - how to update ListView item that is currently displayedand http://commonsware.com/Android/excerpt.pdfand 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.
To update the listview, I am calling a servlet which returns an XML that I loop through using DOM.
我的问题:
使用处理程序,我试图更新我从从 MySQL 数据库检索数据的 web 服务填充的 Stock 数据多列列表视图。为了更新列表视图,我调用了一个 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. Also when I try to cast a View from a ListView row, I get a NullPointerException and can't figure out why.
我找不到将新数据(来自 XML)应用到 Listview 的有效方法,尽管只需要更新第三列(贸易列)。此外,当我尝试从 ListView 行投射 View 时,我收到 NullPointerException 并且无法弄清楚原因。
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 };
回答by Dmitry Ryadnenko
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.
您应该实现您自己的 ListView Adapter,它将为列表视图提供数据。从适配器调用 notifyDataSetChanged() 将强制列表视图从适配器获取数据。直接更新列表视图看起来很奇怪。
回答by Gao Yuesong
You can call invalidate to let the listview redraw.
您可以调用 invalidate 让列表视图重绘。