即使使用Sql数据库和Cursor调用的notifyDataSetChanged(),Listview也不会自动更新

问题描述 投票:0回答:1

我正在使用一个功能从Android手机书中选择手机名称,并使用光标来获取联系人的姓名和号码。我能够成功地执行此操作并将值插入到我在此处使用另一个问题的帮助编写的sqllite数据库中,但是当有新值插入数据库时​​,listview不会自动更新。

我已经使用了所有必需的行,包括

  • ((BaseAdapter)display_contacts1.getAdapter()).notifyDataSetChanged();
  • display_contacts1.invalidateViews();
  • arrayAdapter.notifyDataSetChanged();
  • manageListView(getContext());

但我仍然没有找到解决方案的运气。 listview将更新的唯一方法是我进入不同的片段或重新启动应用程序。

原始SQL数据库代码Android OnItemClick not working with loaded SQL Database into listview

select modem fragment.Java

//--------------------------------------------------------------------
// Select Phone number from Contacts list
//--------------------------------------------------------------------
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        // Check for the request code
        switch (reqCode)
        {
            case CONTACT_PICKER:
                contactPicked(data);
                break;
        }
    }
    else
    {
        Log.e("MainActiivity", "Failed to pick contact");
    }
}



private void contactPicked(Intent data)
{
    //Cursor cursor = null;
    try
    {
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();

        //Query the content uri
        cursor = getActivity().getApplicationContext().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();

        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        name = cursor.getString(nameIndex);
        phoneNo = cursor.getString(phoneIndex);

        // Show message displaying the contact selected from the phonebook
        Toast.makeText(getContext(),name, Toast.LENGTH_SHORT).show();
        Toast.makeText(getContext(), phoneNo, Toast.LENGTH_SHORT).show();

        // Insert values to sqlite database
        dbHelper.insert(name, phoneNo);

        // Need to immediately update the listview one the values have been inserted into the database
        ((BaseAdapter)display_contacts1.getAdapter()).notifyDataSetChanged();

        //display_contacts1.invalidateViews(); // This isn't working
        //arrayAdapter.notifyDataSetChanged(); // This isn't working
        //manageListView(getContext());, this didn't work
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
android sqlite listview android-cursor
1个回答
0
投票

将值插入数据库后,您仍然可以调用notifyDataSetChanged(),然后再次打开该片段

 // Need to immediately update the listview one the values have been inserted into the database
            ((BaseAdapter)display_contacts1.getAdapter()).notifyDataSetChanged();

            // Reopen the fragment
            getFragmentManager().beginTransaction().replace(R.id.frame_layout, SelectModemFragment.newInstance()).commit();
© www.soinside.com 2019 - 2024. All rights reserved.