【问题标题】:Populating contacts listvew from SQLite - no contacts displayed从 SQLite 填充联系人列表视图 - 不显示联系人
【发布时间】:2016-11-22 10:11:51
【问题描述】:

我开始通过修改示例来学习软件开发。现在我正在制作一个联系人列表,我想通过修改 this 教程从本地 SQLite 数据库填充它,而不是从 json 获取数据,我想从本地数据库获取数据。我有一个包含许多联系人的本地数据库。

这是我的问题:当我打开我的联系人列表时,进度对话框一直在旋转,并且没有显示任何联系人。

这是我的代码:

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;
        }
    }
}

【问题讨论】:

  • 从数据库中获取联系人完成后,调用适配器的通知数据更改方法。
  • 请通过以下链接查看我的解决方案。 stackoverflow.com/questions/40170095/…
  • 你有一个方法hidePDialog()。看起来你(几乎)从不调用它,所以“进度对话框一直在旋转”直到活动被破坏。 @Febi Mathew 对 Adapter.notifyDatasetChanged() 的看法是正确的

标签: java android sqlite listview


【解决方案1】:

初始化适配器时,列表为空,因此您需要先从数据库中获取数据

// 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();

【讨论】:

    【解决方案2】:

    您需要获取数据后隐藏对话框。目前你隐藏在onDestroy

    【讨论】:

      【解决方案3】:

      我发现了两个错误

      1. 你从未调用 hidePDialog();只有你在 onDestroy 方法上调用它。所以 ProgressDialog 正在旋转。

      2. 您从数据库中获取联系人但未加载到列表视图。

      像这样修改代码

      SQLiteHandler db = new SQLiteHandler(this.getApplicationContext());
              contactList = db.getMyContactDetails();
       adapter = new ContactsListAdapter(this, contactList);
              listView.setAdapter(adapter);
      

      【讨论】:

        【解决方案4】:

        需要更多信息:

        1. 必须将所有联系人从 AndroidDB 复制到您的 localDb。
        2. 如果是。检查您的数据库一次。
        3. 如果你的 LocalDb 有联系人 然后更改您的代码:

          SQLiteHandler db = new SQLiteHandler(this.getApplicationContext()); 联系人列表 = db.getMyContactDetails(); 适配器 = 新的 ContactsListAdapter(this, contactList); listView.setAdapter(适配器);

        或

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

        在上一行之后添加下一行。

        adapter.notifydatasetchanged();
        

        稍后调用hidePDialog();,因为除了destroy()之外,您不会在任何地方停止对话

        【讨论】:

          猜你喜欢
          • 2014-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-24
          • 2016-09-24
          • 1970-01-01
          相关资源
          最近更新 更多