Home > OS >  How to update data from listview if the condition were met android java
How to update data from listview if the condition were met android java

Time:01-15

Hi Iam creating this app were you input Name and it automatically put the Time-inon the listview with current time. Now, if the user were to put the same Name, the system then recognized it to avoid duplication. Here is the code for condition

                    getTime();
                    String fnlAddName = finalTextName "\n" formattedDate "\n" strTime;
                    if (names.indexOf(finalTextName) > -1){
                        //the system recognized the same input
                        String beforeName = listView.getItemAtPosition(position).toString();
                        names.add(beforeName "\n" strTime);
                        myAdapter.notifyDataSetChanged();
                    }else{
                        names.add(fnlAddName);
                        myAdapter.notifyDataSetChanged();
                        dialog.cancel();
                        position = position 1;
                    }

Now, I already achieved to detect same input from the user. What I want now is, I want to take that same data from the list (with also the time) and add another current time. So the list must update from "Name 1stCurrentTime" to "Name 1stCurrentTime 2ndCurrentTime"

CodePudding user response:

Your code should look something like this

        if (names.indexOf(finalTextName) > -1){
            //the system recognized the same input
            int index = names.indexOf(finalTextName);

            names.set(index, names.get(index)   "\n"   strTime);
            myAdapter.notifyDataSetChanged();
        }else{
            names.add(fnlAddName);
            myAdapter.notifyDataSetChanged();
            dialog.cancel();
            position = position 1;
        }

With the indexOf method you can get the position of the list that contains the name, then we replace it, I hope this helps you.

  • Related