3

I have a ListView which I populate with data from a database.

ArrayList<HashMap<String, String>> userList = dBcode.getAllUsers();

ListAdapter adapter = new SimpleAdapter(
                Main_Activity.this,userList, R.layout.list_view_search_item, new String[]
                { "name", "age", },
                new int[] {R.id.name, R.id.Age});

It works perfectly well.

Now I want to create a new vResult for every row in the Listview with the values from the ListView, instead of typing it like the example below.

private Result[] resultArray = {
        vResult("Thomas", "12"),
        vResult("Mike", "15")};

This is the code to retrieve the data from the database.

 public ArrayList<HashMap<String, String>> getAllUsers(){

    ArrayList<HashMap<String, String>>userArrayList = new ArrayList<HashMap<String, String>>();

    String selectQuery = "SELECT * FROM user ORDER BY name";

    SQLiteDatabase database = this.getWritableDatabase();

    Cursor cursor = database.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()){

        do{

            HashMap<String, String> contactMap = new HashMap<String, String>();
            contactMap.put("name", cursor.getString(0));
            contactMap.put("age", cursor.getString(1));

            userArrayList.add(contactMap);

        } while(cursor.moveToNext());

    }

    return userArrayList;

}

Thanks

6
  • It is hard to understand what is your goal Commented Nov 12, 2015 at 22:04
  • @Joseph82 i dont want to hardcode the values in resultArray, i want it to use the values in the listview that is coming from the database. Thanks Commented Nov 12, 2015 at 22:07
  • Is your DB a remote DB (i.e. Postgres, MongoDB, MySQL db) or a local one (SQLite). Also, what is the format of the response: If it is from a remote DB? JSON? For a local DB you should be returning a list of Objects, rather than a HashMap, where the Objects have fields such as name and age (say, an Employee objecT) Commented Nov 12, 2015 at 22:19
  • @LucasCrawford its sqlLite, i have included the code the retrive the database data. Thanks Commented Nov 12, 2015 at 22:23
  • Okay, I thought so. So, instead of creating a HashMap and returning results in a hashmap, return a List<Person> where Person is a simply POJO with fields of name and age. This will simplify your development process and allow for extensibility down the road! Also, you will need to create your own Adapter by extending BaseAdapter. While a simple adapter works, you will eventually again need to do more and a BaseAdapter will allow you to do this. For example, customizing a view with the Person data. Commented Nov 12, 2015 at 22:27

1 Answer 1

1

It would probably be easier for you if instead of using a HashMap, you created a class to hold your user fields (with getter/setter methods):

public class User {
    private String mName;
    private String mAge;

    public User(String name, String age){
        mName = name;
        mAge = age;
    }

    public String getName(){
        return mName;
    }
    public String getAge(){
        return mAge;
    }
    public String setName(String name){
        mName = name;
    }
    public String setAge(String age){
        mAge = age;
    }
}

And to create a User object from the database:

ArrayList<User> userArrayList = new ArrayList<User>();
...
contact = new User(cursor.getString(0), cursor.getString(1));
userArrayList.add(contact);

You can call adapter.getItem(position) to return the data object at a particular index:

User user = (User)adapter.getItem(0);

Then convert the data into whatever format you prefer. Maybe something like this:

private Result[] resultArray = new Result[1];
resultArray[0] = vResult(user.getName(), user.getAge());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.