Android: Fast scrolling ListView weird behaviour of getPositionForSection - java

I'm trying to implement a listview that has fast scrolling with header previews. It looks like it's almost working correct, but I'm encountering some weird, bug-like behaviour. When I scroll down without using the fast scroll bar, the fast scroll bar disappears, and reappears only almost at the end. So there seems to be a gap or something like that.
My ListView's ArrayAdapter implements SectionIndexer and it's methods getSections(), getPositionForSection(int sectionIndex) and getSectionForPosition(int position). The getPositionForSection method is in my belief, the one causing trouble. When I log the value given by sectionIndex and I scroll down the list, this value exceeds the length of the actual sections (which is 20). This value comes from the SectionIndexer, not from myself. The Android refererence states:
If the section's starting position is outside of the adapter bounds,
the position must be clipped to fall within the size of the adapter.
But when I clip the value to either 0 or section_size -1 (=19), the weird behaviour keeps appearing. Below is my ListView's ArrayAdapter implementing SectionIndexer. One note: the updateSections method is called from outside the adapter when the data changes in an AsyncTask. I hope someone knows what the problem is! Thanks in advance.
public class SoortArrayAdapter extends ArrayAdapter<Soort> implements SectionIndexer {
List<Soort> data;
private HashMap<String, Integer> alphaIndexer;
private ArrayList<String> sections;
public SoortArrayAdapter(#NonNull Context context, int resource, int textViewResourceId, List<Soort> data) {
super(context, resource, textViewResourceId, data);
this.data = data;
sections = new ArrayList<>();
alphaIndexer = new HashMap<String, Integer>();
}
private void updateSections() {
alphaIndexer.clear();
sections = new ArrayList<String>();
for (int i = 0; i < data.size(); i++) {
String s = data.get(i).getNaam().substring(0, 1).toUpperCase();
if (!alphaIndexer.containsKey(s)) {
alphaIndexer.put(s, i);
sections.add(s);
}
}
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(data.get(position).getNaam());
return convertView;
}
#Override
public Object[] getSections() {
return sections.toArray(new String[0]);
}
#Override
public int getPositionForSection(int sectionIndex) {
System.out.println(sectionIndex);
if (sectionIndex >= sections.size()) {
return 0;
}
System.out.println("position for section=" + sections.get(sectionIndex));
return alphaIndexer.get(sections.get(sectionIndex));
}
#Override
public int getSectionForPosition(int position) {
String section = data.get(position).getNaam().substring(0, 1).toUpperCase();
System.out.println("section for position=" + section);
return alphaIndexer.get(section);
}
}

Looks like I misread the documentation for the above stated methods getSectionForPosition() and getPositionForSection(), especially for getSectionForPosition(). This made getPositionForSection() act strangely. I corrected my implementation, so the final implementation of the two methods is as follows:
#Override
public int getPositionForSection(int sectionIndex) {
if (sectionIndex >= sections.size()) {
return data.size() - 1;
}
if (sectionIndex < 0) {
return 0;
}
return alphaIndexer.get(sections.get(sectionIndex));
}
#Override
public int getSectionForPosition(int position) {
String section = data.get(position).getNaam().substring(0, 1).toUpperCase();
for (int i = 0; i < sections.size(); i++) {
if (section.equals(sections.get(i))) {
return i;
}
}
return 0;
}

Related

Why is the drag animation being repeated in RecyclerView?

