我要做的是每5秒刷新一次ListView。为此,我使用了notifyDataSetChanged()。如果我只使用此列表,列表将刷新,但项目将添加到已存在的项目的末尾。如果我在此之前使用clear()和addAll(),则ListView为空。我该怎么做才能解决这个问题?这是我的代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.online_drivers_window);
online_drivers_name = getIntent().getExtras().getStringArrayList("drivers_name");
online_drivers_availability = getIntent().getExtras().getStringArrayList("drivers_availability");
ListView L = (ListView)findViewById(android.R.id.list);
final IconAdapter adapter = new IconAdapter(this,R.layout.list_item,R.id.label,online_drivers_name);
L.setAdapter(adapter);
Timer tm = new Timer();
tm.schedule(new TimerTask()
{
@Override
public void run()
{
update();
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.clear();
adapter.addAll(online_drivers_name);
adapter.notifyDataSetChanged();
}
});
}
},0,5000); }
public void update()
{
try
{
cs = new Socket(HOST, 2014);
final DataOutputStream os = new DataOutputStream(cs.getOutputStream());
final DataInputStream is = new DataInputStream(cs.getInputStream());
os.writeUTF(type);
os.writeUTF("show_drivers");
int num = is.readInt();
String s,av;
ArrayList<String> aux_name = new ArrayList<>();
ArrayList<String> aux_availability = new ArrayList<>();
for(int i = 0; i < num; i++)
{
s = is.readUTF();
av = (is.readBoolean() == true) ? "true" : "false";
aux_name.add(s);
aux_availability.add(av);
}
for(int i = 0; i < aux_name.size();i++)
if(!online_drivers_name.contains(aux_name.get(i)))
{
online_drivers_name.add(aux_name.get(i));
online_drivers_availability.add(aux_availability.get(i));
}
for(int j = 0; j < online_drivers_name.size();j++)
if(!aux_name.contains(online_drivers_name.get(j)))
{
online_drivers_name.remove(j);
online_drivers_availability.remove(j);
}
else
{
int k = aux_name.indexOf(online_drivers_name.get(j));
if(!aux_availability.get(k).equals(online_drivers_availability.get(j)))
{
online_drivers_name.remove(j);
online_drivers_availability.remove(j);
online_drivers_name.add(aux_name.get(k));
online_drivers_availability.add(aux_availability.get(k));
}
}
cs.close();
os.close();
is.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
答案 0 :(得分:0)
听起来很清楚,你在online_drivers_name
中添加了一个字符串,因此它出现在列表的末尾。
如果您想对ArrayList
进行排序,请在更新方法结束时尝试:
Collections.sort(online_drivers_name.subList(1, online_drivers_name.size()));
答案 1 :(得分:0)
似乎你没有更新你的arraylist尝试使用下面的代码,如果你有任何疑问,请告诉我
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.online_drivers_window);
online_drivers_name = getIntent().getExtras().getStringArrayList("drivers_name");
online_drivers_availability = getIntent().getExtras().getStringArrayList("drivers_availability");
ListView L = (ListView)findViewById(android.R.id.list);
final IconAdapter adapter = new IconAdapter(this,R.layout.list_item,R.id.label,online_drivers_name);
L.setAdapter(adapter);
Timer tm = new Timer();
tm.schedule(new TimerTask()
{
@Override
public void run()
{
new ArrayList<String> online_drivers_name = update();
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.clear();
adapter.addAll(online_drivers_name);
adapter.notifyDataSetChanged();
}
});
}
},0,5000); }
public ArrayList<String> update()
{
try
{
cs = new Socket(HOST, 2014);
final DataOutputStream os = new DataOutputStream(cs.getOutputStream());
final DataInputStream is = new DataInputStream(cs.getInputStream());
os.writeUTF(type);
os.writeUTF("show_drivers");
int num = is.readInt();
String s,av;
ArrayList<String> aux_name = new ArrayList<>();
ArrayList<String> aux_availability = new ArrayList<>();
for(int i = 0; i < num; i++)
{
s = is.readUTF();
av = (is.readBoolean() == true) ? "true" : "false";
aux_name.add(s);
aux_availability.add(av);
}
for(int i = 0; i < aux_name.size();i++)
if(!online_drivers_name.contains(aux_name.get(i)))
{
online_drivers_name.add(aux_name.get(i));
online_drivers_availability.add(aux_availability.get(i));
}
for(int j = 0; j < online_drivers_name.size();j++)
if(!aux_name.contains(online_drivers_name.get(j)))
{
online_drivers_name.remove(j);
online_drivers_availability.remove(j);
}
else
{
int k = aux_name.indexOf(online_drivers_name.get(j));
if(!aux_availability.get(k).equals(online_drivers_availability.get(j)))
{
online_drivers_name.remove(j);
online_drivers_availability.remove(j);
online_drivers_name.add(aux_name.get(k));
online_drivers_availability.add(aux_availability.get(k));
}
}
cs.close();
os.close();
is.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return online_drivers_name;
}