user6863953
user6863953

Reputation:

How to refresh RecyclerView from button on DialogFragment

I have an application with a RecyclerView and a DialogFragment, in the Dialog I add data to the database and display it in the RecyclerView. I tried to refresh the RecyclerView when I clicked in to add.

This is the Fragment

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();
}

}

and this is the 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();
    }
}

Upvotes: 2

Views: 2139

Answers (2)

amorenew
amorenew

Reputation: 10896

First Solution

cast activity and call your method like this:

in your activity add this method

public void myMethod(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
//your code here
        }
    });
}

in your DialogFragment call the activity method after you cast it.

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

Second Solution

add eventbus to your gradle

compile 'org.greenrobot:eventbus:3.0.0'

make a class that you want to pass to activity

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

listen to events in your activity

// 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();
}

register EventBus in your Activity

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

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

finally send in Event from Dialog to your Activity

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

don't forgot adding this to proguard file

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

Upvotes: 0

Rahul Khurana
Rahul Khurana

Reputation: 8835

Call mAdapter.notifyDataSetChanged(); in your Activity's onResume() method.

Upvotes: 2

Related Questions