views:

67

answers:

3

Currently my ListView is filling up with the given String[] but I wanted to alternate some styles on the ListView items. Something weird is happening (I'm surely missing something obvious); The ListView Index is not fixed and the styles are not alternating as supposed.

My Code is the following:

package com.blah.blah;


import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Help extends ListActivity {
    public class CustommAdapter<E> extends ArrayAdapter<E>{


        public CustommAdapter(Context context, int textViewResourceId,
                E[] objects) {
            super(context, textViewResourceId, objects);

        }

        public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.help_item, null);
}

if(convertView != null){
    TextView tvfila = (TextView) convertView.findViewById(R.id.tvfila);         
                if(position % 2 == 0){
                convertView.setBackgroundColor(Color.LTGRAY);

                tvfila.setTextColor(Color.BLACK);

            }
                    }
            return super.getView(position, convertView, parent);

        }
        @Override
        public boolean areAllItemsEnabled() {

            return false;
        }
        @Override
        public boolean isEnabled(int position) {
            if(position % 2 == 0){
            return false;
            }else{
                return true;
            }
        }
    }

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            String[] questions = getResources().getStringArray(R.array.help_questions);
            String[] answers = getResources().getStringArray(R.array.help_answers);
            String[] DATA = new String[questions.length + answers.length];
            int dataindex = 0;
            for (int i = 0; i < questions.length; i++){
                DATA[dataindex] = questions[i];
                dataindex++;
                DATA[dataindex] = answers[i];
                dataindex++;
            }
            setListAdapter(new CustommAdapter<String>(this, R.layout.help_item, DATA));
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);
        }


}

The question is: is there a way to fix the ListView index so that I can use if(position % 2 == 0) to separate odds and evens matching Questions and Answers??

NOTES:
Both StringArrays from resources are of the same size.
The help_item.xml contains only a TextView

Images that show the problem:
The expected behavior

Expected behavior
.
.

The result of this code

alt text

A: 

Not sure if this is the problem, but don't return super.getView(position, convertView, parent);

Just return convertView.

Also you should cast convertView to whatever view you're actually expecting.

Falmarri
A: 

I would just say, create a container class, that has a string and a boolean flag, instead of evaluating the position just evaluate the isQuestion flag. And keep an array of that.

blindstuff
A: 

The problem is here:

if(position % 2 == 0){
   convertView.setBackgroundColor(Color.LTGRAY);
   tvfila.setTextColor(Color.BLACK);
}

You don't have a corresponding else statement. Thus you're not resetting the values back to what they should be for the odd numbered rows. Recall that Android ListViews reuse their views ( Android Click on listItem checks wrong checkbox ) and you might begin to see why you'd be getting strange behavior.

I82Much
This makes a lot of sense. I think this might be the answer.
blindstuff
This is exactly why I said: QUOTE: "(I'm surely missing something obvious)". Thanks I82Much, you saved me!
Lord Otori
Glad I could be of some help
I82Much