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 8423083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:38:08+00:00 2026-06-10T03:38:08+00:00

In onListItemClick I want to change clicked item background, this is my code: public

  • 0

In onListItemClick I want to change clicked item background, this is my code:

public void onListItemClick(ListView parent, View v, int position, long id){

    if (position != previous_position){
        v.setBackgroundResource(R.drawable.category_clicked_item_bg);
        if (previous_category != null)
            previous_category.setBackgroundResource(R.drawable.category_item_bg);
        previous_category = v;
        previous_position = position;
    }
}

previous_category and previous_position are protected variables.

The problem is, that on item click multiple items in list are highlighted (more specifically 1 or 2). In list there are 20 rows, if I click one in middle, only one gets highlighted, if I click any row in beginning or end, other row in opposite side gets highlighted with interval 13 rows (that’s probably the reason why pressing middle row don’t highlight second).

I need list to highlight only one row the clicked one, what is the problem?

ADDITIONAL CODE

If that can help, here is also ListView adapter class:

package domehotel.guestbook.page.category;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import domehotel.guestbook.R;

public class CategoryAdapter extends ArrayAdapter<CategoryItem>{

Context context; 
int layoutResourceId;    
CategoryItem data[] = null;

public CategoryAdapter(Context context, int layoutResourceId, CategoryItem[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    CategoryHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new CategoryHolder();
        holder.category_name = (TextView)row.findViewById(R.id.category_name);

        row.setTag(holder);
    }
    else
    {
        holder = (CategoryHolder)row.getTag();
    }

    CategoryItem category = data[position];
    holder.category_name.setText(category.category_name);

    return row;
}

static class CategoryHolder
{
    TextView category_name;
}
}
  • 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-06-10T03:38:09+00:00Added an answer on June 10, 2026 at 3:38 am

    In fact you are reusing views. getView takes as 2nd parameter a View named convertView. The ListView calls this method with an old View item (if there is one), which is instantiated (inflated) but not displayed at the moment. Your meth. onListItemClick now sets the backround of one of these Views, but this View will be reused later for another row, without resetting it’s backround.

    A possible soloution:

    1. The click listener should only store the background color, or a clicked flag, for a clicked position somewhere and then ask the View (row) in question to rerender (it could also save state and then set the background directly).

    2. The getView(..) method should always set the background color of a View according to the saved color/state for its position. Set a normal background for positions not marked as clicked or set another background for positions marked as clicked by the click listener.

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

Sidebar

Related Questions

Activity that is sending the putExtra() @Override protected void onListItemClick(ListView l, View v, int
I want all the views of Row in OnListItemClick() Actually I want to change
My listview row appears with one word name. I want to change into two
I want to pass the integer (id) from listview (when it is clicked) to
I have a ListView, and I want that when I press an item, go
i have a button in listview .now i want to use button.setonclicklistener and onListItemClick
I want to open a Custom Dialog() when my Custom list view item is
I have done Muticolum listview onListItemClick .Its working fine.Problem is This line String selection
Due to this feature in ListViews, items cannot gain focus if you want ListView
I know onListItemClick method to handle click listener as below. protected void onListItemClick(ListView l,

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.