Scrollbar disappears when using SectionIndexer at specific sections on HoneyComb - java

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);
}

Related

Android: Fast scrolling ListView weird behaviour of getPositionForSection

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;
}

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

How to add an existing image?

I'm editing an open source app: A simple coloring page app for kids. I need to be able to make the user import his own images to be colored,a code for a button in my menu that when clicked by the user it opens his gallery(on SD) to choose an image from and then import this image to the view where he could color it . Here is the full source code.
And here is the code for loading images from R.drawable:
public class StartNewActivity extends NoTitleActivity implements View.OnClickListener
{
// This is an expensive operation.
public static int randomOutlineId()
{
return new ResourceLoader().randomOutlineId();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Apparently this cannot be set from the style.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
setContentView(R.layout.start_new);
GridView gridview = (GridView) findViewById(R.id.start_new_grid);
gridview.setAdapter(new ImageAdapter(this));
}
public void onClick(View view)
{
setResult(view.getId());
finish();
}
private static class ResourceLoader
{
ResourceLoader()
{
// Use reflection to list resource ids of thumbnails and outline
// images.First, we list all the drawables starting with the proper
// prefixes into 2 maps.
Map<String, Integer> outlineMap = new TreeMap<String, Integer>();
Map<String, Integer> thumbMap = new TreeMap<String, Integer>();
Field[] drawables = R.drawable.class.getDeclaredFields();
for (int i = 0; i < drawables.length; i++)
{
String name = drawables[i].getName();
try
{
if (name.startsWith(PREFIX_OUTLINE))
{
outlineMap.put(name.substring(PREFIX_OUTLINE.length()),
drawables[i].getInt(null));
}
if (name.startsWith(PREFIX_THUMB))
{
thumbMap.put(name.substring(PREFIX_THUMB.length()),
drawables[i].getInt(null));
}
}
catch (IllegalAccessException e)
{
}
}
Set<String> keys = outlineMap.keySet();
keys.retainAll(thumbMap.keySet());
_outlineIds = new Integer[keys.size()];
_thumbIds = new Integer[keys.size()];
int j = 0;
Iterator<String> i = keys.iterator();
while (i.hasNext())
{
String key = i.next();
_outlineIds[j] = outlineMap.get(key);
_thumbIds[j] = thumbMap.get(key);
j++;
}
}
public Integer[] getThumbIds()
{
return _thumbIds;
}
public Integer[] getOutlineIds()
{
return _outlineIds;
}
public int randomOutlineId()
{
return _outlineIds[new Random().nextInt(_outlineIds.length)];
}
private static final String PREFIX_OUTLINE = "outline";
private static final String PREFIX_THUMB = "thumb";
private Integer[] _thumbIds;
private Integer[] _outlineIds;
}
private class ImageAdapter extends BaseAdapter
{
ImageAdapter(Context c)
{
_context = c;
_resourceLoader = new ResourceLoader();
}
public int getCount()
{
return _resourceLoader.getThumbIds().length;
}
public Object getItem(int i)
{
return null;
}
public long getItemId(int i)
{
return 0;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
// If it's not recycled, initialize some attributes
imageView = new ImageView(_context);
imageView.setLayoutParams(new GridView.LayoutParams(145, 145));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(StartNewActivity.this);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(_resourceLoader.getThumbIds()[position]);
imageView.setId(_resourceLoader.getOutlineIds()[position]);
return imageView;
}
private Context _context;
private ResourceLoader _resourceLoader;
}
}
I think you need to use pictures from sd card in your application .
You can do so using the following links
Load images from sd card folder
loading images from SD card directory in GridView
How to Dynamically show images from a folder in sdcard
gridview and setImageBitmap when load image from SDcard
Hope it helps

Converting custom ArrayList to LinkedHashSet

I have a custom ArrayAdapter<Summary> which holds a list of events. There are duplicate values in the List<Summary>, so I'm trying to put the values of List<Summary> to LinkedHashSet<Summary> but this displays a blank page.
How do I convert custom ArrayList to LinkedHashSet to get unique data?
Main.java:
LinkedHashSet<Summary> listToSet = new LinkedHashSet<Summary>();
final List<Summary> summaries = new ArrayList<Summary>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.events_summary, container, false);
.......
setListView(month, year, date_value);
summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, listToSet);
calendarSummary = (ListView) v.findViewById(R.id.calendarSummary);
calendarSummary.setAdapter(summaryAdapter);
return v;
}
public void setListView(int month, int year, int dv) {
events = new HolidayEvents();
_calendar = Calendar.getInstance(Locale.getDefault());
int totalDays = _calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i = 1; i <= totalDays; i++){
if(isHoliday(i, month, year, dv))
{
date = i + " " + getMonthForInt(month-1) + " " + year;
for (Event event : events.eventDetails(this, month, i))
{
summaries.add(new Summary(date, event.eventdetails));
listToSet.addAll(summaries);
}
}
}
}
ArrayAdapter.java:
public class SummaryAdapter extends ArrayAdapter<Summary>{
Context context;
int layoutResourceId;
LayoutInflater mInflater;
LinkedHashSet<Summary> list = null;
List<Summary> data = null;
public SummaryAdapter(Context context, int layoutResourceId, LinkedHashSet<Summary> summaries) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.list = summaries;
data = new ArrayList<Summary>(list); //converting LinkedHashSet to List
mInflater = LayoutInflater.from(context);
}
....rest of the code retrieving data by using data.get(position) ...
You need to ensure that the class you are putting into Set properly overrides Equals and hashCode functions.
Lets have a look at case where hashCode is not overriden:
import java.util.*;
public class Example {
public static class Abc {
protected String s;
public Abc(String s) {
this.s = s;
}
#Override
public boolean equals(Object other) {
if (other instanceof Abc) {
return ((Abc) other).s.equals(this.s);
}
return false;
}
#Override
public int hashCode() {
return (int) (Math.random() * Integer.MAX_VALUE);
}
public String toString() {
return "Abc: " + this.s;
}
}
public static void main(String[] args) {
ArrayList<Abc> ar = new ArrayList<>();
ar.add(new Abc("a"));
ar.add(new Abc("a"));
ar.add(new Abc("a"));
LinkedHashSet<Abc> lhs = new LinkedHashSet<>(ar);
System.out.println("ar: " + ar);
System.out.println("LinkedHashSet: " + lhs);
}
}
This will produce:
ar: [Abc: a, Abc: a, Abc: a]
LinkedHashSet: [Abc: a, Abc: a, Abc: a]
even though equals are properly implemented.
I believe you may want to double-check proper implementation of both HashCodes and Equals.

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