I am using the ItemTouchHelper class to support drag and drop in my RecyclerView. While I am dragging an item around it visually updates (swaps rows) as expected. Once I drop the item, another **visual** drag occurs. For example (see diagram below) if I drag item "a" from index 0 to index 3, the correct list shows that item "b" is now at index 0. They recycler view repeats the operation and takes the new item at index 0 ("b") and drags it to index 3! This repeated drag happens no matter what index I drag from or to.
I called it a **visual** drag because the list I am submitting to my RecyclerView's ListAdapter is correctly ordered (verified by logs). And if I restart my app the list is in the correct order. Or if I call notifyDataSetChanged(), after the unwanted animation, it will order itself properly. What could be causing this second animation?
EDIT: According to the documentation, if you use equals() method in your areContentsTheSame() method (DiffUtil), "Incorrectly returning false here will result in too many animations." As far as I can tell, I am properly overriding this method in my POJO file below. I am stumped...
MainActivity.java
private void setListObserver() {
viewModel.getAllItems().observe(this, new Observer<List<ListItem>>() {
#Override
// I have verified newList has the correct order through log statements
public void onChanged(List<ListItem> newList) {
adapterMain.submitList(newList);
}
});
}
...
// This method is called when item starts dragging
public void onSelectedChanged(#Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
...
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
currentList = new ArrayList<>(adapterMain.getCurrentList()); // get current list from adapter
}
...
}
// This method is called when item is dropped
public void clearView(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder viewHolder) {
...
// I have verified that all code in this method is returning correct values through log statements.
// If I restart the app, everything is in the correct order
// this is position of the where the item was dragged to, gets its value from the onMoved method.
// it's the last "toPos" value in onMoved() after the item is dropped
int position = toPosition;
// Skip this code if item was deleted (indicated by -1). Otherwise, update the moved item
if(position != -1) {
ListItem movedItem = currentList.get(position);
// If dragged to the beginning of the list, subtract 1 from the previously lowest
// positionInList value (the item below it) and assign it the moved item. This will ensure
// that it now has the lowest positionInList value and will be ordered first.
if(position == 0) {
itemAfterPos = currentList.get(position + 1).getPositionInList();
movedItemNewPos = itemAfterPos - 1;
// If dragged to the end of list, add 1 to the positionInList value of the previously
// largest value and assign to the moved item so it will be ordered last.
} else if (position == (currentList.size() - 1)) {
itemBeforePos = currentList.get(position - 1).getPositionInList();
movedItemNewPos = itemBeforePos + 1;
// If dragged somewhere in the middle of list, get the positionInList variable value of
// the items before and after it. They are used to compute the moved item's new
// positionInList value.
} else {
itemBeforePos = currentList.get(position - 1).getPositionInList();
itemAfterPos = currentList.get(position + 1).getPositionInList();
// Calculates the moved item's positionInList variable to be half way between the
// item above it and item below it
movedItemNewPos = itemBeforePos + ((itemAfterPos - itemBeforePos) / 2.0);
}
updateItemPosInDb(movedItem, movedItemNewPos);
}
private void updateItemPosInDb(ListItem movedItem, double movedItemNewPos) {
movedItem.setPositionInList(movedItemNewPos);
viewModel.update(movedItem); // this updates the database which triggers the onChanged method
}
public void onMoved(#NonNull RecyclerView recyclerView,
#NonNull RecyclerView.ViewHolder source, int fromPos,
#NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
Collections.swap(currentList, toPos, fromPos);
toPosition = toPos; // used in clearView()
adapterMain.notifyItemMoved(fromPos, toPos);
}
}).attachToRecyclerView(recyclerMain);
RecyclerAdapterMain.java
public class RecyclerAdapterMain extends ListAdapter<ListItem, RecyclerAdapterMain.ListItemHolder> {
// Registers MainActivity as a listener to checkbox clicks. Main will update database accordingly.
private CheckBoxListener checkBoxListener;
public interface CheckBoxListener {
void onCheckBoxClicked(ListItem item); // Method implemented in MainActivity
}
public void setCheckBoxListener(CheckBoxListener checkBoxListener) {
this.checkBoxListener = checkBoxListener;
}
public RecyclerAdapterMain() {
super(DIFF_CALLBACK);
}
// Static keyword makes DIFF_CALLBACK variable available to the constructor when it is called
// DiffUtil will compare two objects to determine if updates are needed
private static final DiffUtil.ItemCallback<ListItem> DIFF_CALLBACK =
new DiffUtil.ItemCallback<ListItem>() {
#Override
public boolean areItemsTheSame(#NonNull ListItem oldItem, #NonNull ListItem newItem) {
return oldItem.getId() == newItem.getId();
}
// Documentation - NOTE: if you use equals, your object must properly override Object#equals().
// Incorrectly returning false here will result in too many animations.
// As far as I can tell I am overriding the equals() properly in my POJO below
#Override
public boolean areContentsTheSame(#NonNull ListItem oldItem, #NonNull ListItem newItem) {
return oldItem.equals(newItem);
}
};
#NonNull
#Override
public ListItemHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item_layout_main, parent, false);
return new ListItemHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ListItemHolder holder, int position) {
ListItem item = getItem(position);
Resources resources = holder.itemView.getContext().getResources();
holder.txtItemName.setText(item.getItemName());
holder.checkBox.setChecked(item.getIsChecked());
// Set the item to "greyed out" if checkbox is checked, normal color otherwise
if(item.getIsChecked()) {
holder.txtItemName.setTextColor(Color.LTGRAY);
holder.checkBox.setButtonTintList(ColorStateList
.valueOf(resources.getColor(R.color.checkedColor, null)));
} else {
holder.txtItemName.setTextColor(Color.BLACK);
holder.checkBox.setButtonTintList(ColorStateList
.valueOf(resources.getColor(R.color.uncheckedColor, null)));
}
}
public class ListItemHolder extends RecyclerView.ViewHolder {
private TextView txtItemName;
private CheckBox checkBox;
public ListItemHolder(#NonNull View itemView) {
super(itemView);
txtItemName = itemView.findViewById(R.id.txt_item_name);
// Toggle checkbox state
checkBox = itemView.findViewById(R.id.checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBoxListener.onCheckBoxClicked(getItem(getAdapterPosition()));
}
});
}
}
public ListItem getItemAt(int position) {
return getItem(position);
}
}
ListItem.java (POJO)
#Entity(tableName = "list_item_table")
public class ListItem {
#PrimaryKey(autoGenerate = true)
private long id;
private String itemName;
private boolean isChecked;
private double positionInList;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public boolean getIsChecked() {
return isChecked;
}
public void setPositionInList(double positionInList) {
this.positionInList = positionInList;
}
public double getPositionInList() {
return positionInList;
}
#Override
public boolean equals(#Nullable Object obj) {
ListItem item = new ListItem();
if(obj instanceof ListItem) {
item = (ListItem) obj;
}
return this.getItemName().equals(item.getItemName()) &&
this.getIsChecked() == item.getIsChecked() &&
this.getPositionInList() == item.getPositionInList();
}
}

