RecyclerView not showing all item - android

here is my recycler view adapter snippet.
#Override
protected void
onBindViewHolder(#NonNull ParentStatsViewHolder holder,
int position, #NonNull ChildModal model) {
/*TODO: CURRENTLY SHOW SINGLE ITEM, FIX TO SHOW ALL*/
mapFromFirebase = model.getAppsList();
List<String> keyset = new ArrayList<>(mapFromFirebase.keySet());
List<Long> values = new ArrayList<>(mapFromFirebase.values());
Log.d("ParentStatsAdapter", keyset.toString());
String pkgnamE = keyset.get(position);
holder.pkgName.setText(pkgnamE);
holder.usageTime.setText(DateUtils.formatElapsedTime(values.get(position) / 1000));
Log.d("Positionofsdsd", String.valueOf(position));
}
As you see, when i log "ParentStatsAdapter", it return a whole keyset, which more than 1. The keyset return [Telegram, Blocked Numbers Storage, Package installer, Messenger,].
But when i log "Positionofsdsd", it only return [Telegram]. I already set layout_height="wrap content" for my itemview.

How did you override "getItemCount()"?
I will assume that this function at you is not redefined. Add a function that returns the size of your array.
#Override
public int getItemCount() {
return mapFromFirebase.size();
}

Related

Show only certain items in recycleview according to condition

I have a recycleview showing a list of audio files fetched from my audios.json file hosted on my server. i have a model class with a getter method getLanguage() to see the audio language. I would like to show only audio files of users preference in recycle view. Say for example, if user wants only english and russian i would like to show only list of russian and english. How can we achieve this? Right now the entire list is displayed.
public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.HomeDataHolder> {
int currentPlayingPosition = -1;
Context context;
ItemClickListener itemClickListener;
List<Output> wikiList;
public AudioAdapter(List<Output> wikiList, Context context) {
this.wikiList = wikiList;
this.context = context;
}
#NonNull
#Override
public HomeDataHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.audio_row_layout,viewGroup,false);
HomeDataHolder mh = new HomeDataHolder(view);
return mh;
}
#Override
public void onBindViewHolder(#NonNull final HomeDataHolder homeDataHolder, int i) {
String desc = wikiList.get(i).getLanguage() + " • " + wikiList.get(i).getType();
homeDataHolder.tvTitle.setText(wikiList.get(i).getTitle());
homeDataHolder.tvotherinfo.setText(desc);
homeDataHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (itemClickListener != null)
itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
}
});
homeDataHolder.rippleLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (itemClickListener != null)
itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
}
});
}
#Override
public int getItemCount() {
return wikiList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public void setClickListener(ItemClickListener itemClickListener) { // Method for setting clicklistner interface
this.itemClickListener = itemClickListener;
}
public class HomeDataHolder extends RecyclerView.ViewHolder {
TextView tvTitle,tvotherinfo;
MaterialRippleLayout rippleLayout;
public HomeDataHolder(View v) {
super(v);
this.tvTitle = v.findViewById(R.id.title);
this.tvotherinfo = v.findViewById(R.id.audioDesc);
this.rippleLayout = v.findViewById(R.id.ripple);
}
}
}
The general idea for this should be:
you have one list with all items
you have filter rules selected by the user
You filter items from number 1, to see which ones match the constraints and store this in another list.
Then the recycler view only shows the items of the list from number 3.
This means that recycler view's getItemCount would return the size of the filtered list, not the whole list.
Instead of passing the wikiList as it is, filter it then send it:
Lets say that you filled up the wikiList, before passing it to the adapter, filter it like this:
In the activity that you initialize the adapter in:
public class YourActivity extends ............{
........
........
//your filled list
private List<Output> wikiList;
//filtered list
private List<Output> filteredList= new ArrayList<Output>();
//filters
private List<String> filters = new ArrayList<String>();
//lets say the user chooses the languages "english" and "russian" after a button click or anything (you can add as many as you want)
filters.add("english");
filters.add("russian");
//now filter the original list
for(int i = 0 ; i<wikiList.size() ; i++){
Output item = wikiList.get(i);
if(filters.contains(item.getLanguage())){
filteredList.add(item);
}
}
//now create your adapter and pass the filteredList instead of the wikiList
AudioAdapter adapter = new AudioAdapter(filteredList , this);
//set the adapter to your recyclerview........
......
.....
......
}
I use above "english" and "russian" for language. I don't know how they are set in your response, maybe you use "en" for "english" so be careful.

android Recyclerview removing item does not update adapter's get item count

