仅当再次重新启动应用程序时,ListView才会更新该条目

时间:2017-10-17 13:32:59

标签: android listview simplecursoradapter

在此应用程序中,我有一个listview和一个sqlitedatabase。有一个floating action button,点击后会显示dialog box,其中包含两个edittext个名称和另一个号码。问题是add上没有显示dialog box条目的listview选项后点击。activity。但是当onCreate被销毁并且activity上再次调用adapter.notifyDataSetChanged()时,会显示该条目。

我尝试使用public class DetailsActivity extends AppCompatActivity { private DatabaseManager manager; private ListView listView; private SimpleCursorAdapter adapter; final String[] from=new String[] {UserDatabase.NAME,UserDatabase.NUMBER}; final int[] to=new int[] {R.id.nameDisplay,R.id.phoneDisplay}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); manager = new DatabaseManager(getApplicationContext()); manager.open(); Cursor cursor=manager.fetch(); listView = (ListView) findViewById(R.id.listViewId); listView.setEmptyView(findViewById(R.id.empty)); adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row_item, cursor, from, to, 0); adapter.notifyDataSetChanged(); listView.setAdapter(adapter); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(DetailsActivity.this); LayoutInflater inflater = DetailsActivity.this.getLayoutInflater(); final View dialogView = inflater.inflate(R.layout.custom_dialog, null); dialogBuilder.setView(dialogView); final EditText name = (EditText) dialogView.findViewById(R.id.dialogEditNmID); final EditText phone = (EditText) dialogView.findViewById(R.id.dialogEditPhID); dialogBuilder.setTitle("Add Details"); dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (!TextUtils.isEmpty(name.getText().toString()) && !TextUtils.isEmpty(phone.getText().toString())) { /*adapter.notifyDataSetChanged(); manager.insert(name.getText().toString(), phone.getText().toString()); Toast.makeText(getApplicationContext(), "Added " + name.getText().toString(), Toast.LENGTH_SHORT).show();*/ insertData(name.getText().toString(),phone.getText().toString()); } else { Toast.makeText(getApplicationContext(), "Empty field(s)", Toast.LENGTH_SHORT).show(); } } }); dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog b = dialogBuilder.create(); b.show(); // listView.setAdapter(adapter); //adapter.notifyDataSetChanged(); } }); } public void insertData(String fname,String phnumber){ manager.insert(fname,phnumber); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); } } ,但它不起作用。代码如下所示:

代码

{{1}}

有些陈述是评论的,​​因为我试图得到所需的结果但却无法得到它。

4 个答案:

答案 0 :(得分:3)

这里有几件事需要改变。看看这段代码:

dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (!TextUtils.isEmpty(name.getText().toString()) && !TextUtils.isEmpty(phone.getText().toString())) {
            adapter.notifyDataSetChanged();
            manager.insert(name.getText().toString(), phone.getText().toString());
            Toast.makeText(getApplicationContext(), "Added " + name.getText().toString(), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Empty field(s)", Toast.LENGTH_SHORT).show();
        }
    }
});
  1. 点击“添加”按钮后,立即拨打adapter.notifyDataSetChanged();。在您的情况下,您只应在将项目添加到listView后调用它,但尚未添加任何内容。
  2. 您使用manager.insert(name.getText().toString(), phone.getText().toString());插入数据库,但不会使用新添加的数据更新listView。您需要将该数据插入数据库,然后将该数据添加到listView
  3. 现在您可以致电adapter.notifyDataSetChanged();

    我建议您在插入数据库时​​,创建一个插入数据的方法,将新数据添加到listView,然后告诉适配器刷新。

    修改

    关于您最近的编辑,还有一些事情需要注意。

    1. 您不应该在方法中使用listView.setAdapter(adapter)。你第一次使用它(在onCreate()中但在对话框构建器之前)。
    2. 您致电manager.insert(fname,phnumber);,但仍未将新插入的数据添加到listView
    3. 这是你的方法应该具有的伪代码:

      public void insertData(String fname,String phnumber){
          manager.insert(fname,phnumber);
          // Code to add the data you just inserted into the manager above to `listView`.
          adapter.notifyDataSetChanged();
      }
      

      请注意,如果adapter.notifyDataSetChanged();发生了更改,则listView仅更新listView,截至目前您尚未添加/删除/修改listView

答案 1 :(得分:1)

我对代码做了一些更改,请尝试一下,如果有帮助请告诉我

public class DetailsActivity extends AppCompatActivity {

private DatabaseManager manager;
private ListView listView;
private SimpleCursorAdapter adapter;

final String[] from=new String[] {UserDatabase.NAME,UserDatabase.NUMBER};

final int[] to=new int[] {R.id.nameDisplay,R.id.phoneDisplay};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    manager = new DatabaseManager(getApplicationContext());
    manager.open();


    listView = (ListView) findViewById(R.id.listViewId);
    listView.setEmptyView(findViewById(R.id.empty));
    adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row_item, cursor, from, to, 0);
    //adapter.notifyDataSetChanged();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(DetailsActivity.this);
            LayoutInflater inflater = DetailsActivity.this.getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
            dialogBuilder.setView(dialogView);
            final EditText name = (EditText) dialogView.findViewById(R.id.dialogEditNmID);
            final EditText phone = (EditText) dialogView.findViewById(R.id.dialogEditPhID);

            dialogBuilder.setTitle("Add Details");
            dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (!TextUtils.isEmpty(name.getText().toString()) &&
                            !TextUtils.isEmpty(phone.getText().toString())) {
                        adapter.notifyDataSetChanged();
                        manager.insert(name.getText().toString(), phone.getText().toString());
                        Toast.makeText(getApplicationContext(),
                                "Added " + name.getText().toString(), Toast.LENGTH_SHORT).show();

                        Cursor cursor = manager.fetch();

                        listView.setAdapter(adapter);
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Empty field(s)", Toast.LENGTH_SHORT).show();
                    }

                }
            });

            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            AlertDialog b = dialogBuilder.create();
            b.show();
            //  listView.setAdapter(adapter);
            //adapter.notifyDataSetChanged();
        }
    });
}}

答案 2 :(得分:1)

在数据库中插入条目后,应再次获取数据,以便列表中包含最新条目。因此,您可以修改代码,以便能够将数据点添加到要传递给适配器的列表,或者在插入之后和notifyDatasetChanged()之前从数据库中重新获取数据。

答案 3 :(得分:0)

尝试在插入操作后使用notifyDataSetChanged。在您致电(尝试致电)的所有地方,通知经理数据尚未改变。