Artsait Artsait - 2 months ago 45
Android Question

How to return all data from one column sugar orm

How to return all data from one column sugar orm ?

Contact.java

import com.orm.SugarRecord;
import java.util.List;

public class Contact extends SugarRecord{
String name;
String mail;
public Contact() {
}
public Contact(String name, String mail) {
this.name = name;
this.mail = mail;
}


}

MainActivity.java

List<Contact> allContacts = Contact.listAll(Contact.class);

ArrayAdapter<Contact> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, allContacts);
ListView catsListView = (ListView) findViewById(R.id.listView);
catsListView.setAdapter(adapter);


How can I Load the name column in the array?

Answer

Looking at the source code of the ArrayAdapter you can't do that.

Either you need to form a new ArrayList<String> as follows and pass it to ArrayAdapter.

ArrayList<String> arr = new ArrayList<>();
for(Contact contact:allContacts){
    arr.add(contact.getName()); // or arr.add(contact.name); if it's public
} 

Or you need to define customAdapter extending ArrayAdapter

Comments