I know that notifyItemInserted(position) is used, but in most of the examples I have seen it gets triggered with help of Click Listeners.
But in my case I want adapter to know the change and update its view when a button in another activity is pressed.
How can I achieve this?
Consider below example scenario:
1) App starts with Activity A
2) Activity A contains recyclerview
3) As Currently data is empty no items is shown in recyclerview
4) Somehow I got into Activity B
4) I updated the data and pressed Button
5) As new data is there, recyclerview is now having a single view with updated data
Open activity B with startActivityForResult intent
Come back From Activity B with your data
In Activity A onActivityResult update your data and notify your adapter
If it suits you, make a global instance of List of data model you want to update RecyclerView from.
List<DataModel> dataModelList = new ArrayList<>();
You can do this in a class extending Application class or somewhere else where you want.
Now in ActivityA
public class ActivityA extends AppCompatActivity {
YourAdapter adapter;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new YourAdapter(YourClassWhereYouPutDataModelList.dataModelList)
recyclerView.setAdapter(adapter);
#Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
}
Now in your ActivityB
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code to get value
YourClassWhereYouPutDataModelList.dataModelList.add(YourValue);
//Now it's up to you, either finish it using finish() or continue working
//Whenever you go to ActivityA, RecyclerView will be updated
}
});
}
}
Related
In android studio, I can refresh the activity using the following method. When Button onClick, it refreshData at current activity.
public void refreshData(){
Intent intent = getIntent();
finish();
startActivity(intent);
}
Now the thing I want to do is, when Button onclick, it jump from Activity A.class to B.class, and instantly refresh B.class. I needed it because of pulling database data at first time .
In order words I need refresh second activity when any activity jump to it.
Second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
getData(); //get database data
addData(); //set data to variable
refreshData(); // I wish to refresh the Activity 2 Interface
}
Just call your database entries from Activity B's onCreate() method, so that every time Activity B is loaded (called from any Activity), the database entries are re-fetched and filled into the UI. Pretty straight forward, isn't it?
Create custom method for pulling database data in second activity .
then call the custom method in oncreate of second activity .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getdataMethod();// pulling database data
}
I have two views In a single activity StackView and Gridview and 2 corresponding adapters .when I clicked on StackView item it should flip the grid item in Gridview and vice versa?
// activity classs
public class SampleActivity extends Activity implements
OnGridChangeListener {
public void onCreate(bundle){
// replace this with your adapter class.
Adapter adapter = new adapter(this);
}
#override
public void OnGridChange(){
//here you go.
// write code to do what you want.
}
// interface to communicate with activity
public interface OnGridChangeListener {
public void OnGridChange()
}
// adaptor class
public class Adaptor extends "you apapter class to extend"{
OnGridChangeListener onGridChangeListener ;
public Adapter(OnGridChangeListener listener){
onGridChangeListener =listener
}
public getView(){
public void onclick(){
onGridChangeListener.OnGridChange("pass you data");
}
}
}
As per your question this is what i get -
You have a Activity with two independent views which has their own adapters. So when there is changes in one of the adapter you want it to be reflected into another adapter.
The simple solution for your query would be -
When there is a change in first adapter you reflect the change to the activity. after that call the function in the second adapter to reflect the change you want in second adapter.
For this you have to define an interface in first adapter and implement this is activity.
When the first adapter changes call the interface method and this will reflect in activity.
Then call method in second adapter to do the changes you want.
Code example -
MainActivity.Java
public class MainActivity extends AppCompatActivity implements FirstAdapter.callBackMethods {
FirstAdapter firstAdapter;
SecondAdapter secondAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstAdapter = new FirstAdapter(MainAtivity.this.getApplicationContext());
//do all the declaration and operation of first adapter. Pass context along with your required params.
secondAdapter = new SecondAdapter();
//do all the declaration and operation of second adapter.
}
//callback method of first adapter
#override
public void callback(){
//changes have been done in FirstAdapter and this methos is fired.
//now do do the changes in SecondAdapter as per req.
if(secondAdapter != null){
secondAdapter.reflectChanges();
}
}
}
FirstAdapter.class
I am taking example of recylerview adapter.
public class FirstAdapter extends RecyclerView.Adapter<FirstAdapter.ViewHolder>{
public FirstAdapter(Context context){
this.context=context;
}
/*
all the boilerplate codes and business logic
*/
//when you want to reflect the changes
callBackMethods callBackMethods = (callBackMethods) context;
callBackMethods.callback();
//this will fireup the implementation in the MainActivity.
public interface callBackMethods{
public void callback();
}
}
SecondAdapter.class
This is where the changes will be reflected.
public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{
/*
all the boilerplate codes and business logic
*/
public void reflectChanges(){
/*
This method will be called from the MainActivity. pass whatever params you want to pass from Activity and do the changes you want to do in the SecondAdapter.
*/
}
}
I hope this solves your problem. Happy coding...
I have an activity that contains listview, inside listview rows i have a checkbox in each row. every time i check or uncheck the checkbox the activity should listen immediately and knows how many rows is checked or unchecked, how do I implement it in android? thanks before
You should create Listener like below.
interface CheckBoxCleckListener{
void OnCheckboxClicked();
}
Define above code in your adapter class or other.
Now whenever you want to call it, write following code in your Adapter class.
//this will create object of listener
public static CheckBoxCleckListener checkBoxCleckListener;
You need to initialized it by your caller class which is your Activity in your case.
public static void addListener(
CheckBoxCleckListener listener) {
checkBoxCleckListener= listener;
}
Now, whenever your check box clicked, write following code inside,
checkBoxCleckListener.OnCheckboxClicked();
In your activity class, write following code,
public class YourActivity extends Activity implements CheckBoxCleckListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//rest of code
YOUR_CLASS_WHERE_INTERFACE_IMPLEMENTED.addListener(this);
}
#Override
public void OnCheckboxClicked() {
//do your coding
}
}
I am using a Fragment class.
I add it using
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Search fragment = new Search(maincontrolActivity.this);
fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
fragmentTransaction.commit();
this.FragmentObject = fragment
when I do refresh the control,I recall this code but by passing this.FragmentObject but I think it be garbage collected because the = refere to the same object , and when say add, it free the old fragement which is the same
so do I need a deep copy or any way to refresh ?
any idea
Ok. So what I would do is have an Interface defined and have each Fragment register with the Activity for a callback when the refresh button is clicked. Then in the fragment itself have it refresh its data. If that means getting new data, switch to a ProgressBar, get the data from the server, and repopulate the Views. Here is an entire article on creating Interfaces in Activities and calling Fragments from them.
Here is roughly what your code will look like...
The Activity:
public class RefreshActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.refresh_button).setOnClickListener(this);
}
public interface OnRefreshPressedListener {
public void onRefreshPressed();
}
#Override
public void onClick(View v) {
((OnRefreshPressedListener)this.FragmentObject).onRefreshPressed();
}
}
The Fragment:
public class Search extends Fragment implements OnRefreshPressedListener {
#Override
public void onRefreshPressed() {
//TODO: Refresh your data!
}
}
I have an activity group and it starts 2 activities. When the user presses a button on one of the activities, the activity group populates an ArrayList.
I am wondering if there is a way to allow both of my activities to access this ArrayList.
Here's what I have at the moment:
public class ExampleGroup extends ActivityGroup {
public static ExampleGroup group;
ArrayList<String> strs = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
group = this;
View exampleView = getLocalActivityManager().startActivity(
"Example",
new Intent(this, Example.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(exampleView);
}
public void populateArrayList(){
//code to do it
}
}
public class Example extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
ExampleGroup.group.populateArrayList();
ArrayList<String> strs2 = ExampleGroup.group.strs;
Log.i("ArrayList contents", strs2);
}
}
The arraylist returns null. Is there something I am missing, or is there a better way to do it?
Yes essentially you're wanting to share a model object between two activities, and this has much to do with the structure of your program. See this post for more details on how that can be done:
Where should I put global methods and variables in an Android app?