从SQLite填充联系人listvew - 不显示任何联系人

时间:2016-11-22 10:11:52

标签: java android sqlite listview

我开始通过修改示例来学习软件开发。现在我正在制作一个联系人列表,我希望通过修改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;
        }
    }
}

4 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:3)

我发现了2个错误

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

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

  3. 更改此类代码

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

答案 3 :(得分:2)

需要更多信息:

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

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

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

    在上面的行之后添加以下行。

    adapter.notifydatasetchanged();
    

    稍后调用hidePDialog();因为除了destroy()

    之外没有停止对话框