我尝试使用customAdapter来填充listView,出于某种原因,当我动态添加新数据时,旧数据将被新数据替换,并且添加的新数据完全无序。这是我的自定义适配器:
public class logAdapter extends BaseAdapter {
private ArrayList<String> kindArray = new ArrayList<String> (), logArray= new ArrayList<String> (), timeArray= new ArrayList<String> ();
private ArrayList<Integer> viewKind= new ArrayList<Integer> ();
private LayoutInflater inflater;
public logAdapter(Context context) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return kindArray.size();
}
public void updateData(String kind, String log, String time, Integer view) {
this.kindArray.add(kind);
this.logArray.add(log);
this.viewKind.add(view);
this.timeArray.add(time);
this.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currentView = convertView;
if (this.viewKind.get(position) == 0)
{
if (currentView == null) {
currentView = inflater.inflate(R.layout.log_list_first, parent, false);
TextView tView = (TextView)currentView.findViewById(R.id.addr);
tView.setText(this.logArray.get(position));
TextView timeView = (TextView)currentView.findViewById(R.id.timeText);
timeView.setText(this.timeArray.get(position));
}
}else if(this.viewKind.get(position) == 1){
if (currentView == null) {
currentView = inflater.inflate(R.layout.log_list_middle, parent, false);
TextView tView = (TextView)currentView.findViewById(R.id.logMessage);
tView.setText(this.logArray.get(position));
TextView titleView = (TextView)currentView.findViewById(R.id.kindText);
titleView.setText(this.kindArray.get(position));
}
}else if(this.viewKind.get(position) == 2){
if (currentView == null) {
currentView = inflater.inflate(R.layout.log_list, parent, false);
TextView tView = (TextView)currentView.findViewById(R.id.logMessage);
tView.setText(this.logArray.get(position));
TextView titleView = (TextView)currentView.findViewById(R.id.kindText);
titleView.setText(this.kindArray.get(position));
TextView timeView = (TextView)currentView.findViewById(R.id.timeText);
timeView.setText(this.timeArray.get(position));
}
}else if (this.viewKind.get(position) == 3)
{
if (currentView == null) {
currentView = inflater.inflate(R.layout.log_list_last, parent, false);
TextView tView = (TextView)currentView.findViewById(R.id.addr);
tView.setText(this.logArray.get(position));
TextView timeView = (TextView)currentView.findViewById(R.id.timeText);
timeView.setText(this.timeArray.get(position));
}
}
return currentView;
}
}
我正在添加新数据,如下面的代码
public void addLog(String date, String kind, Integer view, String log)
{
getActivity().runOnUiThread(new Runnable(){
public void run() {
adapter.updateData(kind, log, date, view);
//simpleList.smoothScrollToPosition(logArray.size());
} });
}
我不明白为什么新数据无序添加以及为什么在listView中替换旧数据。
感谢您的帮助
答案 0 :(得分:0)
迭代集合(例如Java中的列表)不在保证顺序中,但它通常是默认的OS文件目录整理顺序。 您需要将列表按顺序放置为数组
最初只使用add()方法在列表创建后输入元素
使用toArray()返回类型&#34; Object&#34;
的数组Object arraylist_object = namedarraylist_reference.toArray();
使用T [] toArray(T [] a)将ArrayList中的数组返回到其特定对象类型&#34; 的 e.g。字符串类型
String[] listedStrings = new String[namedarraylist_reference.size()];
namedarraylist_reference.toArray(listedStrings);
使用 set(int index,E element) 将元素准确替换为列表中的所需元素
namedarraylist_reference.set(3,"WHAT");
// or
namedarraylist_reference.set(7,listedStrings[4]);
在GUI小部件上调用repaint()方法。
使用&#34; public&#34;可能会出现问题方法&#34;私人&#34;变量/对象也是如此!