ranjith
ranjith

Reputation: 4536

Change text color of list items in array adapter

I create a list view and implement that list view in my custom dialog. that list view uses the array adapter and in my array adapter I am using my own layout with desired color. The code is given below.

 listView = new ListView(context);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
                        R.layout.my_spinner_layout, items);
listView.setAdapter(adapter);

Here, click listener for my list item is working fine.

Problem is starting now. I need a list view inside my Custom Alert Dialog with each row contains a radio button. I use the same method. Here is my code.

listView = new ListView(context);
                 ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,R.layout.my_single_choice_layout, choice);
                 listView.setAdapter(adapter);

here all radioButtons can be checked at same time. and my listener is not working fine.

my_spinner_layout_xml

<?xml version="1.0" encoding="utf-8"?>
<TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        style="@style/ListItemTextColor"
        />

and my_single_choice_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/my_choice_radio"
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"
    android:text="Option"
    style="@style/ListItemTextColor" >


</RadioButton>

Upvotes: 3

Views: 6927

Answers (2)

Shubham
Shubham

Reputation: 1442

Try this:

list.setAdapter(new EfficientAdapter(context,R.layout.my_single_choice_layout, choice));

Then create a class

 public class EfficientAdapter extends ArrayAdapter {

         private LayoutInflater mInflater;

            private String[] mStrings;

            private int mViewResourceId;

            public EfficientAdapter(Context ctx, int viewResourceId,String[] strings) {
                super(ctx, viewResourceId, strings);

                mInflater = (LayoutInflater)ctx.getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                mStrings = strings;

                mViewResourceId = viewResourceId;
            }
            @Override
            public int getCount() {
                return mStrings.length;
            }

            @Override
            public String getItem(int position) {
                return mStrings[position];
            }

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = mInflater.inflate(mViewResourceId, null);

                convertView.setMinimumHeight(132);
                TextView tv = (TextView)convertView.findViewById(R.id.option_text); //Give Id to your textview
                tv.setText(mStrings[position]);
                    tv.setTextColor(Color.RED);
                RadioButtons r=(RadioButtons)convertview.findviewById(Radio button id);
                r.setOnCheckedListener(new ur listener()
{
/////////Do whatever you wanna do overhere
});

                return convertView;
            }

    }

Hope it Helps.

Upvotes: 5

MohsinSyd
MohsinSyd

Reputation: 175

You have to use custom Adapter for that.

sample code for jst reference, Its not a full implementation

class Myadapter extends ArrayAdapter<String>{

        LayoutInflater inflater=null;

        public Myadapter(Context context, int resource, int textViewResourceId,
                List<String> objects) {
            super(context, resource, textViewResourceId, objects);
             inflater = (LayoutInflater)getLayoutInflater();
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View row = convertView;
            if(convertView==null){
              row= inflater.inflate(R.layout.activity_list_item, null);

              RadioButton rb = row.findViewById(R.id.radiobtn);

              rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub

                    //here write your code for radio button event 

                }
            });

            }
            return row;
        }
    }

Upvotes: 0

Related Questions