Best solution for Checkbox in LinearLayout

In my LinearLayout, there's a variable number of CheckBoxes. In a question I had a month ago someone said it´s better to add checkboxes dynamicly instead of make them not visible.
Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10};
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {
cBox = new CheckBox(this);
cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
cBox.setId(count[i]);
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
antwortencode[position] += "" + buttonView.getId();
frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
} else {
String id = Integer.toString(buttonView.getId());
antwortencode[position] = antwortencode[position].replaceAll(id,"");
if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
} else {
frageBeantworten.setText("Keine Checkbox(en) gewählt");
}
}
}
});
antworten.addView(cBox);
Currently, I'm able to save a string with the checked checkboxes, if I un-check a checkbox, it deletes it's value out of the string.
If I update the activity, the string is saved, and the checkboxes get a new List from the mFrageList.get(position)getAuswahlList(); and fill a new string in the "antwortencode" List with their values.
If I go back to the last position, I have the string which was generated but the checkboxes aren't checked anymore. But they have the Strings from the old position. that means everything is saved except the state of the checkboxes. I cant set a cBox.setChecked(isChecked) or buttonView.setChecked(isChecked) or buttonView.setChecked(buttonView.isChecked()) or something which is nearly the same in syntax.
I don't know what I can do besides declaring 10 Checkboxes in a xml file to talk to them one by one and set the VISIBLE.false if the auswahlList.get(position).isEmpty().
IMPORTANT: My XML is a Scrollable Activity because the size of the content overextended the screen. Thats why i didn´t and can´t use a Listview. So i need a solution that uses a LinearLayout
The truth is, you should actually use a ListView. As long as you reuse a layout multiple times - do it.
There are 2 options:
ListView as root - add other contents of your layout as different types of view
ListView inside a scrollable layout - there are many lightweight implementations of ListView that allow it to wrap content, e.g. https://github.com/paolorotolo/ExpandableHeightListView
The other thing is how to maintain the state of Checkboxes - use model classes. It's extremely easy with a ListView as it forces you to use an Adapter which provides methods to iterate over all positions.
Example of an adapter:
public class CheckableItemAdapter extends BaseAdapter {
private List<Pair<Integer, Boolean>> items = new ArrayList<>();
public void setItems(List<Pair<Integer, Boolean>> items) {
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checkable, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Pair<Integer, Boolean> item = items.get(position);
holder.itemCheck.setChecked(item.second);
return view;
}
static class ViewHolder {
CheckBox itemCheck;
public ViewHolder(View itemView) {
itemCheck = (CheckBox) itemView.findViewById(R.id.check);
}
}
}
I´ve managed to solve my problem alone, and now i want to share it, even if it isn´t the best example of programming.
Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10}; //maximum of 10 Checkboxes
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {
cBox = new CheckBox(this);
cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
cBox.setId(count[i]);
try{ //this is where the magic happens
if(antwortencode[position] != ""){ //cause i won´t want null in my db i´ve set "" as standard string in my activity for the List<String>
String code = antwortencode[position];
char[] c = code.toCharArray();
for(int j=0;j<=c.length;j++){
int x = c[j] -'0'; // 'char 1' - 'char 0' = Integer 1 , lol
if(cBox.getId()== x){ //compare them
cBox.toggle(); //if it fits, toggle
}
}
}
} catch (Exception e){
e.printStackTrace();
} //and here it ends
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
antwortencode[position] += "" + buttonView.getId();
frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
} else {
String id = Integer.toString(buttonView.getId());
antwortencode[position] = antwortencode[position].replaceAll(id,"");
if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
} else {
frageBeantworten.setText("Keine Checkbox(en) gewählt");
}
}
}
});
antworten.addView(cBox);
Ty for the answers and for the correction of my question.
Nostramärus

