-2

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

MSO
  • 349
  • 1
  • 5
  • 20
  • When You add data from Activity first time you use `list.add("")` , But when you add more items, You can use other list and add with this method `addAll()` with the previous list. pretty simple – Farhana Naaz Ansari Apr 06 '18 at 12:04
  • 2
    Use a `POJO` class for data . And Follow https://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter. Its hard to understand the uses of `viewKind`. Consider Using `RecyclerView` for multiple view types . – ADM Apr 06 '18 at 12:05
  • Hi but this does not explain why old data is replaced, and also not why data is out of order! I'm testing it with sequential data, and it goes well till I scroll for the first time – MSO Apr 06 '18 at 12:08
  • Do you know which is the best way for biding data? – MSO Apr 06 '18 at 12:18
  • Hi what @ADM said worked fine, thanks! – MSO Apr 06 '18 at 12:32

1 Answers1

0

iterated collections such as lists in Java are not in a guaranteed order , but it usually is the default OS file directory collating order. You need to place in order the list as an array

Use add() method initially only to enter the element after list creation

Use toArray() to return an array of type "Object"

Object arraylist_object = namedarraylist_reference.toArray();

Use T[] toArray(T[] a) to return an array from the ArrayList into its "particular object type" e.g. a string type

String[] listedStrings = new String[namedarraylist_reference.size()];
namedarraylist_reference.toArray(listedStrings); 

Use set(int index, E element) to replace an element exactly where you want in the list

namedarraylist_reference.set(3,"WHAT");
// or
namedarraylist_reference.set(7,listedStrings[4]);

call repaint() method on the GUI widget after.

There may be a problem with using "public" methods upon "private" variables/objects too !