2015-11-04 62 views
1

我创建了一种联系人应用程序。我正在使用ListView和SimpleCursorAdapter,并且联系人存储在SQLite数据库中。带有SimpleCursorAdapter的ListView在更改后不刷新

在主屏幕中,有两个按钮:“添加联系人”和“查看联系人”。当我查看联系人并点击其中一个联系人时,我可以编辑或删除该联系人。

我做了任何这些更改后,显示的联系人列表应根据所作更改进行刷新,但不会。

我已经阅读并尝试了很多可能的解决方案,但是我发现每种情况都不同,显然某些解决方案仅在某些情况下有效,所以我非常困惑。

在主活性,我把这种方法:

public void viewContacts(View view){ 
    Intent intent = new Intent(this, Contacts.class); 
    startActivity(intent); 
} 

这是联系人类:

public class Contacts extends ExampleActivity { 

ContactsOpenHelper dbHelper; 
SQLiteDatabase db; 
SimpleCursorAdapter adapter; 
ListView listView; 
Cursor c; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contactslayout); 

    listView = (ListView) findViewById(R.id.listView); 

    dbHelper = new ContactsOpenHelper(this); 
    db = dbHelper.getReadableDatabase(); 

    String[] projection = {Contract.Entry.NAME,Contract.Entry.NUMBER,_ID}; 

    c = db.query(Contract.Entry.TABLE_NAME, projection, null, null, null, null, null); 

    String[] fromColumns = {Contract.Entry.NAME,Contract.Entry.NUMBER}; 
    int[] toViews = {R.id.textViewName, R.id.textViewNumber}; 

    adapter = new SimpleCursorAdapter(this,R.layout.contactslayoutentry,c,fromColumns,toViews,0); 

    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      ContactDialog cd = new ContactDialog(db, id, adapter); 
      cd.show(getFragmentManager(), "Contactos"); 
     } 
    }); 

} 
} 

的ContactDialog类:

public class ContactDialog extends DialogFragment { 

SQLiteDatabase db; 
long id; 
SimpleCursorAdapter adapter; 

public ContactDialog(SQLiteDatabase db, long id, SimpleCursorAdapter adapter) { 
    this.db = db; 
    this.id = id; 
    this.adapter = adapter; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle("Choose an action") 
      .setPositiveButton("Edit", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        MyDialogFragment mdf = new MyDialogFragment(getActivity().getApplicationContext(), db, true, id); 
        mdf.show(getFragmentManager(), "TAG"); 
       } 
      }) 
      .setNegativeButton("Delete", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        String selection = Contract.Entry._ID + " LIKE ?"; 
        String[] selectionArgs = {String.valueOf(id)}; 
        db.delete(Contract.Entry.TABLE_NAME, selection, selectionArgs); 
        adapter.notifyDataSetChanged(); 
       } 


      }); 
    return builder.create(); 
} 
} 

在MyDialogFragment I类跑线

db.update(Contract.Entry.TABLE_NAME, myValues, Contract.Entry._ID + " LIKE " + id,null); 

我不知道我是否需要notifySetDataChanged()方法,何时执行它,或者我是否需要其他解决方案。

我真的很感谢一些帮助。

谢谢。

编辑:

忘指出的元件被正确编辑或在数据库中删除。当我做一些更改时,我会返回到主屏幕并再次单击“查看联系人”,并且列表已更新。我想这很重要。

+0

你已经删除了数据库中的记录,但你必须从数据库中获取未更新的记录再次。您必须在通知适配器之前执行该操作。 –

+0

如何从数据库中获取更新的记录? –

+0

联系人数据库是由您自己或系统创建的? – starkshang

回答

0

首先,我建议你使用CursorLoaderLoaderManager,它会比你现在的解决方案好。

如果你仍然想使用此解决方案,请更新后添加cursor.requery()您的数据,这样的:

db.update(Contract.Entry.TABLE_NAME, myValues, Contract.Entry._ID + " LIKE " + id,null); 
adapter.getCursor().requery(); 
+0

** requery()**方法已弃用,我的软件不允许使用它。 –

+0

我正在更改为CursorLoader和LoaderManager。只有一个问题:在这种情况下,CursorLoader的构造函数中的参数CONTENT_URI的值是多少? –

相关问题