java.lang.ArrayIndexOutOfBoundsException: length=27; index=-1

I'm getting this error when i'm implementing footer within recycler view.
This is how i have done it. I was using two types for showing different views in list, but something is not set well in method getItemCount() or maybe when i'm getting position of clicked item model in list.
This is what i have so far:
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_FOOTER = 1;
#Override
public int getItemCount() {
return mUsers == null ? 0 : mUsers.size() + 1;
}
#Override
public int getItemViewType(int position) {
if (isFooterPosition(position)) {
return VIEW_TYPE_FOOTER;
}
return VIEW_TYPE_ITEM;
}
private boolean isFooterPosition(int position) {
return position == mUsers.size() + 1;
}
private User getUser (int position) {
return mUsers.get(position - 1); // Here i'm getting an error mentioned in title
}
Edit:
if (holder instanceof UserHolder) {
final User user = getUser(position);
UserHolder userViewHolder = (UserHolder) holder;
userViewHolder.tvUserName.setText(user.getName());
userViewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, position);
}
});
} else if (holder instanceof FooterViewHolder) {
FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Lato-Thin.ttf");
footerViewHolder.mButton.setTypeface(typeface);
}
I have some items for normal holder view in list and one item for footer view.
mUsers.get(position - 1); will crash when the position is 0 because you're looking for the item at index -1 which is invalid.
If you're adding a footer, which will be present after all of the previous items, then do you need do the substraction?
Position 0 => User 0
Position 1 => User 1
Position N => User N
Position N + 1 => Footer
It might be better to just return mUsers.get(position).
Edit: There's another small issue:
Here's an issue:
private boolean isFooterPosition(int position) {
return position == mUsers.size() + 1;
}
mUsers.size() is 20, so users will have positions 0-19.
isFooterPosition should return true for 20 (users size + 1). However, that will return false because the footer is at position 21.
Thus, you have a spot (20) that is completely invalid.
private boolean isFooterPosition(int position) {
return position == mUsers.size();
}
I think you can modify getUser() method like following:
private boolean isValidPos(int position){
return position >= 0 && position < mUsers.size();
}
private User getUser (int position) {
if (isFooterPosition(position)) return null;
return isValidPos(position) ? mUsers.get(position) : null;
}
I can't see exactly what you want to do, but, as someone said before me, the error is in mUsers.get(position - 1);, because it's gonna search for a negative index if position == 0.
So, if you really need the subtraction, you can do like this:
private User getUser (int position) {
if(position != 0)
return mUsers.get(position - 1);
else
return mUsers.get(position);
}
But, as you can see, for position == 0, it will return the same output as position == 1.
I have found a solution and i have also extended RecyclerView with one more typeview for showing one view where there is no item:
First i have declared these variables in my adapter:
public static final int COUNT_FOOTER = 1;
public static final int COUNT_NO_ITEMS = 1;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_FOOTER = 1;
private final int VIEW_TYPE_NO_ITEM = 2;
After that i have define three view types for my list:
#Override
public int getItemViewType(int position) {
if (!mUsers.isEmpty()) {
if (position < mUsers.size()) {
return VIEW_TYPE_ITEM;
} else {
return VIEW_TYPE_FOOTER;
}
} else {
if (position == 0) {
return VIEW_TYPE_NO_ITEM;
} else {
return VIEW_TYPE_FOOTER;
}
}
}
#Override
public int getItemCount() {
if (!mUsers.isEmpty()) {
return mUsers.size() + COUNT_FOOTER;
} else {
return COUNT_NO_ITEMS + COUNT_FOOTER;
}
}
private User getUser (int position) {
return mUsers.get(position);
}

