How to refresh a view in main activity from an adapter? - android

I have a graph in main activity also I have a recycler view in main activity. Custom adapter is used for recyclerview. I have a check box and swipe layout in list item layout. in swipe layout there is a delete button.
I want to reset the graph of main activity when I check the check box or when I delete any item.
For this I created one method in main activity. And called this method in adapter onCheckedChangeListener and on click of delete.
But I am getting a null pointer exception on mBarChart. i.e . graph. I have instantiated in mBarChart in setUI method and this is called in onCreate of an activity.
resetMethod
public void resetGraph(Context context)
{
mBarChart.invalidate();
}
in adapter :
Context conext;
MainActivity mainActivity;
mainActivity = new MainActivity();
mainActivity.resetGraph(conext);
How to do this? Please help.. Thank you..

In Adapter call your resetMethod this way
((MainActivity)context).resetGraph(context);

Create a interface that implement Activity, Main activity in your case and override method and perform operation.
//Interface
public interface OnRefreshViewListner{
public void refreshView();
}
//Main Activity
MainActivity extends Activity implements OnRefreshViewListner
{
//Other methods
#Override
public void refreshView(){
// write refresh code here
}
}
//Initialize Interface in adapter constructor
public class YourAdapter extends BaseAdapter {
private OnRefreshViewListner mRefreshListner;
public YourAdapter (Context context) {
mRefreshListner = (OnRefreshViewListner)context;
}
//call MainActivity method
mRefreshListner.refreshView();
}

In the adapter, you should not create a new instance of MainActivity and call resetGraph(). You should use the instance of MainActivity, that created the adapter. Send the instance of MainActivity to the adapter, new Adapter(this) and save it in adapter.

You can change a view from the context of an adapter like this :
cast context to activity.
use findviewbyid method to find the view you want.
initiliaze it to a variable.
View v = ((Activity)getContext()).findViewById(WHATEVER_VIEW_COMPONENT_YOU_WANT);
change the variable as you want.
note. Don't forget to use the type of view that you want and cast the findview method to it.
If you want to call a method just cast the context to MainActivity and call it.

Related

Android change UI from adapter

In my activity I have one recyclerview and each item view contains buttons. I want to be able to change some UI elements and other things such as an Array of custom objects for the adapter itself in my activity from the recyclerview adapter. Until now I declared the needed views as static but I found out that it's a terrible practice.
Example: I have the following recyclerview that represents a cart filled with a custom viewholder, from an Array of custom "cart_product" objects. (One of this custom oject's proprieties is "quantity" - represented by the spinner). I want to be able to change the object's "quantity" property by changing the spinner's value from the adapter... How could this be done? And when all the products are removed from cart (by swiping & detected from adapter) I want to display a textvie
ScreenShot
You can use callbacks:
In adapter create an interface:
public interface EventHandler {
void handle(int position) // if u need know position. If no, just create method without params
}
Create an private instance of interface in adapter:
public class YourAdapter extends RecyclerView.Adapter<YourHolder> {
private EventHanlder handler;
}
Implement EventHanlder in activity:
public class Mainacitivity extends Activity implements YourAdapter.EventHandler {
//.....
#Override
void handle (int position) {
// TODO do whatever u want
}
}
Add EventHandler to constructor parameters:
public YourAdapter (List<YourObject> data, EventHandler handler) {
//....
this.handler = handler;
}
When you need to change UI call
handler.hanlde(position);
And, finally pass this when initializing adapter
adapter = new YourAdapter (data, this)
If u need something else (not position), just change signature of handle() method

I have an activity with recycler view adapter class, when button clicked in adapter class I want to refresh the main activity

I have an activity with RecyclerView.Adapter class. RecyclerView contains button. When click that button I want to change a value defined in main activity and display it in TextView that also defined in main activity , otherwise I want to refresh the activity.
I already tried some code like:
Android, How to call onCreate() explicitly from other method?
How to restart the onCreate function
YourActivity extends Activity implements YourAdapter.ClickCallback{
#override
oncreate(Bundle bundle){
// initialize views
YourAdapter adapter = new YourAdapter(this);
// do something with your adapter
}
#Override
public void updateView(){
//update your views here
}
}
//In Recycler Adapter pass ClickCallBack As parameter
class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ClickCallBack clickCallBack;
public YourAdapter(ClickCallBack clickCallBack){
this.clickCallBack=clickCallBack;
}
//ClickCallback Interface
interface ClickCallBack {
void updateView();
}
//In your ButtonClick
clickCallBack.updateView();
}
You can also add parameters in updateView method.
Pass an instance of those TextView through constructor and update the value there in the Adaptor. That would be the easiest way.
You don't need to recreate Activity in this case. The best solution would be:
Declare an interface.
Implement it in Activity(here implement your updates to TextView, etc.).
Pass object that implements the interface to your adapter.
Call the method on this interface in adapter.
Here is example:
https://stackoverflow.com/a/32720879/2528967

