I wanted to set the data(result)into the recycler view, but when i run, the data is not displayed on the screen
RPCRequest.OnResponseListener orderListener = new RPCRequest.OnResponseListener<ArrayList<Person>>() {
#Override
public void onSuccessResponse(RPCRequest request, ArrayList<Person> response) {
ArrayList<Person> dups=new ArrayList<>();
for(int i=0;i<response.size();i++){
if(compare(response.get(i).getDate(),Person.getInstance().getDate()))
dups.add(response.get(i));
}
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.form);
View layout=(View) dialog.findViewById(R.id.row);
layout.setVisibility(View.GONE);
Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.toolbar);
toolbar.setTitle(" Duplicates");
final RecyclerView dupOrders = (RecyclerView) dialog.findViewById(R.id.recycler_view);
dupOrders.setAdapter(adapter);
adapter.setData(dups);
dialog.show();
This is my code where i want to set the data in recycler list view. but when i debug i get the value in data. but its not displayed while executing. Can anyone please help.
You need to set a LayoutManager for the RecyclerView.
Add this line:
dupOrders.setLayoutManager(new LinearLayoutManager(context));
You should probably do this just once, so you might want to make your dupOrders variable into a field of your Activity and initialize it and set the LayoutManager in onCreate().
Related
Let me explain you what's happening. I have a fragment and in its onCreateView() I am setting the adapter to recyclerview without any item..
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
downloadsList = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
downloadsRecyclerview.setLayoutManager(linearLayoutManager);
downloadsList.clear();
downloadAdapter = new DownloadAdapter(context, downloadsList);
downloadsRecyclerview.setAdapter(downloadAdapter);
}
Now the weird part is whenever I add items to downloadsList ( which is basically an ArrayList ), then my recyclerview get updated with the content available on downloadsList. I am not even calling notifyDataSetChanged() or setting new adapter again... Any help..
If you don't want to reload entire RecyclerView using notifyDataSetChanged() then simple add notifyItemInserted()
when you add value in downloadsList then after just call
downloadAdapter.notifyItemInserted(downloadsList.size()-1)
What is the use of recyclerview.setLayoutManager() in this code?
Please elaborate it in detail.I know about recyclerview but i am confused on what is the use of setLayoutManager() here?
public class MainActivity extends AppCompatActivity {
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
private WordViewModel mWordViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
final WordListAdapter adapter = new WordListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
More than a year late but the idea behind the setLayoutManager is to set the layout of the contents, i.e. list of repeating views in the recycler view. If you scroll down to the documentation here, it will tell you that there are several strategies for lists and grids so that should give you a clue. Furthermore, it tells you that without it, RecyclerView will not function i.e. there is no out of the box default.
So if you want e.g. to set the LinearLayoutto be horizontal (by default it is vertical) then you have to specify that.
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myItems = findViewById(R.id.my_recycler_view);
myItems.setLayoutManager(layoutManager);
I have a list of data to be displayed in a recycler view and that is working fine. Now I have another dynamic list of data to be displayed inside the parent recycler-view. So I tried using another recycler-view inside the parent recycler-view, that is not working fine. It will be good if I get some idea of using recycler-view inside another one. Thanks in advance..!
I have illustrated my problem with an example:
For eg: I have a parent recyclerView with five linearLayout and I have created a child recyclerView inside the Linearlayout with visibility GONE. Now when I click the first Linearlayout I am changing the visibility of child recyclerView for the first Linearlayout to VISIBLE and attaching a separate view to it and same concept for all the other Linearlayouts. What happens is when I click first, second, third and fourth linearLayout the child recyclerView is not displaying date which I pass to it, all those first, sec, third and fourth data are accumulated and displayed in the last (i.e) inside fifth linearLayout.
Here is my parent recyclerview code:
class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyViewHolder>
{
RecyclerView insideCardRecyclerView,recyclerView;
List<String> monthsWeek = new ArrayList<>();
List<String> dealers = new ArrayList<>();
List<String> dealersList = new ArrayList<>();
List<String> date = new ArrayList<>();
HashSet<String> dealersListHash = new HashSet<>();
public CardAdapter(List<String> monthsWeek,List<String> dealers,List<String> date)
{
this.monthsWeek = monthsWeek;
this.dealers = dealers;
this.date = date;
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
ProgressBar progressBar;
RecyclerView recyclerView;
TextView period;
LinearLayout linearLayoutParent,linearLayoutCardDetails;
public MyViewHolder(View view)
{
super(view);
linearLayoutParent = (LinearLayout) view.findViewById(R.id.card_view_linear_parent_layout);
linearLayoutCardDetails = (LinearLayout) view.findViewById(R.id.linear_card_layout_details);
period = (TextView) view.findViewById(R.id.period_summary_graph_card);
insideCardRecyclerView = (RecyclerView) view.findViewById(R.id.summary_graph_card_view_recycler_view);
}
}
#Override
public CardAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(getActivity())
.inflate(R.layout.summary_card_view,parent,false);
recyclerView = (RecyclerView) parent;
return new CardAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
{
holder.period.setText(monthsWeek.get(position));
holder.linearLayoutParent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
if(searchClick)
{
for (String date1 : date)
{
if(Objects.equals(date1,monthsWeek.get(position)))
{
Log.e("Summary123 date..///", date1);
dealersList.add(dealers.get(date.indexOf(date1)));
}
}
searchClick = false;
holder.linearLayoutCardDetails.setVisibility(View.VISIBLE);
dealersListHash.addAll(dealersList);
dealersList.clear();
dealersList.addAll(dealersListHash);
//if the condition is true i am attaching another recyclerview inside this.
cardAdapterList = new CardAdapterList(dealersList);
LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,true);
mLayoutManager1.setReverseLayout(false);
insideCardRecyclerView.setLayoutManager(mLayoutManager1);
insideCardRecyclerView.setItemAnimator(new DefaultItemAnimator());
insideCardRecyclerView.setAdapter(cardAdapterList);
}
else
{
searchClick = true;
holder.linearLayoutCardDetails.setVisibility(View.GONE);
}
}
});
}
#Override
public int getItemCount() {
return monthsWeek.size();
}
}
I've been in trouble sometimes with RecyclerView when multiple lists are needed to be shown in a single page of the application. Its not a very good idea actually to have multiple lists in a single layout but however, the idea of having a ScrollView and the lists inside that ScrollView is even worse.
I had to implement a ListView inside a ScrollView once and yes it was not a very good experience. Firstly, my list was not scrolling at all. Then I had to add some code to disable the scrolling when the touch is detected inside the list. It was not a very good idea of solving the actual problem. I had another problem of having a fixed height of the ListView. In case of list items with dynamic heights, the solution failed.
Having two lists in the layout, one after one is not a good idea either. As the first list need to have a fixed height.
So, after searching for suggestions about how can I implement two lists in a single layout file, I found most of the developers suggests of having a single list with a header and footer if necessary. Later, I could manage to show two lists in a single RecyclerView using my custom Adapter. I thought I should save some of my code for future use and hence, you see this note.
You can refer this sample code.
I would like to insert a tutorial on my list of Cardview using a library. For this library I need to give a view (element pointed) but my mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView always returns null. Why does this return null? Because the recycle view is not yet created?
I also try to replace it with : mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_players);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_list_player);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mDataset = Data.bdd.get_list_players();
mAdapter = new CardsViewAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
// Tutorial
new MaterialIntroView.Builder(this)
.enableDotAnimation(true)
.enableIcon(true)
.setFocusGravity(FocusGravity.CENTER)
.setFocusType(Focus.MINIMUM)
.setDelayMillis(500)
.enableFadeAnimation(true)
.setInfoText("Hi There! Click this card and see what happens.")
.setShape(ShapeType.RECTANGLE)
.setTarget(mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)))
.show();
}
I think your CardsViewAdapter library uses the default getItemId (which returns -1). You can use RecyclerView.getChild() instead.
The easiest way for me was to modify my builder like this and place my function directly in my adapter
CardsViewAdapter(List<Player> myDataset, Activity activity) {
mDataset = myDataset;
mActivity = activity;
}
I have got CardView with Views inside a RecyclerView. I have created adapter in which assets are attached to Views and everything works. Now, I would like to change these Views from my activity. Is it any simple way to do that?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Offer>offers;
TextView timer; //timer inside CardView
private void getViewReferences() {
recyclerView = (RecyclerView)findViewById(R.id.mainRecyclerView);
timer = (TextView)findViewById(R.id.timer);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewReferences();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
timer.setText("13:16"); //NullPointerException here
}
private void initializeData(){
offers = new ArrayList<>();
offers.add(new Offer("godzina", R.drawable.zdj, 200));
offers.add(new Offer("godzina", R.drawable.zdj));
}
private void initializeAdapter() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(offers);
recyclerView.setAdapter(adapter);
}
}
So you don't want to change the Views but you want to update the text values of the views in your list.
You can't directly do that by trying to find the Views in the recyclerView and change the text. The adapter is responsible for giving values to your list. So you need to update the dataset in the adapter and call one of the notifyDataSetChanged methods on the adapter to update the recyclerView.
The big advantage of this design is that it places Views in Cache in order to reuse them multiple times without having to inflate and construct them again. This is a huge performance improvement.
You need to read the docs and some tutorials to understand the adapter design pattern better.