views:

39

answers:

2

Hello,

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!

A: 

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 mAllWords is some sort of map. By doing mAllWords.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:

public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);

  TextView textView = view.findById(R.id.text);  // id of the text view in R.layout.simplerow

  String text = textView.getText();

  if(mHighlightSet.contains(text))
   view.setTextColor(Color.RED);
  else
   view.setTextColor(Color.WHITE);

  return view;
 }

The super's getView will already fill in the correct word. You just want to update the styling in your getView method.

Mayra
Thanks for the quick response Mayra! mAllwords was originally a HashMap changed to a TreeMap due to it's auto-sorting properties. I didn't want to sort manually, as it seemed inefficient (though copying all the keys does essentially the same thing).Regarding the TextView-- these are actually the items *in* the ListView. A past version of code, I pulled the existing TextView object from 'super' and modified the text color:TextView view = (TextView) super.getView(position, convertView, parent);I will try using smaller sets of words. And sorting manually. Thanks again!
Jamasan
A: 

It turned out the data was different than what I was expecting / thought it was. (Mayra, you were essentially correct).

Otherwise the original code would have been functioning correctly.

In the end, the getView(...) class looks like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    TextView view = (TextView) super.getView(position, convertView, parent);

    final String text = (String) view.getText();

    if(mHighlightSet.contains(text))
        view.setTextColor(Color.RED);
    else
        view.setTextColor(Color.WHITE);

    return view;
}
Jamasan