I have 3 buttons inside recyclerview whose click event is to be set on main activity and not in adapter class?

How can I make onclick listener of recyclerview button in main activity and perform click from main activity.
create a interface in your adapter class and implement it in your activity class.
public interface handleClick
{
public void onFirstButtonClick();
public void onSecondButtonClick();
public void onThirdButtonClick();
}
and when creating your adapter in your activity class pass a additional parameter YourAdapter(yourparamters,this)
and now in your adapter
when you intialize your values from construcor assign this to your inteface
Adapter Class
Youradapter(yourParamters,handleClick)
{
this.handleClick=handleClick;
}
now in your onBindViewHolder create onClickListener for your buttons and on
onClick call interface methods like this
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
handleClick.onFirstButtonClick();
}
});)
so the method onFirstButtonClick which you will have to implement in your activity class will be called.
You have to do following thing
Firstly create an interface for listening on click event from adapter to activity.
Second implement that interface in your activity.
Third create the instance of your interface in your activity and pass it to adapter.
Four in adapter call your method of interface using instance received at adapter and do your work in your activity.If you will upload code then i will correct.

Android - exit activity on item click in recycler view

I've got a bunch of list items in my RecyclerView. I am handling the clicks properly for each item, but I need to close the activity when any item is clicked. Becuase RecyclerView doesn't have setOnItemClickListener method, I have to do this within the adapter:
#Override
public void onBindViewHolder(final Holder holder, int position) {
// ...
holder.flagNameTextView.setText(arrayList.get(position).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Go back to the previous activity
// such as onBackPressed();
}
});
}
But of course, the adapter is not extending the Activity, so I can't use onBackPressed() or finish().
How can I do this?
Use the Following:
Declare context globally.
Context c;
Then type-cast context with your activity.
((YourActivity)c).finish();
Hope this helps.
You can pass a reference of your activity throw the adapter constructor and call Activity.finish();
Hope this Helps.
Sorry for my english.
You can get the context from the flagNameTextView :
((Activity)holder.flagNameTextView.getContext()).finish();
Besides, you can define a interface callback like OnClickListener in RecyclerView adapter. Then Implement this interface in Activity, pass it into the adapter and then use it in View's OnClickListner callback.
Just use YourActivityName.this.finish().
By adding the Activity name you are telling android which "this" you want.
This will only work if your adapter is NOT defined as a static class.

Android: Inform main activity about CheckBox of custom ListView

I have a Custom BaseAdapter showing a ListView containing a TextView and a CheckBox. The TextView has an OnClickListener implemented to perform a specific action when clicked on the text. Within the Adapter I have a OnCheckedChangeListener registered which keeps track of which item is checked (because of the ListView recycling).
I want to start an ActionMode when I check a CheckBox and stop it when I uncheck it.
How can I inform the main activity hosting the adapter that a check has been made?
Create a listener call back interface, something like:
public interface ListenerCheckBox
{
public void onRowChecked(int rowNun);
}
Then, make your main activity implement this listener:
public class ActivityMain implements ListenerCheckBox
Then, when you instantiate your custom BaseAdapter, pass in the listener:
//kv 3rd parameter would be listener
CustomBaseAdapter customBaseAdapter = new CustomBaseAdapter(this, items, this);
Then, in the constructor of your CustomBaseAdapter, set a member field to the listener:
public CustomBaseAdapter(Context context, ArrayList<Item> items, ListenerCheckBox listenerCheckBox)
{
mListenerCheckBox = listenerCheckBox;
...
}
Then, every time an item is checked, call:
mListenerCheckBox.onRowChecked(rowNum);
Here is a Android Studio project that shows an example of how to do what you are asking. In particular, check out the check listener and its onClick():
#Override
public void onClick(View v) {
if (mMode != null) {
mMode = mListView.startActionMode(new ExampleMultiChoiceModeListener(mActivity));
}
mListView.setItemChecked(mPosition, ((Checkable) v).isChecked());
}

Categories

Resources