2

![I am using custom adapter for a listview in which i have three items text,buttons and radio buttons.I can select only one row at a time with the help of radio buttons.Now what i want,when i select the row using radio button the particular row of selected radio button should be set as with some color. here is my custom adapter code code in which all items are there.

package com.pramod.customlistviewwithradiobutton;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends ArrayAdapter<Item> {

     private final Context context;
     private boolean userSelected = false;
     private RadioButton mCurrentlyCheckedRB;
     private final ArrayList<Item> itemList;

     public CustomAdapter(Context context, ArrayList<Item> itemList) {

         super(context, R.layout.row_item, itemList);

         this.context = context;
         this.itemList = itemList;
     }

     @Override
     public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // Item rowItem = getItem(position);

         // 1. Create inflater 
         LayoutInflater inflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2. Get rowView from inflater
          View rowView = inflater.inflate(R.layout.row_item, parent, false);

         // 3. Get the two text view from the rowView
         Button btn = (Button) rowView.findViewById(R.id.button1);
         TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
         TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
         RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);

         // 4. Set the text for textView 
         tv1.setText(itemList.get(position).getName());
         tv2.setText(itemList.get(position).getAddress());
         System.out.println(""+getCount());

         if (position == getCount() - 1 && userSelected == false) {
          //   radio.setChecked(true);
             mCurrentlyCheckedRB = radio;
         } else {
             radio.setChecked(false);
         }
         btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();

            }
        });
         radio.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 if (mCurrentlyCheckedRB != null) {
                     if (mCurrentlyCheckedRB == null)
                         mCurrentlyCheckedRB = (RadioButton) v;
                     mCurrentlyCheckedRB.setChecked(true);

                     Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();

                     convertView.setBackgroundColor(Color.BLUE);
                 }

                 if (mCurrentlyCheckedRB == v)
                     return;

                 mCurrentlyCheckedRB.setChecked(false);

                 ((RadioButton) v).setChecked(true);
                 mCurrentlyCheckedRB = (RadioButton) v;
             }
         });

         return rowView;
     }

}
Here ,Note i don't have to put onclickitem on listview ,because i have a click on button and textview.][1]
abh22ishek
  • 2,631
  • 4
  • 27
  • 47

3 Answers3

1

Try the following code: In get view set background to your rowView instead of convertView because the view that you return from the getView is the one that is displayed.

 @Override
     public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // Item rowItem = getItem(position);

        final View rowView;
         // 1. Create inflater 
         if(convertView==null){
         LayoutInflater inflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2. Get rowView from inflater
          rowView = inflater.inflate(R.layout.row_item, parent, false);
        }else{
          rowView=convertView;
        }
         // 3. Get the two text view from the rowView
         Button btn = (Button) rowView.findViewById(R.id.button1);
         TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
         TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
         RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);

         // 4. Set the text for textView 
         tv1.setText(itemList.get(position).getName());
         tv2.setText(itemList.get(position).getAddress());
         System.out.println(""+getCount());

         if (position == getCount() - 1 && userSelected == false) {
          //   radio.setChecked(true);
             mCurrentlyCheckedRB = radio;
         } else {
             radio.setChecked(false);
         }
         btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();

            }
        });
         radio.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 if (mCurrentlyCheckedRB != null) {
                     if (mCurrentlyCheckedRB == null)
                         mCurrentlyCheckedRB = (RadioButton) v;
                     mCurrentlyCheckedRB.setChecked(true);

                     Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();

                     rowView.setBackgroundColor(Color.BLUE);
                 }

                 if (mCurrentlyCheckedRB == v)
                     return;

                 mCurrentlyCheckedRB.setChecked(false);

                 ((RadioButton) v).setChecked(true);
                 mCurrentlyCheckedRB = (RadioButton) v;
             }
         });

         return rowView;
     }

EDIT

