简体   繁体   中英

Android listview item change background color

I can't change my listview item's background color.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_layout);
    ListView listView;
    listView = (ListView) findViewById(R.id.infolistView);
    final String[] values = new String[] { "TO1", "TO2", "TO3" };
    ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this,
                                 android.R.layout.simple_list_item_1,
                                 android.R.id.text1,
                                 values);
    listView.setAdapter(adapter);

    // This is not make anything , why ?
    View vi=adapter.getView(0, null, null);
    vi.setBackgroundColor(Color.YELLOW);

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                //But here is running good
                view.setBackgroundColor(Color.RED);}
    });

The way you are doing just gets an instance of a view filled with the data of position, which then dies and is garbage collected after the method is called.

To change the actual view in the list you will have to change color from your Adapter and write a custom Adapter, or have the color start out in that state.

Basically getView does not take from a list of views, but just makes a new view, unless you pass it a view.

You have to create Custom adapter to change the item's Background Color.

Here is the Example of Custom adapter:

public class PaListAdapter  extends BaseAdapter{
        private LayoutInflater mInflater;

         private ArrayList<String> platevalue = new ArrayList<String>();

           ViewHolder holder;
        public PaListAdapter(Context context,ArrayList<String> value)
        {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);



            //mycontext = context;
            platevalue.clear();
            platevalue =value;



        }


        public int getCount() 
        {
            return platevalue.size();
        }

        public Object getItem(int position) 
        {
            return position;
        }

        public long getItemId(int position) 
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {





            if (convertView == null) 
            {
                 convertView = mInflater.inflate(R.layout.select_dialog, null);

                holder = new ViewHolder();
                holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);

                holder.ch   =(CheckBox) convertView.findViewById(R.id.chk);


                convertView.setTag(holder);
            }
            else 
            {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.hTransID.setText(platevalue.get(position));




            return convertView;
        }

        static class ViewHolder 
        {      
              TextView    hTransID;


        }
    }


select_dialog.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
 android:background="#000000"
    >

    <TextView
        android:id="@+id/txtChoice"

        android:layout_gravity="center_vertical|left"
        android:gravity="center_vertical|left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textColor="#000000"/> 

</LinearLayout>

In Activity Class.Define it like:

   simpleefficientadapter efficientadapter;

   efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
   listView.setAdapter(efficientadapter);

You can create an instance of the listView item and use the getChildAt() method which accepts an integer. Then set the view instance background colour -

View focusedChild = listView.getChildAt(r);
focusedChild.setBackgroundColor(Color.RED);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM