I’ve been stuck on a little bug trying to implement a custom listview in Java for an Android application.
I’m trying to list a bunch words (typically, 100 < n < 500) and highlight a subset of those rows by changing the text color. The both sets of words (global and subset) are listed in a collection (currently an ArrayList)
The problem is that some words are missing. It seems random. I think it might be more likely that the words intended for ‘highlighting’ are missing. (I.e.
I’ve tried a couple different variations of code, but here is what I’ve currently got:
public class ResultsAdapter<T> extends ArrayAdapter<String> {
private ArrayList<String> mHighlightSet;
private ArrayList<String> mGlobalSet;
private Context mContext;
public ResultsAdapter(
Context context,
int textViewResourceId,
ArrayList<String> globalSet,
ArrayList<String> highlightSet) {
super(context, textViewResourceId, globalSet);
mContext = context;
mGlobalSet = globalSet;
mHighlightSet = highlightSet;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
final String text = mGlobalSet.get(position);
TextView view = new TextView(mContext);
view.setText(text);
if(mHighlightSet.contains(text))
view.setTextColor(Color.RED);
else
view.setTextColor(Color.WHITE);
return view;
}
This custom adapter gets instantiated and assigned by the following code:
if (mSummaryList != null & mAllWords != null & foundWords != null) {
ArrayList<String> globalSet = new ArrayList<String>(mAllWords.keySet()); // mAllWords is a TreeMap
ArrayList<String> subset = hud.getFoundWords();
mResultsAdapter = new ResultsAdapter<String>(this, R.layout.simplerow, globalSet, subset);
mSummaryList.setAdapter(mResultsAdapter);
mSummaryList.setOnItemClickListener(onWordListItemClickListener);
}
It appears that there’s some disconnect between the data variables, and what shows up on the screen. I’m lost, please help.
Thanks in advance!
It might help if you test it out with a small number of words, so you can better see if you actually have a problem. I’m assuming that
mAllWordsis some sort of map. By doingmAllWords.keySet()you are getting the words in a random order. This probably makes it hard to tell if a word is actually there or not. You should try sorting the words there, or using some known ordered set so you can better tell whats going on.Also, in getView you do not want to be creating a TextView. This is really inefficient. Instead, you should get a view from the already inflated layout and update the styling. I.e, something like:
The super’s getView will already fill in the correct word. You just want to update the styling in your getView method.