Kemo Kemo - 16 days ago 8
Android Question

Populating contacts listvew from SQLite - no contacts displayed

I am starting to learn software development by modifying examples. Right now I am making a list of contact and I want to populate it from local SQLite db, by modifying this tutorial and instead of getting data from json I wanted do get data from my local db. I have a local database with a number of contacts.

This is my problem: when I open my contacts list, the progress dialog keeps spinning, and there are no contacts displayed.

Here is my code:

SQLiteHandler:

public List<Contact> getMyContactDetails() {
List<Contact> contacts = new ArrayList<Contact>();

String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;

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


if (cursor != null) {
try {
while (cursor.moveToNext()) {
Contact contact = new Contact();
contact.setUserName(cursor.getString( cursor.getColumnIndex(DISPLAY_NAME)));
contact.setThumbnailUrl(cursor.getString( cursor.getColumnIndex(IMAGE)));
contacts.add(contact);
}
} finally {
cursor.close();
}
}
db.close();
return contacts;
}


ContactListAdapter:

public class ContactsListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Contact> contactItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public ContactsListAdapter(Activity activity, List<Contact> contactItems) {
this.activity = activity;
this.contactItems = contactItems;
}

@Override
public int getCount() {
return contactItems.size();
}

@Override
public Object getItem(int location) {
return contactItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row_contact, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.imageview_profile);
TextView userName = (TextView) convertView.findViewById(R.id.textview_username);

// getting contact data for the row
Contact m = contactItems.get(position);

// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

// userName
userName.setText(m.getUserName());

return convertView;
}
}


ContactListActivity:

public class ContactsListActivity extends AppCompatActivity {

private ProgressDialog pDialog;
private List<Contact> contactList = new ArrayList<>();
private ListView listView;
private ContactsListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setContentView(R.layout.activity_contacts_list);

listView = (ListView) findViewById(R.id.contacts_list);
adapter = new ContactsListAdapter(this, contactList);
listView.setAdapter(adapter);

pDialog = new ProgressDialog(this);
pDialog.setMessage(getString(R.string.loading));
pDialog.show();

SQLiteHandler db = new SQLiteHandler(this.getApplicationContext());
contactList = db.getMyContactDetails();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}

Answer

While initializing adapter the list is empty so you need to fetch data from DB first

// show dialog
pDialog = new ProgressDialog(this);
pDialog.setMessage(getString(R.string.loading));
pDialog.show();

// fetch the data first 
SQLiteHandler db = new SQLiteHandler(this.getApplicationContext());
contactList = db.getMyContactDetails();    

// now the list has data so display it
listView = (ListView) findViewById(R.id.contacts_list);
adapter = new ContactsListAdapter(this, contactList);
listView.setAdapter(adapter);

// dismiss the dialog 
hidePDialog();