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

  • SEARCH
  • Home
  • 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 6344919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:43:50+00:00 2026-05-24T20:43:50+00:00

I have a ListView that is populated with 50 items. This is the xml

  • 0

I have a ListView that is populated with 50 items.
This is the xml that I use for my ArrayAdapter.

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="@drawable/listview_background"
android:textColor="#ff000000">
</TextView>

I type

ArrayAdapter<String> list = new ArrayAdapter<String>(this, R.layout.my_simple_list); 

to create a new ArrayAdapter. Then I add a String[] items to the list. Then I call

setListAdapter(list);

To add Strings from the String[] items to list, I use a for loop to loop through the String array and add each String to list using command: list.add(items.get(i)); where i is my for loop counter.

for(int i=0;i<items.size();i++){
list.add(items.get(i));
}

This gets my ListView created and populated. I also have an onListItemClick function.

public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
        v.setBackgroundResource(R.drawable.listview_background);
    copyitems.remove(Select);
}else{
    copyitems.add(Select);
    v.setBackgroundColor(Color.GRAY);
}       
}

In the onListItemClick function copyitems is an ArrayAdapter variable that I declare as a global variable. So what this function does is store the text from the selected items of the listview so later I can copy that selected text. So if an item is selected it’s text is added to copyitems and if the same item is selected it will be removed form copyitems. Every time an item is added to copyitems the background of that item is changed to Gray and if an item is removed from copyitems the background of that item is set back to original. All this work fine. But when I have lots of items(like 50) they don’t all fit to the screen so if I want to select an item that is not on screen then I have to scroll down or up to get to the item. The wierd this is that when I scroll away from a selected item and comeback to it the Background changes to original and a different item has the Gray background. So basically if my screen fits 10 items and I select the 1st one and scroll to select others that are passed 10(for example 11). When the 1st item leaves the screen and the 11 comes in, it is already colored Gray but I didn’t selected. When I scroll back up to see the 1st item, the background may or may not be Gray. If its not Gray so other item’s Background is set to Gray like the 2nd one. Is this happening because the items are recycled when they go off the screen and because of it the positions change of the items? If it is how can I disable that so I only have items background being gray if I have selected that item. If there is a better approach to what I’m trying to do please tell me. I’m new to android.

  • 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-24T20:43:50+00:00Added an answer on May 24, 2026 at 8:43 pm

    You should write your own Adapter. I would recommend you to extend from BaseAdapter. Code would look like this:

    // These are members of your activity
    private List<String> mList = null;
    private int mNewlyAddedItem;
    
    private class Adapter extends BaseAdapter {
    
        @Override
        public int getCount() {
            return mList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }
    
        @Override
        public long getItemId(int position) {
    
            return (long)position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            if(position == mNewlyAddedItem) {
                convertView.setBackgroundColor(Color.GRAY);
            } else {
                convertView.setBackgroundResource(R.drawable.listview_background);
            }
    
            // Set text to our TextView
            TextView nameText = (TextView)convertView.findViewById(R.id.text1);
            nameText.setText(mList.get(position));
    
            return convertView;
        }
    
    }
    

    Instead of background changing inside your onListItemClick handler you should just assign mNewlyAddedItem:

    public void onListItemClick(ListView parent, View v, int position, long id) {
        String Select = this.getListAdapter().getItem(position).toString();
        if(copyitems.contains(Select)){
            copyitems.remove(Select);
            mNewlyAddedItem = -1;
        }else{
            copyitems.add(Select);
            mNewlyAddedItem = position;
        }       
    }
    mNewlyAddedItem = position;
    

    I hope I understood your task right. Anyway, you got the point: you should change adapter code to draw your views based on some state (which you can set using Activity member variables), change the state and your views will update (you can also call getListView().invalidateViews() to force redrawal of all views if it is not happening at some point).

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

Sidebar

Related Questions

I have a ListView that is populated using an XML file. However, I want
I have 3 ListView's that are populated like this (well this can be anything,
I have created a custom listview that is populated using a row.xml file. Each
I have a ListView that I am populating with items from an ObservableCollection .
A have a ListView that is rendered with multiple items. Now I want to
I have a listview that's populated by rows that get their data from a
Hopefully someone can help here. I have a ListView that is populated by a
I have a listview which is populated by a SimpleCursorAdapter that is binding data
I'm writing an Android app that is basically a ListView populated with an array
I have a ListView that is populated with rows. These rows come from an

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.