Try this adapter class. In this you have selectedItemIndex in adapter which you update every time one item of the list is getting selected and notify your adapter and in getView you check if position is equals to selectedItemIndex then set blue color otherwise set default color.

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends ArrayAdapter<Item> {

     private final Context context;
     private boolean userSelected = false;
     private RadioButton mCurrentlyCheckedRB;
     private final ArrayList<Item> itemList;
     private int selectedItemIndex=-1;

     public CustomAdapter(Context context, ArrayList<Item> itemList) {

         super(context, R.layout.row_item, itemList);

         this.context = context;
         this.itemList = itemList;
     }

 @Override
     public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // Item rowItem = getItem(position);

        final View rowView;
         // 1. Create inflater 
         if(convertView==null){
         LayoutInflater inflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2. Get rowView from inflater
          rowView = inflater.inflate(R.layout.row_item, parent, false);
        }else{
          rowView=convertView;
        }
         // 3. Get the two text view from the rowView
         Button btn = (Button) rowView.findViewById(R.id.button1);
         TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
         TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
         RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);

         if(position==selectedItemIndex){
             rowView.setBackgroundColor(Color.BLUE);
                     radio.setChecked(true);//Check here
         }else{
             rowView.setBackgroundColor(Color.WHITE);//Color when not selected
                     radio.setChecked(false);//Uncheck here
         }
         // 4. Set the text for textView 
         tv1.setText(itemList.get(position).getName());
         tv2.setText(itemList.get(position).getAddress());
         System.out.println(""+getCount());

         if (position == getCount() - 1 && userSelected == false) {
          //   radio.setChecked(true);
             mCurrentlyCheckedRB = radio;
         } else {
             radio.setChecked(false);
         }
         btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();

            }
        });
         radio.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 if (mCurrentlyCheckedRB != null) {
                     if (mCurrentlyCheckedRB == null)
                         mCurrentlyCheckedRB = (RadioButton) v;
                     mCurrentlyCheckedRB.setChecked(true);

                     Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
                     selectedItemIndex=position;
             CustomAdapter.this.notifyDataSetChanged();
                 }

                 if (mCurrentlyCheckedRB == v)
                     return;

                 mCurrentlyCheckedRB.setChecked(false);

                 ((RadioButton) v).setChecked(true);
                 mCurrentlyCheckedRB = (RadioButton) v;
             }
         });

         return rowView;
     }

}
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Thanks ,the row is coloring but as i mentioned in my question,its need to color only one row at a time on the basis of radiobutton. whenever other radio button is clicked,previous color in the row should be removed.This is not happening in this case . – abh22ishek Dec 08 '13 at 10:59
  • thanks @vipul it is working,but there is little problem ,when row is selecting a color,the radio button is not checked. – abh22ishek Dec 08 '13 at 15:13
  • in your getView you can make it checked as well. Check out another edit – vipul mittal Dec 08 '13 at 16:18
0

I believe all you're missing inside your onClick(View v) method is:

View parentView = (View) v.getParent();
parentView.setBackgroundColor(Color.BLUE);

Instead of convertView.setBackgroundColor(Color.BLUE) , even though it is in the getView() method it cannot access its parameters.

EDIT: You also need to check when a particular radio button becomes unchecked so you revert the background color to default using the same process.

EDIT#2: You should check this answer

Community
  • 1
  • 1
nstosic
  • 2,584
  • 1
  • 17
  • 21
  • Be careful if the `radio` is tied to a `RadioGroup` then you need to put `(View)((View)v.getParent()).getParent()` in the first line to get the appropriate layout. – nstosic Dec 08 '13 at 08:08
  • i don't have a radio group,its only a radio button. – abh22ishek Dec 08 '13 at 10:48
  • can u tell me how i should check for the condition here in this code ,that only a radio button is checked then only color should be put and whenever the other radio button is checked ,the previous one is discarded. – abh22ishek Dec 08 '13 at 11:10
  • Added a link in Edit#2 – nstosic Dec 08 '13 at 11:37
0

a simple solution is to create a custom method in your adapter called:

public void setChosenPosition(int position){
    this.chosenPosition = position;
}

then in getView, check to see if chosenPosition == position, then use that to set the particular background.

Of course in the listener, you call the method setChosenPosition. I would simply listen in the activity, but in the adapter is fine too.

learner
  • 11,490
  • 26
  • 97
  • 169