Scrollbar disappears when using SectionIndexer at specific sections on HoneyComb

I'm using adapter for ListView that implements SectionIndexer. ListView has fastScrollEnabled set to true in xml file. Everything works great on Android 2.2 and 2.3, but when I test my application on a tablet with Android 3.0, at some sections scrollbar disappears. For example when I scroll down the list, at elements beginning with letters A-B scrollbar is visible, but for letters C-H it's not, and then after H again visible.
This adapter is made for sorting content alphabetically in ListView so that fastscroll can be used.
Application is designed for API Level 8, so I couldn't use fastScrollAlwaysVisible.
Here is a code of my adapter:
public class AlphabetSimpleAdapter extends SimpleAdapter implements SectionIndexer {
private HashMap<String, Integer> charList;
private String[] alphabet;
public Typeface tfSansMedium;
public Context mContext;
public int mResource;
public int[] mTo;
public List<? extends Map<String, ?>> mData;
public String mTitleKey;
public AlphabetSimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to, String titleKey /* key sent in hashmap */) {
super(context, data, resource, from, to);
mData = data;
mTitleKey = titleKey;
mContext = context;
mResource = resource;
mTo = new int[to.length];
for ( int i = 0; i < to.length; i ++)
{
mTo[i] = to[i];
}
charList = new HashMap<String, Integer>();
int size = data.size();
tfSansMedium = Typeface.createFromAsset(context.getAssets(), "fonts/VitesseSans-Medium.otf");
for(int i = 0; i < size; i++) {
// Parsing first letter of hashmap element
String ch = data.get(i).get(titleKey).toString().substring(0, 1);
ch = ch.toUpperCase();
if(!charList.containsKey(ch)) {
charList.put(ch, i); // Using hashmap to avoid duplicates
}
}
Set<String> sectionLetters = charList.keySet(); // A set of all first letters
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); // Creating arraylist to be able to sort elements
Collections.sort(sectionList, Collator.getInstance(new Locale("pl", "PL"))); // Sorting elements
alphabet = new String[sectionList.size()];
sectionList.toArray(alphabet);
}
// Methods required by SectionIndexer
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(mResource, null);
}
for (int i = 0; i < mTo.length; i ++) {
TextView tv = (TextView)v.findViewById(mTo[i]);
if (tv != null) tv.setTypeface(tfSansMedium);
}
return super.getView(position, v, parent);
}
#Override
public int getPositionForSection(int section) {
if(!(section > alphabet.length-1)) {
return charList.get(alphabet[section]);
}
else {
return charList.get(alphabet[alphabet.length-1]);
}
}
#Override
public int getSectionForPosition(int position) {
return charList.get(mData.get(position).get(mTitleKey).toString().substring(0, 1));
}
#Override
public Object[] getSections() {
return alphabet;
}
}
charList is a HashMap where I store letters with their last appearing index so when I have 6 elements starting with letter "A", the value for key "A" is 5 and so on.
alphabet is a String array with all existing first letters.
I was facing same problem. I just gave my minimum SDK version as 8 and traget as 16 and wrote the code below. And everything worked fine.
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion <= android.os.Build.VERSION_CODES.FROYO){
// Do something for froyo and above versions
fblist.setFastScrollEnabled(true);
} else if(currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){
// do something for phones running an SDK before froyo
fblist.setFastScrollEnabled(true);
fblist.setFastScrollAlwaysVisible(true);
}

