The RecyclerView has components with text, I need that by clicking on a component that has text for example "Chess", the DialogFragment opens and the text "Chess" from the RecyclerView component is transferred to the TextView which is inside this DialogFragment.
To get a key from my item layout file, I write this:
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
But what needs to be written in the DialogFragment so that our text is transferred there?
I am writing the following in my DialogFragment but it doesn't work
public class DialogSkatingRinkSchedule extends DialogFragment{
TextView number_srs_2;
TextView start_srs_2;
TextView end_srs_2;
TextView cost_srs_2_1;
TextView cost_srs_2_2;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_skatingrinkschedule, container, false);
number_srs_2 = view.findViewById(R.id.number_srs_2);
start_srs_2 = view.findViewById(R.id.start_srs_2);
end_srs_2 = view.findViewById(R.id.end_srs_2);
cost_srs_2_1 = view.findViewById(R.id.cost_srs_2_1);
cost_srs_2_2 = view.findViewById(R.id.cost_srs_2_2);
Bundle getArguments = new Bundle();
number_srs_2 = (TextView) getArguments.getSerializable("item");
return view;
}
}
My adapter
public class AdapterSkatingRinkSchedule extends RecyclerView.Adapter<AdapterSkatingRinkSchedule.SkatingRinkScheduleViewHolder> {
public ArrayList<ItemSkatingRinkSchedule> mSkatingRinkScheduleList;
public List<ItemSkatingRinkSchedule> mSearch;
private AppCompatActivity mActivity;
public AdapterSkatingRinkSchedule(ArrayList<ItemSkatingRinkSchedule> SkatingRinkScheduleList) {
mSkatingRinkScheduleList = SkatingRinkScheduleList;
mSearch = new ArrayList(mSkatingRinkScheduleList);
}
#NonNull
#Override
public SkatingRinkScheduleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_srs, parent, false);
return new SkatingRinkScheduleViewHolder(v);
}
public static class SkatingRinkScheduleViewHolder extends RecyclerView.ViewHolder {
public TextView number_srs, start_time_srs, end_time_srs, cost_1_srs, cost_2_srs;
public TextView number_srs_2;
public LinearLayout ll_main;
public SkatingRinkScheduleViewHolder(View itemView) {
super(itemView);
number_srs = itemView.findViewById(R.id.number_srs);
start_time_srs = itemView.findViewById(R.id.start_time_srs);
end_time_srs = itemView.findViewById(R.id.end_time_srs);
cost_1_srs = itemView.findViewById(R.id.cost_1_srs);
cost_2_srs = itemView.findViewById(R.id.cost_2_srs);
ll_main = itemView.findViewById(R.id.ll_main);
}
}
#SuppressLint("LongLogTag")
#Override
public void onBindViewHolder(SkatingRinkScheduleViewHolder holder, int position) {
ItemSkatingRinkSchedule currentItem = mSkatingRinkScheduleList.get(position);
holder.number_srs.setText(currentItem.get_session_number());
holder.start_time_srs.setText(currentItem.get_session_start());
holder.end_time_srs.setText(currentItem.get_session_end());
holder.cost_1_srs.setText(currentItem.get_weekdays());
holder.cost_2_srs.setText(currentItem.get_weekends_and_holidays());
holder.ll_main.setOnClickListener(v -> {
DialogSkatingRinkSchedule dialogSRS = new DialogSkatingRinkSchedule();
dialogSRS.show(((AppCompatActivity) v.getContext()).getSupportFragmentManager(), "item");
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
});
holder.getAdapterPosition();
}
#Override
public int getItemCount() {
return mSkatingRinkScheduleList.size();
}
}
You can access the arguments Bundle of a Fragment or DialogFragment by calling the method getArguments(). Since you put a String there previously by writing
bundle.putString("item", String.valueOf(dialogSRS));
you should retrieve the value by using getString("item"). Then you can display the text from the Bundle with the TextView as follows:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_skatingrinkschedule, container, false);
number_srs_2 = view.findViewById(R.id.number_srs_2);
// assign other Views like before
Bundle args = getArguments();
String myText = args.getString("item");
number_srs_2.setText(myText);
return view;
}
Related
I have a list using the RecyclerView, what I want is that when I click on the list item, it opens a fragment, I've been searching and I only found it with an activity, but I'm working only with fragments, if I use activity it disappears with my action bar, it's all set in the fragment.
This is my adapter class:
public class CreditCardAdapter extends RecyclerView.Adapter<CreditCardHolder> {
private final List<CreditCard> creditList;
public CreditCardAdapter(List<CreditCard> creditList) {
this.creditList = creditList;
}
#NonNull
#Override
public CreditCardHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.credit_card_item, parent, false);
return new CreditCardHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CreditCardHolder holder, int position) {
CreditCard credit = creditList.get(position);
String flag = credit.getFlag();
holder.flag.setText(flag);
String owner = credit.getOwner();
holder.owner.setText(owner);
String valueLimit = String.format("%.2f", credit.getLimit());
holder.limit.setText(valueLimit.replace(".", ","));
holder.creditCardId = credit.getId();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Method on click
}
});
}
#Override
public int getItemCount() {
return creditList.size();
}
}
I need to send the credit card id to the fragment, to call the controller and get the information from the database, I tried it with the intent, but it only worked if I call the activity with startActivity(intent).
So here it is the fragment class:
public class InfoCreditCardFragment extends Fragment {
public InfoCreditCardFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info_credit_card, container, false);
String id; //Here I need the id from the Adapter
TextView flagInfoTxt = view.findViewById(R.id.flagInfoTxt);
TextView ownerInfoTxt = view.findViewById(R.id.ownerInfoTxt);
CreditCard cc = CreditCardController.getWithId(id);
flagInfoTxt.setText(cc.getFlag());
ownerInfoTxt.setText(cc.getOwner());
return view;
}
}
If anyone knows anything to help, please, I'm desperate.
So, I managed to go to the fragment using this:
Bundle bundle = new Bundle();
bundle.putString("id", holder.creditCardId);
InfoCreditCardFragment fragment = new InfoCreditCardFragment();
AppCompatActivity activity = (AppCompatActivity) view.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment).addToBackStack(null).commit();
And inside the fragment I just get the bundle with "getArguments().getString("id");"
I'm new to android studio . I'm trying to display items in RecycleView but it's doesn't show anything.
This is what android studio says:
E/RecyclerView: No layout manager attached; skipping layout
My Fragment :
public class Articles extends Fragment {
View root;
RecyclerView recyclerView;
NewsList[] myListData = new NewsList[] {
new NewsList("Article 1", "Some Author"),
new NewsList("Article2", "Some Author"),
new NewsList("Article3", "Some Author"),
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
return root;
}
Here is the code of my Adapter class :
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private NewsList[] listdata;
// RecyclerView recyclerView;
public NewsAdapter(NewsList[] listdata) {
this.listdata = listdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final NewsList myListData = listdata[position];
holder.textView1.setText(listdata[position].getTitle());
holder.textView2.setText(listdata[position].getAuthor());
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getTitle(),Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView1;
public TextView textView2;
public RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
}
}
Previously I had an error where the app crashed when opened the fragment but now it's skips the RecyclerView.
I will be gratefull if you help me to resolve this issue.
You need to add a layoutManager to your recycler view:
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
// Add this line
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()))
recyclerView.setAdapter(adapter);
return root;
}
It's mandatory to have a Layout manager for your Recyclerview. For example, you can use a linear one by adding this after inflating your view :
recyclerView.setLayoutManager(new LinearLayoutManager( context ));
where context is whatever your context is
This question already has an answer here:
showing RecyclerView on fragment with data from Firebase
(1 answer)
Closed 3 years ago.
I am not able to attach the Firestore Recycler Adapter in the fragment.
I googled many things but no good yet.
The fragment is one of the tabs of the bottomtablayout.
The data is retrieved from a single collection. I want represent the list of multlipe-user post in a single view.
I have tried the below code but to no good yet.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//just change the fragment_dashboard
//with the fragment you want to inflate
//like if the class is HomeFragment it should have R.layout.home_fragment
//if it is DashboardFragment it should have R.layout.fragment_dashboard
View view = inflater.inflate(R.layout.fragment_home, container, false);
final FragmentActivity c = getActivity();
LinearLayoutManager layoutManager = new LinearLayoutManager(c);
Query query = PostsRef.orderBy("timestamp", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<PostsModel> options = new FirestoreRecyclerOptions.Builder<PostsModel>()
.setQuery(query, PostsModel.class)
.build();
adapter = new PostsAdapter(options);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(c));
recyclerView.setAdapter(adapter);
mParent =view.findViewById(R.id.relative_home);
return inflater.inflate(R.layout.fragment_home, null);
}
Adapter Code
public class PostsAdapter extends FirestoreRecyclerAdapter<PostsModel, PostsAdapter.PostsHolder> {
private OnItemClickListener listener;
public PostsAdapter(#NonNull FirestoreRecyclerOptions<PostsModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull PostsHolder holder, int position, #NonNull PostsModel model) {
//retrieve the fields here
holder.textViewTitle.setText(model.getPostTitle());
holder.textViewDescription.setText(model.getPostContent());
holder.textViewPriority.setText(String.valueOf(model.getSpinnerC()));
holder.AuthorName.setText(model.getName());
holder.AuthorGender.setText(model.getGender());
}
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
parent, false);
return new PostsHolder(v);
}
public void deleteItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
class PostsHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewPriority;
TextView AuthorName;
TextView AuthorGender;
public PostsHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.quote1);
textViewDescription = itemView.findViewById(R.id.name1);
textViewPriority = itemView.findViewById(R.id.timestamp1);
}
}
}
You're inflating the other view in the onCreateView method.
return inflater.inflate(R.layout.fragment_home, null);
I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment
I'm trying to add items at runtime to ListView placed on Tab.
But when I do so, I get an error. There are 3 tabs and I'm not using ListFragments, because I have different content in all tabs
my code below
SwipeTabFragment.java
public class SwipeTabFragment extends Fragment {
private String tab;
private int color;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
tab = bundle.getString("tab");
color = bundle.getInt("color");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bookshelf, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(tab);
view.setBackgroundResource(color);
return view;
}
}
TabPagerAdapter.java
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Bundle bundle = new Bundle();
String tab = "";
int colorResId = 0;
SwipeTabFragment swipeTabFragment = new SwipeTabFragment();
switch (index) {
case 0:
tab = "BookShelf";
bundle.putString("tab",tab);
swipeTabFragment.setArguments(bundle);
return swipeTabFragment;
// break;
case 1:
tab = "Now Listening";
colorResId = R.color.color2;
BookFragment bookFragment = new BookFragment();
return bookFragment;
//break;
case 2:
tab = "Library";
colorResId = R.color.color3;
TabLibrary tabLibrary = new TabLibrary();
return tabLibrary;
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
TabLibrary.java
public class TabLibrary extends Fragment {
final String LOG_TAG = "myLogs";
ListView lvLibList;
ImageButton btAddItem;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.library, parent, false);
ListView lvLibList = (ListView)v.findViewById(R.id.lvLibList);
ImageButton btn = (ImageButton)v.findViewById(R.id.btAddItem);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText)v.findViewById(R.id.eAddItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
lvLibList.setAdapter(adapter);
return v;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
#Gurusharan S is right, the problem is about the names of your variables. To avoid confusions I'd suggest to change your code to this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.library, parent, false); //Change the name from v to view
ListView lvLibList = (ListView)view.findViewById(R.id.lvLibList);
ImageButton btn = (ImageButton)view.findViewById(R.id.btAddItem);
EditText edit = (EditText)view.findViewById(R.id.eAddItem); //Initialize the EditText outside of the click method
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
lvLibList.setAdapter(adapter);
return view;
}
And in the future I'd advice to give your variables more meaningful names, sometimes it takes you a little more coding but it is worth it.
Posting my comment as answer, as I'm pretty sure that is the issue:
The variable edit seems to be null. That is because when you call v.findViewById, you are actually calling it on the view that was clicked and NOT the fragment's view.
So you got two options:
Change the name of the fragment's view. eg. View v1 = inflater.inflate(R.layout.library, parent, false); and then change the line to v1.findViewById
Change the name of the argument of onClick. eg. public void onClick(View v1) {.
Hope that helps.