Android 如何更新列表视图的最后一行?
我正在创建一个聊天应用程序。当我从服务器获得新的聊天信息时,我会通过在列表中添加最后一行来更新列表。但一旦我发送了一条消息,我就会将其添加到列表中。发送消息时,我从服务器获得成功响应。我需要在不添加新行的情况下更新列表最后一行中的成功消息 我正在粘贴适配器类中的代码块Android 如何更新列表视图的最后一行?,android,listview,adapter,Android,Listview,Adapter,我正在创建一个聊天应用程序。当我从服务器获得新的聊天信息时,我会通过在列表中添加最后一行来更新列表。但一旦我发送了一条消息,我就会将其添加到列表中。发送消息时,我从服务器获得成功响应。我需要在不添加新行的情况下更新列表最后一行中的成功消息 我正在粘贴适配器类中的代码块 @Override public View getView(int position, View convertView, ViewGroup parent) { if (type == TYPE_MESSAGE) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (type == TYPE_MESSAGE) {
view = inflater.inflate(R.layout.message_list_view, null);
if(msg.equals("Seen")) {
// need to change the status here. Don't add new row
}else if(msg.equals("Not Seen")) {
// need to change the status here. Don't add new row
} else if(msg.equals("Received message")){
// need to change the status here. Don't add new row
} else {
// else add new row and add new chat message
}
return view;
}
有人能给我一个解决方案吗?您可以使用
listView.getChildAt(listView.getAdapter().getCount()-1)
-返回最后一行的视图。更新适配器并调用:
adapter.notifyDataSetChanged();
getListView().setSelection(adapter.getCount()-1);
notifyDataSetChanged不适用于更新。用于添加/删除。它将在适配器更改后调用。