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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:53:42+00:00 2026-06-05T13:53:42+00:00

I have a list view with multi columns… Want to change the background color

  • 0

I have a list view with multi columns… Want to change the background color of a row item that is selected or clicked. Have referred to various articles on stackoverflow but they dont seem to help…What i ve done in OnItemClick is .setBackgroundColor. Cannot use the selector as would want to remember the color change in the sense the row that I have changed color for, when the application relaunches should remember the change…I put the flag value in database and can set the color in getView but how to do it for first time? Here is the code…..

public class AdminMultiColumn extends Activity
{
    private ArrayList<HashMap<String,String>> list;
    ArrayList<String> al;
     AlertDialog.Builder alertDialogBuilder;

    public void onCreate(Bundle savedInstanceState)  
    {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.admin_checkoutlist);  

      lview = (ListView)findViewById(R.id.admin_list);  
      populateList();  
      Admin_checkoutlist adapter = new Admin_checkoutlist(this,list);  
      lview.setAdapter(adapter);  
    }  


    private void populateList() {  

        list = new ArrayList<HashMap<String,String>>();  

        al = new ArrayList<String>();

        testday = c.get(Calendar.DAY_OF_MONTH);
        testmonth = c.get(Calendar.MONTH)+1;
        testyear = c.get(Calendar.YEAR);
        dateget = testday+"-"+testmonth+"-"+testyear;

        mdb.open();
        va.open();
        Cursor c1 = va.getNameTimeinTimeout(dateget);
        DatabaseUtils.dumpCursor(c1);
        if(c1.moveToFirst())
        {
            do{ 
                idfromdb = c1.getString(0);
                al.add(idfromdb);
                namefromdb = c1.getString(1);
                al.add(namefromdb);
                timefromdb = c1.getString(2);   
                al.add(timefromdb);
                timeoutfromdb = c1.getString(3);
                al.add(timeoutfromdb);

              }while(c1.moveToNext());
        }
        c1.close();
        va.close();
        mdb.close();
        Log.d("pavan","data retrived "+namefromdb+" "+timeoutfromdb+" "+timefromdb+" "+idfromdb);

        for(int i = 0;i<al.size();i=i+4)
        {
            temp   = new HashMap<String,String>();  
            temp.put(FIRST_COLUMN, al.get(i));  
            temp.put(SECOND_COLUMN, al.get(i+1));
            temp.put(THIRD_COLUMN, al.get(i+2));
            temp.put(FOURTH_COLUMN, al.get(i+3));

            Log.d("pavan","test here for admin");
            list.add(temp); 
        }   
    }
    //adapter class goes here////////////
    public class Admin_checkoutlist extends BaseAdapter implements OnItemClickListener   
    {

        public ArrayList<HashMap<String,String>> list;  
        Activity activity;   

        public Admin_checkoutlist(Activity activity, ArrayList<HashMap<String,String>> list) 
        {  
            super();  
            this.activity = activity;  
            this.list = list;  
        }  

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size(); 
        }

        @Override
        public Object getItem(int pos) {
            // TODO Auto-generated method stub
            return list.get(pos);
        }

        @Override
        public long getItemId(int pos) {
            // TODO Auto-generated method stub
            return 0;
        }
        public class ViewHolder 
        {  
            TextView txtFirst;  
            TextView txtSecond;  
            TextView txtThird;
            TextView txtFOURTH;
        }  

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {   
            LayoutInflater inflater = activity.getLayoutInflater();  

            if (convertView == null)  
            {  
                convertView = inflater.inflate(R.layout.temp, null);  
                holder = new ViewHolder();

                holder.txtFirst = (TextView)convertView.findViewById(R.id.uid); 
                holder.txtSecond = (TextView)convertView.findViewById(R.id.nametext); 
                holder.txtThird = (TextView)convertView.findViewById(R.id.checkintext);
                holder.txtFOURTH = (TextView)convertView.findViewById(R.id.checkouttext);
                convertView.setTag(holder); 
                lview.setOnItemClickListener(this);     
            }  
            else  
            {  
                holder = (ViewHolder) convertView.getTag();  
            }             
            HashMap<String, String> map = list.get(position);  
            holder.txtFirst.setText(map.get(FIRST_COLUMN));  
            holder.txtSecond.setText(map.get(SECOND_COLUMN));  
            holder.txtThird.setText(map.get(THIRD_COLUMN));
            holder.txtFOURTH.setText(map.get(FOURTH_COLUMN));
            return convertView;  
        }//getview ends here.

        @Override
        public void onItemClick(final AdapterView<?> parent, final View view, int pos,
                long id) {
            // TODO Auto-generated method stub
            Object  listItem = parent.getFirstVisiblePosition()+pos+1;
            //lview.getChildAt(pos).setBackgroundColor(R.color.blue);

            view.setBackgroundColor(pos);
            idtosend = listItem.toString();

            Log.d("pavan","id is"+idtosend);

            /////alert displaing block starts here////////////////////////////////
                     alertDialogBuilder = new AlertDialog.Builder(AdminMultiColumn.this);            
                        alertDialogBuilder.setTitle("Alert");    
                        alertDialogBuilder
                            .setMessage("Check out now")
                            .setCancelable(false)
                            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity
                                    Toast.makeText(getApplicationContext(), "Checkout successfull", Toast.LENGTH_LONG).show();

                                    testhour = c.get(Calendar.HOUR_OF_DAY);
                                    testmin = c.get(Calendar.MINUTE);
                                    testsec = c.get(Calendar.SECOND);
                                    timings = testhour+":"+testmin+":"+testsec;

                                        mdb.open();
                                        va.open();
                                        va.upDate(idtosend,timings);
                                        Toast.makeText(getApplicationContext(), "update the checkout options "+timings, Toast.LENGTH_SHORT).show();
                                        va.close();
                                        mdb.close();    

                                        /*Intent go = new Intent(AdminMultiColumn.this,MultiColumn.class);
                                        startActivity(go);*/
                                }
                              })
                            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });                  
                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();           
                            alertDialog.show();              
                 ///////alert displaing block ends here//////////////////////////////////           
        }

    }

}

