如何从DialogFragment上的按钮刷新RecyclerView

时间:2016-09-22 10:58:53

标签: android android-fragments android-recyclerview

我有一个RecyclerViewDialogFragment的应用程序,在对话框中我将数据添加到数据库并显示在RecyclerView中。我点击添加时尝试刷新RecyclerView

这是片段

public class addAction extends DialogFragment implements View.OnClickListener {
EditText addTitle, addDesc;
Button add, clear,close;
Context context;
private DatabaseHelpher db;
String Title,Des;
public addAction() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.addaction, container, false);
    addTitle = (EditText) rootView.findViewById(R.id.todotitle);
    addDesc = (EditText) rootView.findViewById(R.id.tododescription);
    add = (Button) rootView.findViewById(R.id.addbutton);
    add.setOnClickListener(this);
    close = (Button) rootView.findViewById(R.id.Close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    });

    clear = (Button) rootView.findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addTitle.setText("");
            addDesc.setText("");
        }
    });
    return rootView;
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getDialog().setTitle("Add Action");
    db = new DatabaseHelpher(getContext());
}
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!");
    }
    insert();
}

}

,这是MainActivity

public class MainActivity extends AppCompatActivity {
    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);
        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();
        dbList = helpher.getDataFromDB();
        mAdapter.notifyDataSetChanged();
    }
}

2 个答案:

答案 0 :(得分:2)

在您的活动的mAdapter.notifyDataSetChanged();方法中调用onResume()

答案 1 :(得分:0)

第一个解决方案

投射活动并按照以下方式调用您的方法:

在您的活动中添加此方法

public void myMethod(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
//your code here
        }
    });
}
在您的DialogFragment中,

在投射后调用activity方法。

 ((YourActivity)getActivity()).myMethod();

第二个解决方案

将eventbus添加到您的gradle

compile 'org.greenrobot:eventbus:3.0.0'

创建一个要传递给活动的课程

public class MessageEvent {
    public final String message;
    public MessageEvent(String message) {
        this.message = message;
    }
}

聆听活动中的事件

// This method will be called when a MessageEvent is posted (in the UI thread for Toast)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}

在您的活动中注册EventBus

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

最后将事件从Dialog发送到您的活动

EventBus.getDefault().post(new MessageEvent("Hello everyone!")); 

不要忘记将此添加到proguard文件

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }