Change content of a DialogFragment from a Recyclerview Adapter - android

I'm not quite sure if this is possible, but I'm trying to change the content of a custom DialogFragment (read: content as in 1 imageview, and 3 textviews) inside of a Recyclerview adapter.
My current custom DialogFragment:
public class ServiceDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.service_card, container, false);
}
}
And the layout:
As you see in the image above: I have a list behind the where the DialogFragment is displayed and whenever I press one of those "buttons" that DialogFragment appears. But my question is: how do I change the content of the custom DialogFragment inside of my Recyclerview Adapter?
My adapter:
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder>{
private final String TAG = "******* Service Adapter";
private ArrayList<ServiceObject> mDataset;
private Context mContext;
private Resources mResources;
private Context location;
public ServiceAdapter(ArrayList<ServiceObject> mDataset, Context mContext, Resources mResources) {
this.mDataset = mDataset;
this.mContext = mContext;
this.mResources = mResources;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
location = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_box, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(mDataset.get(position).getTitle());
holder.icon.setImageResource(mResources.getIdentifier(Integer.toString(mDataset.get(position).getIcon()), "drawable", "net.example.adapter"));
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title, desc, price;
ImageView icon;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.service_box_title);
price = (TextView) v.findViewById(R.id.service_card_price);
icon = (ImageView) v.findViewById(R.id.service_box_icon);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
FragmentActivity activity = (FragmentActivity)(mContext);
FragmentManager fm = activity.getSupportFragmentManager();
ServiceDialogFragment alertDialog = new ServiceDialogFragment();
alertDialog.show(fm, "display_service");
}
}
}

Thanks to #raghunandan, I figured it out. Resource: http://developer.android.com/reference/android/app/DialogFragment.html

Related

In Recylerview Adapter Class I am adding item in arraylist and want to use it in another recylerview adapter class

I am adding data into an addToSepetims arraylist and I want to use it in another fragment but I cant get this arraylist
public class productAdapter extends RecyclerView.Adapter<productAdapter.ViewHolder> {
ArrayList<product_bilgileri> product_bilgileris = new ArrayList<product_bilgileri>();
LayoutInflater layoutInflater;
Context context;
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
public productAdapter(ArrayList<product_bilgileri> product_bilgileris, Context context) {
this.product_bilgileris = product_bilgileris;
this.context = context;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.productName.setText(product_bilgileris.get(position).getProductname());
holder.description.setText(product_bilgileris.get(position).getDescription());
holder.imageView.setImageDrawable(context.getResources().getDrawable(product_bilgileris.get(position).getImage()));
holder.ratingBar.setRating((product_bilgileris.get(position).getRating()));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a =holder.productName.getText().toString();
String b =holder.description.getText().toString();
int c = R.drawable.iphone5;
//Toast.makeText(context, ""+a, Toast.LENGTH_SHORT).show();
addToSepetims.add(new AddToSepetim(a,b,c));
}
});
}
This is Where I want to use addToSepetims arraylist :
public class SepetimFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
public SepetimFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sepetim, container, false);
recyclerView = view.findViewById(R.id.products_recylerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
SepetimAdapter sepetimAdapter = new SepetimAdapter(addToSepetims,getActivity());
recyclerView.setAdapter(sepetimAdapter);
return view;
}
}
I have also SepetimAdapter Class
public class SepetimAdapter extends RecyclerView.Adapter<SepetimAdapter.ViewHolder> {
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
LayoutInflater layoutInflater;
public SepetimAdapter(ArrayList<AddToSepetim> addToSepetims, Context context) {
this.addToSepetims = addToSepetims;
this.context = context;
}
Context context;
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.row_product_sepetim,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.productName.setText(addToSepetims.get(position).getProductname());
holder.description.setText(addToSepetims.get(position).getDescription());
holder.imageView.setImageDrawable(context.getResources().getDrawable(addToSepetims.get(position).getImage()));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// addToSepetims.remove(addToSepetims.get(position));
}
});
}
#Override
public int getItemCount() {
return addToSepetims.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView productName,description;
ImageView imageView;
Button button;
public ViewHolder(#NonNull View itemView) {
super(itemView);
productName = itemView.findViewById(R.id.product_Name);
description = itemView.findViewById(R.id.descripton);
imageView = itemView.findViewById(R.id.product_pic);
button = itemView.findViewById(R.id.deleteFromCard);
}
}
}
Second QUESTION :
Product adapter class in onBindViewHolder class :
int c = R.drawable.iphone5;
in this line I want to get holder.imageView instead of "R.drawable.iphone5"
How can I do that ?
You can share data between fragments through a ViewModel:
Add dependency in build.gradle (Module:app) level.
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
implementation "android.arch.lifecycle:runtime:2.2.0"
Create a custom ViewModel class that extends from AndroidViewModel
Here added the list you want to be shared between fragments.
public class MainViewModel extends AndroidViewModel {
private ArrayList<product_bilgileri> products;
public TempViewModel(#NonNull Application application) {
super(application);
}
public ArrayList<product_bilgileri> getProducts() {
return products;
}
public void setProducts(ArrayList<product_bilgileri> products) {
this.products = products;
}
}
Instantiate the ViewModel instance in activity/fragment
// In activity
MainViewModel mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
// In fragment
MainViewModel mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
Then set the list of data with mViewModel.setProducts(myList) and retrieve it with mViewModel.getProducts()
For more info, check the documentation. Also check this article.

