0

我知道这里有很多关于更新列表视图的建议,我已经尝试过了,但没有任何效果。你能帮我么?我不知道我是否错过了什么。这是我的代码:

simpleAdapterConsumerList= new ConsumerList(RegistrationActivity.this, arraylistRegisteredConsumer, R.layout.registeredconsumerlistattributecell, Constants.REGISTERED_ATTRIBUTE_KEYS, Constants.REGISTERED_ATTRIBUTES_VIEWS, false);
        lv_ConsumersList.setAdapter(simpleAdapterConsumerList);
        registeredConsumerCount = lv_ConsumersList.getCount();

当我勾选提交按钮时,我想在不离开页面的情况下自动更新 lisview。当我点击 listview 时,会出现一个警告对话框,询问用户是否要删除所选项目,如果用户点击 OK 按钮,它已经在我的数据库中被删除,但 listview 没有更新。我仍然需要离开页面才能看到更新的列表视图。我尝试将代码放在下面,但没有任何效果。()

simpleAdapterConsumerList.notifyDataSetChanged(); 

((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged();

这里是我在数据库中添加新数据的代码

btn_Register.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            Emp = txt_Emp.getText().toString();
            Lastname = txt_Lname.getText().toString();
            Firstname = txt_Fname.getText().toString();
            Middlename = txt_Mname.getText().toString();
            Cp = txt_Cp.getText().toString();
            Email = txt_Email.getText().toString();
            fullName = Lastname +" "+ Firstname +" "+Middlename;
            careFriend = sharedPreferences.getString(Constants.SHARED_PREFERENCES_CAREFRIEND, "");  
            companyName = sharedPreferences.getString(Constants.SHARED_PREFERENCES_COMPANY_CARE_FRIEND, "");
            consumercompanycode =  sharedPreferences.getString(Constants.SHARED_PREFERENCES_COMPANYCODE_CARE_FRIEND, "");
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
            emailmark = "Not send";
            consumerId = sharedPreferences.getInt(Constants.SHARED_PREFERENCES_CONSUMER_ID, 1);
            consumerId = consumerId + 1;
            if (!Lastname.equals(""))
            {
                ....
{
                            String currentDateandTime = sdf.format(new Date());
                            databaseAdapter.SaveConsumerDetails(new Constructor(Emp, Lastname, Firstname, Middlename, Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode));
                            sharedPreferencesEditor.putInt(Constants.SHARED_PREFERENCES_CONSUMER_ID, consumerId);
                            sharedPreferencesEditor.commit();
                            simpleAdapterConsumerList.notifyDataSetChanged();
                            //((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged(); 
                            //registeredConsumerCount = lv_ConsumersList.getCount();
                            Toast.makeText(RegistrationActivity.this, "Registered successfully. " + currentDateandTime, Toast.LENGTH_LONG).show();                                                                          simpleAdapterConsumerList.notifyDataSetChanged();
                            Clear();

                        }else{
          ....          
        }

在我的删除按钮上。这是我的代码

public void onClick(DialogInterface dialog, int id) 
                {
                    databaseAdapter.deleteSelectedItem(ID);
                    Toast.makeText(RegistrationActivity.this, "Delete " + ID, Toast.LENGTH_LONG).show();                                                                            
                    simpleAdapterConsumerList.notifyDataSetChanged();
                }

....
final Cursor cursorRegisteredConsumer = databaseAdapter.getCursorRegisteredConsumer();

   ....
//on my databaseAdapter here is my code

public Cursor getCursorRegisteredConsumer(){
    Cursor c = dbSqlite.query(Constants.DATABASE_TABLE_CONSUMER, new String[] { Constants.DATABASE_COLUMN_ID, Constants.CONSUMER_EMPNO, Constants.CONSUMER_FIRSTNAME, Constants.CONSUMER_LASTNAME, Constants.CONSUMER_MIDDLEINITIAL, Constants.CONSUMER_CELLPHONENO, Constants.CONSUMER_EMAIL, Constants.CONSUMER_FULLNAME, Constants.CONSUMER_CAREFRIEND, Constants.CONSUMER_COMPANY, Constants.CONSUMER_REGISTRATIONDATE, Constants.CONSUMER_EMAILMARK,Constants.CONSUMER_ID,Constants.CONSUMER_COMPANYCODE}, null , null, null, null, Constants.DATABASE_COLUMN_ID + " ASC");
    //c.setNotificationUri(getContext().getContentResolver(), uri);
    if (c != null) {
        c.moveToFirst();
    }
    return c;

}
4

4 回答 4

1

这有两个部分:

1. 支持您的适配器的光标不会自行更改。

支持您的适配器的光标指向查询的一组结果。当数据库中的数据发生变化时,Cursor 仍然指向同一个结果集。您需要再次查询以获取更新的数据。每当您手动删除项目时,您都可以自己执行此操作。

如果你已经为你的数据库设置了一个合适ContentProvider的,你可以改为使用 aContentObserver和/或 a CursorLoader(它自己设置一个ContentObserver),并确保在你的 ContentProvider 的query(...)方法中你这样做:

Cursor cursor = ...; // query
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;

 

2.如果你得到一个新的Cursor,你需要更换旧的。

您可能有一个新光标,但您的适配器仍使用旧光标,因此您需要更换它们。将此方法添加到您的适配器,并在您获得查询结果时调用它。

public void swapCursor(Cursor newCursor) {
    if (cursor != null && oldCursor.isOpen()) {
        cursor.close();
    }
    cursor = newCursor;
    notifyDataSetChanged();
}
于 2013-06-05T02:11:43.277 回答
0

处理光标适配器时,您需要调用swapCursor()并传递更新后的光标,然后调用notifyDataSetChanged.

于 2013-06-05T00:49:50.897 回答
0

在调用 notifyDataSetChanged() 之前,应更新Adapter 引用的ConsumerList 。
从您的代码中,我猜您使用了 ListAdapter 或其子类,我没有测试其他 API 级别,但我相信 API 级别 16(JELLY_BEAN)对 ListAdapter.notifyDataSetChanged() 不起作用,即使您刷新列表。所以,我建议你使用 BaseAdapter。

于 2013-06-05T02:12:44.947 回答
0

老兄,

  After calling "NotifyDatasetChanged" also your listview try to access your  "arraylistRegisteredConsumer" arralist. But in this ArrayList you didn't delete or insert the data. 

   That's why your listview again loading same data.

   Perform delete operation in "arraylistRegisteredConsumer" based on "Index" then call

   "NotifyDatasetChanged".
于 2013-06-05T02:54:13.467 回答