gpt4 book ai didi

android - ListView 中 radio 组的 OnChecked 事件多次触发

转载 作者:太空狗 更新时间:2023-10-29 12:55:04 26 4
gpt4 key购买 nike

我在 ListView 的每一行中创建了一个包含 radio 组的应用程序。问题是,当我滚动 ListView 时, radio 组的选择会随着 radio 组的 oncheckedevent 多次触发而改变。

这是我的 getView 函数:

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new WeatherHolder();

holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
holder.txtTitle = (TextView)row.findViewById(R.id.toptext);
holder.rdgCategory = (RadioGroup)row.findViewById(R.id.radiogroup);

RadioGroup.OnCheckedChangeListener rdGrpCheckedListener = new RadioGroup.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// setCategoryinList(position, checkedId);
switch (checkedId){
case R.id.option1:
Log.i("WeatherAdapter","Data set index : " + position + " category : 1");
data.get(position).setCategory(1);
break;
case R.id.option2:
Log.i("WeatherAdapter","Data set index : " + position + " category : 2");
data.get(position).setCategory(2);
break;
default:
Log.i("WeatherAdapter","Data set index : " + position + " category : 0");
data.get(position).setCategory(0);
break;
}
}
};

holder.rdgCategory.setOnCheckedChangeListener(rdGrpCheckedListener);

row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}

Weather weather = data.get(position);
holder.txtTitle.setText(weather.getAppName());
holder.imgIcon.setImageDrawable(weather.getIcon());
int objCategory = weather.category;
Log.i("WeatherAdapter","Data get name : " + weather.getAppName() + " index : " + position + " category : " + objCategory);
switch (objCategory)
{
case option1:
holder.rdgCategory.check(R.id.option1);
break;
case option2:
holder.rdgCategory.check(R.id.option2);
break;
default:
holder.rdgCategory.check(R.id.none);
break;
}
return row;
}

下面是我滚动 ListView 时在 logcat 中得到的跟踪:

10-02 21:31:29.844: INFO/WeatherAdapter(28361): Data get name : ProgressBar index : 11 category : 0
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 1
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 0
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 0

感谢任何帮助。

最佳答案

我在之前的回答中已经回答过了。所以你可以看看this answer .

关于android - ListView 中 radio 组的 OnChecked 事件多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7627529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com