Need explanation on sending data from adapter to fragment

I want to send data from an adapter to a fragment that is already open.
In the main activity, there are two fragments already open, fragment A and fragment B. In fragment A, I have a RecyclerView and CardView placed. When one of the card views is clicked, I want to send its data to fragment B. And I get an NPE when calling mCallbacks.
if (mCallbacks != null){
mCallbacks.onItemClicked(strStockName);}
In this case, I used an interface. But I need more explanation about it.
Here is my adapter's code:
public class adapterStock extends RecyclerView.Adapter<adapterStock.ViewHolder> {
private String[] stockNameDataSet;
private Context mContext;
//==============================================
//---- interface
public interface Callbacks{
public void onItemClicked(String StockName);
}
private Callbacks mCallbacks;
//==============================================
public adapterStock(Context context, String[] DataSet){
stockNameDataSet = DataSet;
mContext = context;
}
#NonNull
#Override
public adapterStock.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
View view = layoutInflater.inflate(R.layout.cv_stock, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final String strStockName = stockNameDataSet[position];
holder.tbxStockName.setText(stockNameDataSet[position]);
holder.cardView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Snackbar.make(view, "Stock "+strStockName, Snackbar.LENGTH_LONG).show();
if (mCallbacks != null){
mCallbacks.onItemClicked(strStockName);
}
}
});
}
#Override
public int getItemCount() {
return stockNameDataSet.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public View view;
public TextView tbxStockName;
public CardView cardView;
public ViewHolder(View v){
super (v);
tbxStockName = (TextView) v.findViewById(R.id.cvTbxItemStockName);
cardView = (CardView) v.findViewById(R.id.cvStock);
}
}
public void setCallbacks(Callbacks callbacks){
this.mCallbacks = callbacks;
}
}
And here is fragment A:
public class AFrag extends Fragment implements adapterStock.Callbacks {
private String strStockName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_A, container, false);
tbxStock = (TextView) view.findViewById(R.id.tbxR);
return view;
}
#Override
public void onItemClicked(String StockName) {
Toast.makeText(getActivity(), "Stock terpilih " + StockName, Toast.LENGTH_LONG).show();
tbxStock.setText(strStockName);
}
}
Suggestions are appreciated. Thanks.
Include, setCallbacks(this) in your fragment A once adapterStock instance is created.
setCallbacks(this) in your fragment
Change this
public adapterStock(Context context, String[] DataSet){
stockNameDataSet = DataSet;
mContext = context;
}
to this
public adapterStock(Context context, String[] DataSet, Callbacks mCallbacks){
stockNameDataSet = DataSet;
mContext = context;
this.mCallbacks=mCallbacks;
}
in fragment's adapterStock instance, pass the mCallbacks as this (i.e., new adapterStock(context, dataset,this))
note: two other answers also correct

RecyclerView on a fragment

i am trying to add a recyclerview on a fragment
fragments code:
public class MediaPlayerController extends Fragment{
private static final String TAG = "MediaPlayerController";
private RecyclerView recyclerViewb;
private RecycleViewAdapter myAdapter;
private ArrayList<String> myRecordings = new ArrayList<>();
//private DBHelper dbHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: started");
View v = inflater.inflate(R.layout.media_player_area, container, false);
recyclerViewb = (RecyclerView)v.findViewById(R.id.recyclerViewXml);
//dbHelper = new DBHelper(v.getContext(), null, null, 1);
myRecordings.add("hau");
myRecordings.add("hau2");
myRecordings.add("hau3");
myRecordings.add("hau4");
myRecordings.add("ha5");
myRecordings.add("hau23");
myRecordings.add("ha31u");
myRecordings.add("haudsa");
System.out.println("what does this print?"+myRecordings);
myAdapter = new RecycleViewAdapter(v.getContext(), myRecordings);
recyclerViewb.setAdapter(myAdapter);
recyclerViewb.setLayoutManager(new LinearLayoutManager(v.getContext()));
Log.d(TAG, "onCreateView: returned");
return v;
}
in the logs i see that that it doesn't even get to the RecycleViewAdapter sections of code at all the fragment is completely blank
i get not errors as well
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private static final String TAG = "RecycleViewAdapter";
private Context mContext;
private ArrayList<String> mRecName = new ArrayList<>();
public RecycleViewAdapter(Context mContext, ArrayList<String> mRecName) {
this.mContext = mContext;
this.mRecName = mRecName;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: i am here");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_recycle, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.recTxt.setText(mRecName.get(position).toString());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "sas gamaw", Toast.LENGTH_LONG);
}
});
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RelativeLayout parentLayout;
TextView recTxt;
public ViewHolder(View itemView) {
super(itemView);
recTxt = itemView.findViewById(R.id.recTextView);
parentLayout = itemView.findViewById(R.id.parentLayoutXml);
}
}
}
in the logs it only displays oncreateView started and the myRecordings arraylist
onCreateView: started
I/System.out: what does this print?[hau, hau2, hau3, hau4, ha5,
hau23, ha31u, haudsa]
MediaPlayerController: onCreateView: returned
i can't think of what is going wrong :/ please help out
i also want to add that i am fairly new to android studio thanks
your code problem it is in Recycler Adapter in getItemCount() method,
You don't return any Item , you must change it to :
#Override
public int getItemCount() {
return mRecName.size();
}

