0

I am getting values from a file onto listview and then if I check a radio button in listview to give a grade(a,b,c) why the other radio button of listview is checked also?

public void read(View v) {



    String row;

    try {
        File file = new File("/sdcard/BatchData.csv");
        if(!file.exists())
        {

            Toast.makeText(getApplicationContext(), "file Dont Exist", Toast.LENGTH_SHORT).show();
        }
        FileInputStream in = new FileInputStream(file);
        BufferedReader myReader = new BufferedReader(new InputStreamReader(in));
        Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
        ArrayList<String> myStringArrayList = new ArrayList<String>();
        //s= (Spinner)findViewById(R.id.spinner);
        l= (ListView)findViewById(R.id.list);
        try {
            while((row= myReader.readLine())!=null)
            {
                String result= Arrays.toString(row.split(",")).replace("[", "").replace("]", "");
                //Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                myStringArrayList.add(result);

                //ArrayAdapter<String> a= new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_xml,R.id.name, myStringArrayList);
                ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_xml,R.id.name, myStringArrayList);
                //a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //s.setAdapter(a);
                l.setAdapter(adapter);

            }
            myReader.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80

2 Answers2

0

You have to keep row id of clicked Radio Button like

viewHolder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 

{

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                int getPosition = (Integer) buttonView.getTag();  
                list.get(getPosition).setSelected(buttonView.isChecked());
            }
});

And save position of view using seTag() method,

convertView.setTag(viewHolder);

convertView.setTag(R.id.label, viewHolder.your_radiobutton);
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
  • What is viewholder exactly? its giving error on view holder, saying to create it a class or local variable? – user2589011 Mar 18 '15 at 11:59
  • Its you Viewholding class instance like, private class ViewHolder { RadioButton yourRadioButton } And I think you are using adapter, so use above posted method in getView() method – Shekhar Mangrule Mar 18 '15 at 12:10
  • Yes i am using adapter, can you give me a hint that to use it in getview method as i have to retrieve data from every row of listview Thank you ! – user2589011 Mar 18 '15 at 12:25
  • i am getting this error when i use this method in getview method @shekhar mangarule The method setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) in the type CompoundButton is not applicable for the arguments (new RadioGroup.OnCheckedChangeListene(){})\ – user2589011 Mar 19 '15 at 06:02
  • Hey check it again, I posted detailed solution – Shekhar Mangrule Mar 19 '15 at 11:12
0

I'm posting detailed solution here.. Keep your adapter like this

public class YourAdapter extends ArrayAdapter<CustomObjectClass>

{ 

    private ArrayList<CustomObjectClass> list;

    . 
    .

@Override

public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder viewHolder = null;

    if (convertView == null)
    {
        viewHolder = new ViewHolder();
        viewHolder.your_radio_btn = (RadioButton) convertView.findViewById(R.id.radio_btn);

        viewHolder.your_radio_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                int getPosition = (Integer) buttonView.getTag();  
                list.get(getPosition).setSelected(buttonView.isChecked());
            }
        });

        convertView.setTag(viewHolder);
     }

     else

     {

                viewHolder = (ViewHolder) convertView.getTag();

    }

    viewHolder.your_radio_btn.setTag(position); // This line where you get already selected Radio Button

    return convertView;
}

Keep this inner View Holder class in your same adapter class

private class ViewHolder

{

    RadioButton your_radio_button;    

}