如果我检查列表视图中的单选按钮,则列表视图的其他单选按钮会被选中 [英] If I check a radio button in listview then other radio buttons of listview get checked
本文介绍了如果我检查列表视图中的单选按钮,则列表视图的其他单选按钮会被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将文件中的值获取到列表视图中,然后如果我在列表视图中检查单选按钮以给出等级(a,b,c),为什么还要同时检查列表视图的另一个单选按钮?
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();
}
}
推荐答案
我在这里发布详细的解决方案.
I'm posting detailed solution here.. Keep your adapter like this
public class YourAdapter extends ArrayAdapter<CustomObjectClass>
{
private ArrayList<CustomObjectClass> list;
.
.
@Override
@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;
}
将此内部View Holder类保留在同一适配器类中
Keep this inner View Holder class in your same adapter class
私有类ViewHolder
private class ViewHolder
{
RadioButton your_radio_button;
}
这篇关于如果我检查列表视图中的单选按钮,则列表视图的其他单选按钮会被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文