How to grey out fragment activity in android on condition

I want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}

Toast message onClick NOT SHOWING (RecycleView)

Im trying show a toast msg when clicked on a item from my RecycleView, Ive tried many examples,
but its not giving me anything. Can somebody give me a different example that i can follow, at the end i wanna set the onClick to show a new fragment. If I can get an example on that, it will be great.
Im using this code:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private List<Movie> movies;
private int card_layout;
private Context mContext;
public MovieAdapter(List<Movie> movies, int card_layout, Context context) {
this.movies = movies;
this.card_layout = card_layout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(card_layout, viewGroup, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final Movie movie = movies.get(i);
viewHolder.movieImage.setImageDrawable(mContext.getDrawable(movie.getImageResourceId(mContext)));
viewHolder.movieName.setText(movie.mName);
viewHolder.currentMovie = movie;
}
#Override
public int getItemCount(){
return movies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView movieName;
public ImageView movieImage;
public Movie currentMovie;
public ViewHolder( View itemView) {
super(itemView);
movieName = (TextView) itemView.findViewById(R.id.movieName);
movieImage = (ImageView)itemView.findViewById(R.id.movieImage);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View itemView){
Toast.makeText(itemView.getContext(),currentMovie.mName,Toast.LENGTH_SHORT ).show();
}
});
}
}
}
Do I have to implement something in my MainActivity as well?
and please dont get mad with me, Im just a starting with all this. All your help will be appriciated. thanks
Layout is not clickable by default. to make clickable add setClickable to true :
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
card_layout, viewGroup, false);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
After wasting an hour of time, I found the most appropriate and simplest solution to this problem:
Try this, it will definitely work.No matters whether cards are used in grids or not.
RecyclerView Adapter:
ProductCardRecyclerViewAdapter.java
public class ProductCardRecyclerViewAdapter extends RecyclerView.Adapter<ProductCardViewHolder> {
public final String TAG=getClass().getSimpleName();
Context context;
private List<ProductEntry> productList;
private Integer[] cardImages;
String[] cardTitle;
String[] cardSubtitle;
public ProductCardRecyclerViewAdapter(Context context, Integer[] imageList, String[] cardTitle, String[] cardSubtitle) {
this.cardImages = imageList;
this.cardTitle = cardTitle;
this.cardSubtitle = cardSubtitle;
this.context = context;
}
#NonNull
#Override
public ProductCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card, parent, false);
return new ProductCardViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull ProductCardViewHolder holder, int position) {
// TODO: Put Recycler ViewHolder Cards binding code here in MDC-102
holder.imgCard.setImageResource(cardImages[position]);
holder.productTitle.setText(cardTitle[position]);
holder.productPrice.setText(cardSubtitle[position]);
holder.productCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Material Card clicked "+cardTitle[position]+" : "+context.getClass());
//TODO: Perform card clicked working
Context c = v.getContext();
Toast.makeText(c, cardTitle[position], Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return cardImages.length;
}
}
RecyclerViewHolder
ProductCardViewHolder.java
public class ProductCardViewHolder extends RecyclerView.ViewHolder {
CardView productCard;
ImageView imgCard;
public TextView productTitle;
public TextView productPrice;
public ProductCardViewHolder(#NonNull View itemView) {
super(itemView);
imgCard = itemView.findViewById(R.id.product_image);
productTitle = itemView.findViewById(R.id.product_title);
productPrice = itemView.findViewById(R.id.product_price);
productCard=itemView.findViewById(R.id.cardofproducts);
// TODO: Find and store views from itemView
}
}
Hope, it will help a lot.

Categories

Resources