从片段

时间:2016-09-23 21:12:26

标签: android sqlite android-fragments

我有一个带有Dialog Fragment的RecyclerView,我通过Dialog从用户那里获取数据并将其存储在数据库中然后,我在recyclerview中显示它,所有这一切都正常,我的问题是,当我添加数据时我必须重新启动应用程序才能将其显示到回收站,我如何通知适配器数据是否有变化?

对话框片段中的添加按钮

  add = (Button) rootView.findViewById(R.id.addbutton);
    add.setOnClickListener(this);

 private void insert() {
        Title = addTitle.getText().toString();
        Des= addDesc.getText().toString();
        db.insertIntoDB(Title, Des);}
    @Override
    public void onClick(View v) {
        if (addTitle.getText().toString().trim().equals("")) {
            addTitle.setError(" Title is required!");
        } else if (addDesc.getText().toString().trim().equals("")) {
            addDesc.setError(" Postion is required!");
        }

        Toast.makeText(getContext(),"your data is saved", Toast.LENGTH_SHORT).show();
        insert();
    }

这是辅助类

中的getdata方法
public List<ToDoModule> getDataFromDB(){
    List<ToDoModule> modelList = new ArrayList<ToDoModule>();
    String query = "select * from "+ TODO_TABLE;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);
    if (cursor.moveToFirst()){
        do {

            ToDoModule model = new ToDoModule();
            model.setActionTitle(cursor.getString(1));
            model.setActionDesc(cursor.getString(2));
            modelList.add(model);
        }while (cursor.moveToNext());
    }
    return modelList;
}

这是主要的活动

public class MainActivity extends AppCompatActivity {
public List<ToDoModule> dbList;
RecyclerView mRecyclerView;
DatabaseHelpher helpher;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().hide();
    helpher = new DatabaseHelpher(this);
    dbList= new ArrayList<ToDoModule>();
    dbList = helpher.getDataFromDB();
    mRecyclerView = (RecyclerView)findViewById(R.id.AppRecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new RecyclerAdapter(this,dbList);
    this.dbList =helpher.getDataFromDB();
    mAdapter.notifyDataSetChanged();
    mRecyclerView.setAdapter(mAdapter);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setImageResource(R.drawable.ic_action_name);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fm = getSupportFragmentManager();
            addAction add = new addAction();
            add.show(fm,"fragment_edit_name");
        }});}

@Override
protected void onResume() {
    super.onResume();
}
}

1 个答案:

答案 0 :(得分:0)

DialogFragments与所有片段一样,直接与使用它们的Activity的生命周期相关联。因此,在用户取消dialogFragment后,活动不会调用onResume()回调。

为了简单和易维护,我将创建一个界面

public interface DialogFragUpdateListener {
    public void OnDBUpdate();
}

您的活动将实施

public class MainActivity extends AppCompatActivity implements  DialogFragUpdateListener{
每当用户完成您在DialogFragment中执行的操作时,

接收回调。在调用 insert()之后,我会在 onClick()方法中添加此内容:

(DialogFragUpdateListener)getActivity()).OnDBUpdate()

然后在您的活动中,这个回调方法可以从数据库中查询更新的数据并再次更新适配器/ recyclerview:

 public void OnDBUpdate() {
        // Requery data from database here
     getDataFromDB();
    }

查看此内容以供参考: https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity