在SqliteDatabase中插入条目后,ListView未获得更新

时间:2016-12-23 07:23:47

标签: android sqlite listview

我已经阅读了很多有关此主题的帖子,但没有人提供帮助。我有一个listview我有一个dialogdialog包含三个Edittext字段对于名称,描述和日期。我在对话框中有两个Buttons保存并取消。单击“保存”按钮,将数据插入数据库,但listview未更新。 这是我指定保存按钮功能的方法:

save_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    selectedModel=new Model();
                    selectedModel.setTask_title(task_Title.getText().toString());
                    selectedModel.setTask_description(task_Description.getText().toString());
                    selectedModel.setTask_date(task_Date.getText().toString());
                    db.insertTask(selectedModel);
                    //Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                    //myAdapter.notifyDataSetChanged();
                }
            });

我尝试使用.notifyDataSetChanged(),但它没有用。

这是我指定所有功能的主要代码:

public class SecondScreen extends ListActivity implements AppCompatCallback,AdapterView.OnItemClickListener {

private Toolbar bar;
private AppCompatDelegate delegate;
List<Model> list;
ArrayAdapter<String> myAdapter;
final Context context=this;
Model selectedModel;
AGSQLiteHelper db;

private EditText task_Title,task_Description,task_Date;

@Override
protected void onCreate(Bundle savedInstanceState) {
    delegate=AppCompatDelegate.create(this,this);

    super.onCreate(savedInstanceState);
    delegate.setContentView(R.layout.activity_second_screen);

    bar=(Toolbar)findViewById(R.id.toolbarID);
    delegate.setSupportActionBar(bar);
    ActionBar ab=delegate.getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    db=new AGSQLiteHelper(this);
    db.onUpgrade(db.getWritableDatabase(),1,2);
    db.createTask(new Model("abcde","description abcde","19/11/12"));
    db.createTask(new Model("bcdese","description bcdese","18/12/12"));
    db.createTask(new Model("mnbv","description mnbv","2/11/10"));
    db.createTask(new Model("poiuy","description poiuy","1/5/6"));
    list=db.getAllTasks();
    List<String> listTitle=new ArrayList<String>();
    for(int i=0;i<list.size();i++){
        listTitle.add(i,list.get(i).getTask_title());
    }
    myAdapter=new ArrayAdapter<String>(this,R.layout.row,R.id.tasktitleTextID,listTitle);
    getListView().setOnItemClickListener(this);
    setListAdapter(myAdapter);
}

@Override
public void onSupportActionModeStarted(ActionMode mode) {
}

@Override
public void onSupportActionModeFinished(ActionMode mode) {
}

@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
    return null;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId()==R.id.add){
        Toast.makeText(getApplicationContext(),"Add was clicked",Toast.LENGTH_LONG).show();

        final Dialog dialog=new Dialog(context);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle("Dialog");
        Button save_button=(Button)dialog.findViewById(R.id.saveButtonID);
        Button cancel_button=(Button)dialog.findViewById(R.id.cancelButtonID);

        task_Title=(EditText)dialog.findViewById(R.id.titleEditTextID);
        task_Description=(EditText)dialog.findViewById(R.id.descriptionEditTextID);
        task_Date=(EditText)dialog.findViewById(R.id.dateEditTextID);

        dialog.show();

        save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                selectedModel=new Model();
                selectedModel.setTask_title(task_Title.getText().toString());
                selectedModel.setTask_description(task_Description.getText().toString());
                selectedModel.setTask_date(task_Date.getText().toString());
                db.insertTask(selectedModel);
                //Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
                dialog.dismiss();
                //myAdapter.notifyDataSetChanged();
            }
        });

        cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    if(item.getItemId()==R.id.done){
        Toast.makeText(getApplicationContext(),"Done was clicked",Toast.LENGTH_LONG).show();
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu);
    return true;
}

}

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

您需要将新条目添加到适配器使用的listTitle

db.insertTask(selectedModel);
dialog.dismiss();
listTitle.add(selectedModel.getTask_title());
myAdapter.notifyDataSetChanged();

所以让你的listTitle全局到班级,以便你可以在其他函数中使用它

public class SecondScreen extends ListActivity...{
    private EditText task_Title;
    private List<String> listTitle;
    // declare list like this as global to class 

   ... onCreate(..){
        listTitle=new ArrayList<String>();
       }
}