0

I am currently trying to create a search function for a ListView for an SQLite database. But to do that I need to convert my database values that are in an array list "userList" to string so it can be used in the menu? I need userList to be an array of strings so I can use them in the search function.

Edit: Instead of doing this could i put only the "Id entry" variable in an arraylist? How could I do this?

Here is my code:

public class ViewListContents extends AppCompatActivity {


    DatabaseHelper deviceDB;
    ArrayList<User> userList;
    //ArrayList<String> listItem;

    ListView listView;
    User user;


    ArrayAdapter adapter1;
    ArrayList<String> list;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewcontents_layout); //sets layout

        deviceDB = new DatabaseHelper(this); //initialises DB for database class
        userList = new ArrayList<>();
        Cursor data = deviceDB.getListContents();


        list = new ArrayList<>();
        //FiveColumn_ListAdapter adapter1 = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,list);


        int numRows = data.getCount();
        if (numRows == 0 ){
            Toast.makeText(ViewListContents.this,"The database was empty",Toast.LENGTH_LONG).show();
        }
        else {
            while (data.moveToNext()){
                user = new User(data.getString(0),data.getString(1),data.getString(2),data.getString(3),data.getString(4),data.getString(5)); //retrieving columns from data
                userList.add(user); //userlist array takes items of type user
            }
        }

        FiveColumn_ListAdapter adapter = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,userList);
        listView = (ListView) findViewById(R.id.ListView);
        listView.setAdapter(adapter);




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        MenuItem searchItem = menu.findItem(R.id.item_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {



                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {

                //ArrayList<String> List = new ArrayList<>();

                for (User user : userList){
                    if (user.
                }

                return true;
            }
        });




        return super.onCreateOptionsMenu(menu);
    }

}

User is a self defined class I made to get the variables here:

public User (String fID_Entry, String fName, String fAddress, String fLatitude, String fLongtitude, String fTime){
        ID_Entry = fID_Entry;
        Name = fName;
        Address = fAddress;
        Latitude = fLatitude;
        Longtitude = fLongtitude;
        Time = fTime;

    }

    public String getID_Entry() {
        return ID_Entry;
    }

    public String getName() {
        return Name;
    }

    public String getAddress() {

        return Address;
    }

    public String getLatitude() {

        return Latitude;
    }

    public String getLongtitude() {

        return Longtitude;
    }
    public String getTime() {
        return Time;
    }
}
  • I believe this was already answered here https://stackoverflow.com/questions/10219527/convert-arraylist-type keep in mind that at the end of the day you are just doing a simple java objects conversion – Camilo Casadiego May 20 '20 at 14:11

1 Answers1

0

Override toString method or create your own method to get your class String representation in your User class and then you could for example do:

List<String> usersStrings = userList.stream().map(User::representation).collect(Collectors.toList());

or for example:

ArrayList<String> idList = new ArrayList<>();
for (int i = 0; i <  user_list.size(); i++) {
    // This line depend on how you want to represent your user string
    idList.add(user_list.get(i).getID_Entry());
}

I also changed name of your fields. They should always start with small letter not to confuse them with classes. And the prefix is also not needed because most IDEs will color them for you. Try to stick with camelCase for fields and methods and PascalCase for classes.

https://en.wikipedia.org/wiki/Camel_case

class User {

        String idEntry;
        String name;
        String address;
        String latitude;
        String longtitude;
        String time;

        public String representation() {
            // This could be implemented as you want
            return name + " " + idEntry;
        }

        public User (String fID_Entry, String fName, String fAddress, String fLatitude, String fLongtitude, String fTime){
            idEntry = fID_Entry;
            name = fName;
            address = fAddress;
            latitude = fLatitude;
            longtitude = fLongtitude;
            time = fTime;
        }

        public String getID_Entry() {
            return idEntry;
        }

        public String toString() {
            // This is also used when you will try to print the object
            // You can change how you want it to look
            return "User{" +
                    "idEntry='" + idEntry + '\'' +
                    ", name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    ", latitude='" + latitude + '\'' +
                    ", longtitude='" + longtitude + '\'' +
                    ", time='" + time + '\'' +
                    '}';
        }

        public String getName() {
            return name;
        }

        public String getAddress() {
            return address;
        }

        public String getLatitude() {
            return latitude;
        }

        public String getLongtitude() {
            return longtitude;
        }

        public String getTime() {
            return time;
        }
    }

Note that when the code from the first snippet is called your users will be represented as their name + " " + id. You could adjust representation method if you want something different.

Sebastian
  • 551
  • 2
  • 8
  • not quite sure how to do this, could you provide an example in both classes? – punter12467 May 20 '20 at 14:08
  • You call the code from first snippet when you want to change list of users to list of strings. – Sebastian May 20 '20 at 14:33
  • You could also read about toString method: https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – Sebastian May 20 '20 at 14:34
  • I get errors when implementing the representation function, things like ".map" and .stream() have errors saying "calls require API level 24, current is 16". – punter12467 May 20 '20 at 14:44
  • Or how could i implement only the "ID entry" parameter into the arraylist string? – punter12467 May 20 '20 at 14:48
  • To use stream you need at least java 8. It looks like you are using quite old version if you do not have to use the old one it would be good to update it. – Sebastian May 20 '20 at 15:53
  • If you want only id in your representation change the representation method to return id. Or instead of using representation in map use User::getID_Entry this would be more clear – Sebastian May 20 '20 at 15:55
  • could you show me how I could implement this toString method to access the array, I cannot use method references unfortunately. – punter12467 May 20 '20 at 19:32
  • you cannot use method refernece or stream? If you cannot use method reference you can write: "usert -> user.getID_Entry()" instead of "User::getID_Entry" – Sebastian May 20 '20 at 19:45