Question

I have a listview. Each row of a listview contains two texts (name and aaddress), one button and one radio button. I am using CustomAdapter for that. Now I have a condition; if a text1(name) is equal to "Pramod", then I have to delete the radio button from entire rows.

What is happening exactly? The radio button of only that row is deleting.

I have to delete the radio button of all rows. How do I fix that?

Here is my code of customAdapter class:

public class CustomAdapter extends ArrayAdapter<Item> {

    private final Context context;
    // private boolean userSelected = false;
    public static RadioButton mCurrentlyCheckedRB;
    private final ArrayList<Item> itemList;
    int selected_itemindex=-1;
    static String abc="";

    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

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

        // 2. Get rowView from inflater
        final 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);
        final TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
        final 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());

        // This is the code I am using to delete the radio button from the entire row
        if (itemList.get(position).getName().toString().equals("Pramod")) {

            radio.setVisibility(View.INVISIBLE);
        }
        if (itemList.get(position).isSelected()) {
            radio.setChecked(true);
            rowView.setBackgroundColor(Color.CYAN);
        }
        else {
            rowView.setBackgroundColor(Color.WHITE);
            radio.setChecked(false);
        }

        // The radio button I am using to select a particular row one at a time.

        radio.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (itemList.get(position).isSelected()) {

                }
                else
                {
                    int selected_previousval = -1;
                    for (int i=0; i<itemList.size(); i++) {
                        if (itemList.get(i).isSelected()) {
                            selected_previousval = i;
                            break;
                        }
                    }
                    String name2=itemList.get(position).getName();
                    String add2=itemList.get(position).getAddress();

                    if (selected_previousval==-1) {
                        itemList.remove(position);
                        itemList.add(position,new Item(name2, add2, true));
                        rowView.setBackgroundColor(Color.CYAN);
                        Toast.makeText(context, "selected a  3 row",2000).show();
                        radio.setChecked(true);
                    }
                    else {
                        itemList.remove(position);
                        itemList.add(position, new Item(name2, add2, true));
                        name2 = itemList.get(selected_previousval).getName();
                        add2 = itemList.get(selected_previousval).getAddress();
                        itemList.remove(selected_previousval);
                        itemList.add(selected_previousval, new Item(name2, add2, false));
                        MainActivity.lv.setAdapter(new CustomAdapter(context, itemList));
                    }
                }
            }
        });

        return rowView;
    }
}

And this is my Arraylist class:

public class Item {

    private String Name;
    private String Address;
    public boolean selected;

    public boolean isSelected() {
        return this.selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;

    }

    public Item(String Name, String Address,boolean selected) {
        super();
        this.Name = Name;
        this.Address = Address;
        this.selected=selected;
    }

    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }
}
Was it helpful?

Solution

UPDATE

Question: I want to remove the radiobutton from every row of listview if text=pramod.

Answer:

MainActivity class

ArrayList<Item> items;
CustomAdapter adapter;
items = new ArrayList<Item>();
ArrayList<String> temparray;

items.add(new Item("Pramod",  "Ballia",     false, 0));
items.add(new Item("Pankaj",  "Mau",        false, 1));
items.add(new Item("Pradeep", "Ranchi",     false, 1));
items.add(new Item("Jitendra", "Varansi",   false, 1));
items.add(new Item("Amresh",   "Sonbhadra", false, 1));
items.add(new Item("Anil",     "Sarnath",   false, 1));

temparray = new ArrayList<String>();

for(int i=0; i<items.size(); i++)
{
    temparray.add(items.get(i).getName());
}

if (temparray.contains("Pramod"))
{
    Log.d("temparray", "contains Pramod");
          Global.show=0;
}
else
{
    Log.d("temparray", "not contain");
          Global.show=1;
}

lv = (ListView)dp.findViewById(R.id.listView1);
Spinner sp = (Spinner) dp.findViewById(R.id.spinner1);
adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);

customAdapter.class

if (Global.show == 0)
{
    radio.setVisibility(View.INVISIBLE);
}
else
{
    radio.setVisibility(View.VISIBLE);

    if (itemList.get(position).isSelected()) {
        radio.setChecked(true);
        rowView.setBackgroundColor(Color.CYAN);
    }
    else {
        rowView.setBackgroundColor(Color.WHITE);
        radio.setChecked(false);
    }
}

Global.class

public static class Global {

    Public static int show;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top