Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3309762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:40:47+00:00 2026-05-17T21:40:47+00:00

Currently my ListView is filling up with the given String[] but I wanted to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:40:47+00:00Added an answer on May 17, 2026 at 9:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a ListView to display items. Currently I am passing a String
Im currently doing some android developer that lists items in a ListView , we
I currently have a tabview with a listview inside. I only have three items
I'm currently having a problem in displaying some stuff in ListView . I have
I currently have a listview showing a couple of string arrays. I was wondering
Currently i am having listview with multiple items (First time, i fetch data with
I have a listView, it has about 100 items. Currently I have this code:
I'm currently using a ListView to display a sort of items. I've implemented an
I am currently working on a ListView that contains around 80 to 100 items
I've tried to filter my ListView from my EditText box, but currently, it's not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.