Android - exit activity on item click in recycler view - android

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.

Related

GetSelectedItem on OnItemClickListener (Android)

let's say you have a listview with custom xml-objects on it in a row.
But you want to address specifically the textview, if it's press in the OnItemClickListener. What's the way - or bette - the best practise to do so? If i check in the OnItemClick method, the specifically element in the row (e.g. the textview, doesn't get recognized.
You can add onClickListener to that textView in the getView method of your adapter like this.
viewHolder.myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});
For this to work create a class which extends BaseAdapter class and implements all its method
1.Now in the getView() method of this class initialize all the required views in a single item of listview
2.After that attach onClickListener() to the desired textView.
3.After that select your custom adapter class in the listView.setAdapter() method
This should help you if it doesn't work use RecyclerView instead and do all the above task in the onCreateViewHolder() method of RecyclerView.adapter Class
if you have model class name **User** now you can get user info by using
viewHolder.tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
User user = data.get(pos);
user.getTitle();
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});

How to get context of the implemented callback inside a method of a fragment

I've defined the next header for a Fragment:
public class AllVideosFragment extends Fragment implements AutomaticCallback{
Inside of this Fragment, I have the next function to handle a spinner:
// Listener called when spinner item selected
spinnerLanguages.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// Get selected row data to show on screen
String language = ((TextView) v.findViewById(R.id.tvSpinnerRow)).getText().toString();
if(language.equals(getString(R.string.str_en)))
Hawk.put(Config.TV_LANGUAGE, "en");
if(language.equals(getString(R.string.str_es)))
Hawk.put(Config.TV_LANGUAGE, "es");
//We need to retrive data again
new AutomaticRequest().getLives(String.valueOf(Hawk.get(Config.TV_LANGUAGE)), **CONTEXT OF THE CALLBACK**);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
If I would have the method outside of the spinner handle method, I could use "this", but inside I don't know what I need to use.
new AutomaticRequest().getLives(String.valueOf(Hawk.get(Config.TV_LANGUAGE)), this);
Which method I need to call in replacement of "this"?
Thank you for your help.
You are in an anonymous scope and this will refer to the scope. If you want to pass an outer this you have to explicitly name it in this case
AllVideosFragment.this
This will give you the current callback's context, FragmentName.this will refer to fragment context while getActivity() refers to the activity context on which your fragment is being hosted. For your ease simple make a Context context in global scope, initialise it in fragments' onViewCreated() method like, context = getActivity() and use it anywhere in your fragment.
the answer is:
new AutomaticRequest().getLives(String.valueOf(Hawk.get(Config.TV_LANGUAGE)), getActivity().this);
AllVideosFragment.this.getActivity();

How to Get a callback for activity started from inside arrayadapter for listview in Android?

How to start an activity from inside ArrayAdapter and get a call back when the activity is finished(just like onActivityResult)?
Following code is on post execute of an asynctask that is started on the button click of a button displayed in each listview row.
((Activity) mContext).startActivityForResult(intent, AppConstants.DUMMY_CONSTANT);
If it's possible, try using this instead:
listView.setOnItemClickListener(new OnItemClickListener() { ... });
Edit. You can also create interface, implement it in your acitvity and pass it to your adapter
My Pscudoe code:-- Create call back method using interface
and make call back wherever you want
MyListener listener;
public interface MyListener {
// TODO: Update argument type and name
void onClick(View view, int position);
}
#Override
public void onClick(View view) {
listener.onCardClick(view, getPosition());
}
From Listview onitem:---

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

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.

Get the value of a single recyclerview item when click and send it to dialog box

I want to get all the value of a single item on recyclerview and pass it to a dialog. a dialog comes when I click that single item.
Create own callback. Example:
public interface OnItemClicked {
onItemClicked(Item item);
}
Your activity or fragment could implement that interface. In implementation you can show a dialog.
Next you need provide a reference of object which implement that interface first to the adapter ( can be by constructor) and then to the viewHolder ( can be by method in bindingView() method)
Then, when you pass a reference to viewHolder.
You can register views in your viewholder to listen onClickEvent. Your ViewHolder will implement OnClickListener.
Example:
public static class MyViewHolder extends ViewHolder implements OnClickListener{
OnItemClicked listener;
Item item;
public void methodWhereYouCreateView(){
view.setOnClickListener(this);
}
public void onClick(View view){
listener.onItemClicked(item);
}
}
I finally solve this with setTag() and getTag(). Thank you for your answer guys.

Categories

Resources