如果我检查列表视图中的单选按钮,则会检查listview的其他单选按钮

时间:2015-03-18 11:32:43

标签: android listview

我从一个文件到listview获取值,然后如果我检查listview中的单选按钮给出一个等级(a,b,c)为什么还检查listview的另一个单选按钮?

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();
    }
}

2 个答案:

答案 0 :(得分:0)

您必须保持点击的单选按钮的行ID,如

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());
            }
});

使用seTag()方法

保存视图位置
convertView.setTag(viewHolder);

convertView.setTag(R.id.label, viewHolder.your_radiobutton);

答案 1 :(得分:0)

我在这里发布详细的解决方案..保持你的适配器像这样

public class YourAdapter extends ArrayAdapter<CustomObjectClass>

{ 

    private ArrayList<CustomObjectClass> list;

    . 
    .

@覆盖

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;
}

将此内部View Holder类保留在同一个适配器类

私人类ViewHolder

{

    RadioButton your_radio_button;    

}