What I have done now is within the getView method, taking the convertView and setting the background resource to a color, that too is not working…below is the code…

 if(holder.txtFOURTH!=null)
   {
    convertView.setBackgroundColor(R.drawable.image_border);
   }
   else
   {
    Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
   }
  • 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-05T13:53:45+00:00Added an answer on June 5, 2026 at 1:53 pm

    There two points –

    1. pos is not a good option as color

    2. Instead of view.setBackgroundColor(pos); sue

    LinearLayout row = (LinearLayout)view.findViewById(R.id.list_row);
            if(null!=row){
                //<listAdapter>.notifyDataSetChanged();
                row.setBackgroundResource(R.drawable.);
            }
    
    • assuming the your list row has LinearLayout in root having id “list_row”, chage this as per your layout.

    but there will be problem this as when you click on the second row it also be selected stated and better to call list.notifydatasetchaged there are few more option look over them, may fill your requirement

    1. android-selected-state-listview-example

    2. android-how-do-i-highlight-a-row-in-listview

    3. highlighting-the-selected-item-in-the-listview-in-android

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

Sidebar

Related Questions

I have a list view with 10 columns and I want each row to
I have a list view that displays a collection of items, each item has
I have a list view that is built using a simpleAdapter, each row is
In my application, I have list view. Selecting another item in it, triggers an
I have a list view with many rows and have it set so that
I have a list view item which has a button and displays properties on
I have a list view and depending on some logic I want to temporary
i have a list view with a search bar, i want to sort the
I have created a dialog that shows a multi-choice list of items that can
I have a multi select list in my View to send to controller a

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.