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
.
.
The result of this code