fabiruu111
fabiruu111

Reputation: 79

Add SQLite database data to Listview

I checked out this tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Now I want to load this data in a Listview. Here is my try:

private ArrayAdapter<Contact> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        DatabaseHandler db = new DatabaseHandler(this);

        /**
         * CRUD Operations
         * */
        // Inserting Contacts
        Log.d("Insert: ", "Inserting ..");
        db.addContact(new Contact("Ravi", "9100000000"));
        db.addContact(new Contact("Srinivas", "9199999999"));
        db.addContact(new Contact("Tommy", "9522222222"));
        db.addContact(new Contact("Karthik", "9533333333"));

        // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<Contact> contacts = db.getAllContacts();

        for (Contact cn : contacts) {
            String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
            // Writing Contacts to log
            Log.d("Name: ", log);

        }
        

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        adapter = new ArrayAdapter<Contact>(getApplicationContext(), R.layout.list_view, contacts);


        ListView listView = (ListView) findViewById(R.id.list);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

    }

So the Class from the Contact you can see in the url from androidhive.

Upvotes: 0

Views: 312

Answers (2)

Rajan Kali
Rajan Kali

Reputation: 12953

Well the thing is Your Supplying Contact to ArrayAdapter ,But it needs String to populate Values

or Use CustomAdapter to popluate all Vaues regarding Contact in List

1.Create a Layout for single item,in your case create list_item.xml in Layout Folder

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:padding="6dip" >

    <TextView
        android:id="@+id/contactname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/contactphone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</RelativeLayout> 

2.Create CustomAdapter which extends ArrayAdapter

CustomAdapter.java

 public class CustomAdapter extends ArrayAdapter<Contacts> {
      private final Context context;
      private ArrayList<Contacts> contacts;
    
      public CustomAdapter(Context context,ArrayList<Contacts> contacts) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.contacts= contacts;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView name= (TextView) rowView.findViewById(R.id.contactname);
        TextView mobile= (TextView) rowView.findViewById(R.id.contactphone);
        name.setText(contacts.get(position).getName());
        mobile.setText(contacts.get(position).getPhoneNumber());
        return rowView;
      }
    } 

3.Call the Adpater from Activity

CustomAdapter adapter = new CustomAdapter (getApplicationContext(),contacts);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(adapter);

Upvotes: 1

Karan Khurana
Karan Khurana

Reputation: 585

You can try this :

1st Step Reading all the Contacts from database one by one through loop :

 protected void onDestroy() {
        super.onDestroy();
    }
    //reading all the contacts from database
    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + <Table-Name>;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setPhoneNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

2nd Step

Now you can call this getAllContacts() in your onCreate and extract out all the contacts and save in the Adapter,then show in your ListView.

Hope it helps. :)

Upvotes: 0

Related Questions