I have a RecyclerView with swiping feature to reveal a delete and edit button.
I added: adapter.notifyItemRemoved(position) and this:
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
when the revealed delete button is clicked, the animation for removing the item works and
The item is deleted from my database
BUT then the deleted item re-appears in my recyclerview. When I change activity and go back, to the activity with the recyclerview, the list that I should be seeing is good.
If I remove the "notifyItemRangeChanged" code, the list updates with the last item repeated.
I think it is my Adapter's getItemCount not properly updating. so what I tried differently was to call my method that generates the list in the first place. This did the trick BUT my remove item animation is gone now because I guess it just skips to re-generate the list....
Any ideas?
Thank you in advanced for your feedback!
****************** UPDATE - ADDING ADAPTER CLASS CODE ****************
public class RVCategoryAdapter extends RecyclerView.Adapter {
Context context;
List categoryItemList;
public RVCategoryAdapter(Context context, List<CategoryItem> categoryItemList) {
this.context = context;
this.categoryItemList = categoryItemList;
}
#NonNull
#Override
public CategoryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.category_item_layout, parent, false);
return new CategoryViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull CategoryViewHolder holder, final int position) {
final int categoryID;
final String categoryTitle;
Glide.with(context).load(categoryItemList.get(position).getImage()).into(holder.ivCategoryIcon);
holder.txtCatID.setText(""+categoryItemList.get(position).getCategoryID());
holder.txtCategoryTitle.setText(categoryItemList.get(position).getTitle());
holder.txtCategoryDesc.setText(categoryItemList.get(position).getDescription());
categoryID = Integer.parseInt(holder.txtCatID.getText().toString());
categoryTitle = holder.txtCategoryTitle.getText().toString();
holder.cardViewItemLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, NotesListActivity.class);
intent.putExtra("CategoryID", categoryID);
intent.putExtra("CategoryTitle", categoryTitle);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return categoryItemList.size();
}
}
In your swipe delete button click listener remove your item from the list, too.
I would suggest you to add delete function in your adapter. Then in that method delete your item from list and call notifyItemRemoved.
public void delete(int position){
categoryItemList.remove(position);
notifyItemRemoved(position);
}

Multiply recycler view and adapters

Actually I am going to ask more than one question here. Don't ban me, please, just read a full story. Let's begin. So I need to create an activity or fragment (it doesn't matter) with to parts (views) inside (top and bottom). Inside the bottom part dynamically loads buttons (sometimes 2, sometimes 30), there is a click listener on them. When a user clicks on a button, the button appears on the top part (view) and disappears on the bottom view. The buttons on the top view also have click listener and if a user clicks on a button it appears on the bottom view and disappears on a top. So this is a task. I thought how to implement it. The simplest solution that I created is: two views are recycler views with two adapters. Mm, probably it is not the best solution, I am pretty sure of it. I could implement two adapters, but I can't implement the click listener for my second adapter. It doesn't work!? I don't like this way for two reasons: 1. both adapters are the same; 2. I can't use click adapter for second adapter. Below you can find my code.
My adapter - standard adapter:
public class KeyboardAdapter extends RecyclerView.Adapter<KeyboardAdapter.KeyboardAdapterViewHolder> {
private List<String> values;
/*
* An on-click handler that we've defined to make it easy for an Activity to interface with
* our RecyclerView
*/
private final KeyboardAdapterOnClickHandler mClickHandler;
/**
* The interface that receives onClick messages.
*/
public interface KeyboardAdapterOnClickHandler {
void onClick(int position, String nameClicked);
}
/**
* Creates a SourceAdapter.
*
* #param clickHandler The on-click handler for this adapter. This single handler is called
* when an item is clicked.
*/
public KeyboardAdapter(List<String> myDataset, KeyboardAdapterOnClickHandler clickHandler) {
values = myDataset;
mClickHandler = clickHandler;
}
/**
* Cache of the children views for a forecast list item.
*/
public class KeyboardAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
private Button btnValue;
private String mName;
public View layout;
private int parentId;
private KeyboardAdapterViewHolder(View view) {
super(view);
//layout = view;
btnValue = view.findViewById(R.id.btn);
//parentId = ((View) btnValue.getParent()).getId();
// Call setOnClickListener on the view passed into the constructor (use 'this' as the OnClickListener)
view.setOnClickListener(this);
}
public void setData(String name) {
mName = name;
btnValue.setText(mName);
}
#Override
public void onClick(View view) {
int adapterPosition = getAdapterPosition();
mClickHandler.onClick(adapterPosition, mName);
}
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
#NonNull
public KeyboardAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new KeyboardAdapterViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull KeyboardAdapterViewHolder viewHolder, final int position) {
viewHolder.setData(values.get(position));
}
#Override
public int getItemCount() {
return values.size();
}
#Override
public int getItemViewType(int position) {
return 0;
}
public void remove(int position) {
values.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, values.size());
}
}
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
String s = "test it";
mAdapter = new KeyboardAdapter(virtualKeyboardInit(s), MainActivity.this);
recyclerView1.setAdapter(mAdapter);
// empty list just to init rv
answerList = new ArrayList<>();
mAdapter1 = new KeyboardAdapter1(answerList, MainActivity.this); // doesn't work, error message "KeyboardAdapter1.KeyboardAdapterOnClickHandler cannot be applied to MainActivity"
recyclerView2.setAdapter(mAdapter1);
}
private List<String> virtualKeyboardInit(String s) {
boolean checkBool = true;
// convert string to array and then to list
String [] strArray = s.split("(?!^)");
stringList = new ArrayList<>(Arrays.asList(strArray));
// shuffle letters in the list
long seed = System.nanoTime();
Collections.shuffle(stringList, new Random(seed));
// API 24
// /String[] strArray = Stream.of(cArray).toArray(String[]::new);
return stringList;
}
#Override
public void onClick(int position, String nameClicked) {
mAdapter.remove(position);
}
These are just a fragment of code. So, what can I do in this case? Thank you for attention and help.