ListView items fading when scrolling in Android

I'm developing for Android and I have an app (called Forget-Me-Not) that uses a ListView as the Tasks screen (Its a ToDo list app). And I have set the items to be grayed out when they are completed.
When the list is bigger than the screen, scrolling occur as usual. But the problem is when scrolled upwards/downwards and come back again, the uncompleted items too have been grayed out. I have checked this many times and the graying-out seems to be random. The items are clickable and they are functioning as expected (i.e. long-clicking, etc). The only problem is the graying-out. (This confuses the users as this app is ToDo list managing app and graying-out is "completing" a task).
I don't know what code to post, so if anyone can tell me what code I should post or could give an answer straightaway, I would be really thankful.
PS: I've got a custom ListAdapter class. It mainly reads the items from SQLite database and sets them to the adapter. You can see the bug in my app- its in the Android Market (Forget-Me-Not).
EDIT:
Here is my custom TaskAdapter class:
public class TasksAdapter extends BaseAdapter {
private String[] items;
private int[] priorities;
private Vector<String> completed;
private Context context;
public TasksAdapter(TasksActivity context, int textViewResourceId, String[] items) {
if(items != null)
this.items = items;
else
this.items = new String[0];
this.priorities = new int[this.items.length];
for(int i = 0; i < this.priorities.length; i++) {
this.priorities[i] = FMN.PRIORITY_LOW;
}
this.completed = new Vector<String>(0);
this.context = context;
}
public TasksAdapter(TasksActivity context, int textViewResourceId, String[] items, int[] priorities, Vector<String> completed) {
if(items != null)
this.items = items;
else
this.items = new String[0];
if(priorities != null)
this.priorities = priorities;
else {
this.priorities = new int[this.items.length];
for(int i = 0; i < this.priorities.length; i++) {
this.priorities[i] = FMN.PRIORITY_LOW;
}
}
if(completed != null)
this.completed = completed;
else
this.completed = new Vector<String>(0);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.task_item, null);
}
TextView task = (TextView) v.findViewById(R.id.tv_task);
task.setText(items[position]);
ImageView prio = (ImageView) v.findViewById(R.id.im_task_priority);
switch(priorities[position]) {
case FMN.PRIORITY_HIGH:
prio.setImageResource(R.drawable.fmn_priority_high);
break;
case FMN.PRIORITY_MEDIUM:
prio.setImageResource(R.drawable.fmn_priority_low);
break;
case FMN.PRIORITY_LOW:
prio.setImageResource(R.drawable.fmn_priority_medium);
break;
default:
prio.setImageResource(R.drawable.fmn_priority_medium);
break;
}
if((completed.size() != 0) && (completed.contains(task.getText()))) {
task.setTextColor(Color.rgb(155, 175, 155));
}
return v;
}
public int getCount() {
return items.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}
And a screenshot BEFORE scrolling:
And AFTER scrolling:
It is probably a bug in your ListAdapter getView. Do you recycle your list objects/items? The properties for colors and so on will remain on the object when recycled. Make sure you set the properties correct depending on in which state the task is in.
In the getView function you probably check if the view-item allready is set and then you just change the text for the task on the item (recycling)? At the same place in the code change the color settings for background to the correct task state.
EDIT
Now that I see your code it looks like you have got the priorities mixed up, default and priority low set the background to prio.setImageResource(R.drawable.fmn_priority_medium);
and medium sets the background to prio.setImageResource(R.drawable.fmn_priority_low);
Ok I found the problem:
if((completed.size() != 0) && (completed.contains(task.getText()))) {
task.setTextColor(Color.rgb(155, 175, 155));
}
You need an else statement here to set the text color to the original color if it is not completed. Like:
if((completed.size() != 0) && (completed.contains(task.getText()))) {
task.setTextColor(Color.rgb(155, 175, 155));
} else {
task.setTextColor(Color.rgb(0, 0, 0));
}

Categories

Resources