In the data I'm getting from the server, not a single item is repeating. I debugged my code during this loop, which adds in the model class then sets in the adapter. Again, no single item is repeating.
However, in my list items are repeating after performing the click event on a row button. When I use the methods defined below the text never appears in a row but the number of empty rows is increasing.
/*When I use these 2 methods then it never displays text in the list but increases list*/
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
/*When I use these 2 methods then it never displays text in the list but increases list*/
Make sure you empty the list before adding data coming from server to your list and setting adapter to your recyclerview
Related
I have a list of elements that is editable: I can add/delete new elements to the list. Furthermore I can duplicate each Element - duplicated elements are appended to the end of the list. Each element is displayed with a corresponding EditText where users can input quantity of the given element. The Problem: After duplicating an Element E1, editing the quantity of E1 also changes quantity of E2.
Every ListItem looks like this:
TextView(ElementTitle) / EditText(ElementQuantity)
Everything works flawlessly on lists of many elements - until I use my "duplicate" function.
I assume that the problem has something to do with the Recyclerview reusing the EditTextListeners. I am assigning these in onCreateViewHolder as described in this answer: https://stackoverflow.com/a/31860393/6551120.
I tried adding notifydatasetchanged() wherever I could imagine any value. In duplicatedSelected() I tried unregistering and clearing adapter and LayoutManager and creating a new Adapter - without any result.
This is the method that duplicates my elements (In ListActivity):
private void duplicateSelected(){
List selectedItemPositions = mAdapter.getSelectedItems();
for (int i = 0; i < selectedItemPositions.size(); i++) {
int j =(int) selectedItemPositions.get(i);
modulElements.add(modulElements.get(j));
}
mAdapter.notifyDataSetChanged();
In MyAdapter:
private class ModulElementEditTextListener implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
//Other Override Methods cut out for simplicity
#Override
public void afterTextChanged(Editable editable) {
updatePosition(position);
int timesMultiplied;
if(editable.toString().equals("")){
timesMultiplied=Integer.parseInt("0");
}else{
timesMultiplied = Integer.parseInt(editable.toString());
}
modulElements.get(position)
.setMultiplier(newModulElementMultiplier());
modulElements.get(position)
.getMultiplier().setTimesMultiplied(timesMultiplied);
}
}
Expected result when entering quantity for E1: Quantity for E1 changes
Actual result when entering quantity for E1: Quantity for E1 and E2 (And E3, E4,E5... when I duplicate multiple times) changes.
If I save the list of elements to a database and reopen it I can flawlessy edit quantity of E1 and it does NOT change quantity of E2 - as I would expect it to happen in the first case.
Every hint or idea welcome, thank you so much!
You must implement the cloneable interface for your data model and modify this line
modulElements.add(modulElements.get(j).clone());
Now you have different objects in the list
I have a RecyclerView that shows different lists depending on which option I pick in my Spinner. I can add items to a list dynamically, but when I add items to the top option on the Spinner, two items appear everytime I add, when it is only supposed to add just one and when I choose a different option in the Spinner and then back to the top option, all items that are supposed to be in it are gone. However, when I repeat the same actions in another option, one item is added at a time, and they are still there if i switch to another and back, so it is only in the first option where I experience these problems.
Here are pictures that shows what happens. First picture shows the entire Spinner, second is the top option selected with items added, third is the second Spinner option selected and fourth is back to the top option and now the items are gone.
This method is used to add a Item to one of the lists. Each option in the Spinner is used to get an element in listOfLists where each element is an ArrayList<Item>(). mItemList is the list which the RecyclerView prints out.
public void addItem(){
mItemList.add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
listOfLists.get(dropdown.getSelectedItemPosition()).add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
System.out.println("ListSize: " + listOfLists.get(dropdown.getSelectedItemPosition()).size());
addAmountField.setVisibility(View.GONE);
btnAdd.setVisibility(View.GONE);
mAdapter.notifyDataSetChanged();
saveData2();
}
This is the code for switching the lists when I choose another option from the Spinner. What I do here is that I clear the mItemList and then I fill it with one of the ArrayList<Item> inside listOfLists.
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
mItemList.clear();
for (int i = 0; i < listOfLists.get(position).size(); i++) {
mItemList.add(listOfLists.get(position).get(i));
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
How can I resolve this so that the top option of the Spinner will work just like the other options where it adds one item at a time and still has the items after I switch to another option?
I managed to solve it so for anyone who faces similar problems this was the problem:
Unlike the other lists in listOfLists where I set mItemList to the chosen list like this
mItemList.clear();
for (int i = 0; i < listOfLists.get(position).size(); i++) {
mItemList.add(listOfLists.get(position).get(i));
}
mItemList is always set to the first list when I run the app like this
mItemList = listOfLists.get(dropDown.getSelectedItemPosition());
So I just changed it so it sets mItemList the same way it is done with the other lists and now it is working.
In the below code, where the item’s position and title is getting saved in list_items works fine if list_items and items are ArrayList. For ex;
ArrayList.add(ArrayList.get(position));
But I am trying to use listview:
ArrayList.add(Listview.get(position));
Below is my code
final Listadapter Adapter =new Listadapter(this,packageList1,packageManager);
items.setAdapter(Adapter);
items.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
items.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
count = count +1;
mode.setTitle(count + "items selected"); //problem resides in the below line
list_items.add(items.get(position));
}
In the above code, on selecting an item from listview I am trying to save it in an Arraylist i.e list_items which could be used in onActionItemClicked method for further action on that respective item.
Any ideas on how to solve or find a workaround to use a listview in the above situation?
I have a recyclerview whose number of elements changes all the time. Sometimes it might have 5 items, sometimes 7, sometimes 70. What is the more efficient way of changing out the entire dataset:
1) calling clear(), adding the new dataset then calling notifydatasetchanged?
2) calling clear(), then calling notifyitemrangeremoved, then adding the new dataset and calling notifyItemRangeInserted?
public void swapData(ArrayList<dataType>() newdata) {
int oldSize = recyclerlist.size();
recyclerlist.clear();
if (oldSize > 0) {
notifyItemRangeRemoved(0, oldSize);
} ...
}
The easiest way involves a few steps, but only works if your items have proper ids.
Override getItemId(int position) and return the item's id.
In your RecyclerAdapter's contructor, add this line:
setHadStableIds(true);
Just use notifyDataSetChanged() in your swapData method after you've switched the lists.
Checking on one item in custom list view and another get selected in Android. I'm using custom adapter list view with check box.
I'm using check boxes in list view.
and I'm using following code for check box count. i.e how many check box are selected.
final int listItemCount = screenList.getChildCount();
for (int i = 0; i < listItemCount; i++) {
CheckBox cbox = (CheckBox) ((View) screenList.getChildAt(i))
.findViewById(R.id.checkBox);
if (cbox.isChecked()) {
count++;
}
}
But while I'm getting count=listItemCount there are some numbers are not shown checked in the list view.
In order to access the one you expect, do something like this (assuming you are using ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// this will give you the widget that was selected.
Widget widget = widgetAdapter.getItem(position);
}
If this doesn't solve your problem, please show some code so we can take a look.
I'm having a similar problem with the list adapter. When the getview() is called for each customviewitem(has a checkbox) in the list the getview receives the position and the view object.
What i'm seeing while debugging is if the list shows 5 item at a time then say for
0th position the view obj id is 830173183032
then for the
6th position the view obj id is same ie 830173183032.
So when i check the checkbox of 0th item and then scroll down then I see that the 6th items checkbox also checked.
code was from http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/