FirebaseRecyclerAdapter add a hasChild() method?

Here's my database:
Database:{
X:{
JK-KDUKSIDKSIIJDSL1:{
text:"hello",
name:"Donald J. Drunk"
}
JK-KDadDFDDIJDSL1:{
name:"Killery Hlinton"
}
}
}
And I want to filter my FirebaseRecyclerAdapter such that only add to my RecyclerView data that contains the key text. How is this possible? How can I add it? Current code:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
}
You could try overriding the onBindViewHolder method in your FirebaseRecyclerAdapter. Something like this:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void onBindViewHolder(CampaignHolder viewHolder, int position) {
Campaign model = getItem(position);
if(model.getText() != null){
populateViewHolder(viewHolder, model, position);
}
}
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
};
With this approach you would still pull all data at the query location but entries without a text attribute just wouldn't be displayed. If that is unsatisfactory and you want to minimize the amount of data you download per query then the only other option I see is closer to what is mentioned in the comments above - meaning you would need to create a separate location that only stores those entries which have a text value.

Can AutoCompleteTextView return an ID?

AutoCompleteTextView lets users choose a string from a list of valid values. Like, I imagine, every developer who wants to use this yoke, I am much more interested in the id of the user's selection than its string label. Is there any way to retrieve the id property of a chosen object, or its index in the source array?
The following C# code let's users pick from a list of SomeObject. I'm working in Xamarin, but don't let this put you off. Fix my problem in java and I'll happily make it work in C#
public class AutoCompleteField : PhysicalField
{
protected AutoCompleteTextView actv;
public AutoCompleteField(IList<SomeObject> choices, LogicalField logical, string id)
: base(logical, id)
{
_choices = choices;
}
protected ArrayAdapter<SomeObject> _adapter;
public override void addToView(LayoutInflater inflater)
{
var ctx = App_NotMobility.CurrentActivity;
actv = new AutoCompleteTextView(ctx);
actv.Id = _form.generateId();
// test choices
var _choices = new List<SomeObject>();
_choices.Add(new SomeObject(234, "Oranges"));
_choices.Add(new SomeObject(456, "Apples"));
_choices.Add(new SomeObject(789, "Bananas"));
_adapter = new ArrayAdapter<SomeObject>(ctx, Android.Resource.Layout.SimpleDropDownItem1Line, _choices);
actv.Adapter = _adapter;
actv.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e)
{
// HOW DO I ACCESS THE ID OR THE INDEX OF USER'S SELECTION ?????????????????
};
_form.AddView(actv);
}
}
public class SomeObject
{
public int Id { get; set; }
public string Label { get; set; }
public SomeObject(int id, string label)
{
Id = id;
Label = label;
}
public override string ToString()
{
return Label;
}
}
Once you have initialized the adapter and overdid the item click, all you need to do is get the particular object from your adapter at that particular position of item which you clicked.
In java it would be somewhat similar to,
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeObject someObject = (SomeObject) adapter.getItem(position);
int id = someObject.getId();
}
Thats all you would need. I am not sure about your code in xamarin, how you would get the position because i don't see any method where the position is input variable, may be you could add the tag to your view and can get the tag on your click method.
var position = ((View)sender).Tag;
But i would recommend , if you can create a class extend the base adapter, that way you will the method GetView to override and can easily do what you are looking for. You constructor could be like this for start,
List<SomeObjects> items;
Activity context;
public CustomAdapter(Activity context, List<SomeObjects> items)
: base()
{
this.context = context;
this.items = items;
}
Ankush's answer worked. I'm posting the C# code here because there are some subtleties with casting and generics...
public class myActv : AutoCompleteTextView, AdapterView.IOnItemClickListener
{
PhysicalField _physical;
public myActv(Activity ctx, PhysicalField physical) : base(ctx)
{
OnItemClickListener = this;
_physical = physical;
}
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
// This is the punchline...
SomeObject whatIwant = ((ArrayAdapter<SomeObject>)this.Adapter).GetItem(position);
}
}
Here searchText is an Autocompletetextview..
searchText.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
int position = position; //Adapter selection position
}
});

Categories

Resources