I'm trying to use a customAdapter to populate a listView and for some reason, when I add new data dynamically, the old data is replaced by new data and the new data added is completely out of order. Here is my custom adapter:
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;
}
}
I'm adding new data as with the following code
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());
} });
}
I don't understand why new data is added out of order and why old data is replaced in the listView.
Thanks for your help