我开始通过修改例子来学习软件开发。现在我正在制作一个联系人列表,我想通过修改 this 从本地 SQLite 数据库填充它。教程,而不是从我想要的 json 获取数据,而是从我的本地数据库获取数据。我有一个包含许多联系人的本地数据库。
这是我的问题:当我打开我的联系人列表时,进度对话框一直在旋转,并且没有显示任何联系人。
这是我的代码:
SQLite处理程序:
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;
}
联系人列表适配器:
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;
}
}
联系人列表 Activity :
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;
}
}
}
最佳答案
在初始化适配器时,列表是空的,所以你需要先从数据库中获取数据
// 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();
关于java - 从 SQLite 填充联系人 ListView - 不显示联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40739070/