gpt4 book ai didi

android - ListView 中的单选按钮

转载 作者:行者123 更新时间:2023-11-29 01:35:44 25 4
gpt4 key购买 nike

我将 RadioGroup 用作 ListView 中的行项目。 RadioGroup 有 5 个单选按钮,我有 1000 行。我无法管理单选按钮的状态。我已经尝试使用 Map 来存储位置和检查状态。我什至尝试制作类并使用 setTag() 和 getTag() 保存选中状态和单选按钮 ID。还没有运气。有什么建议吗?

代码

适配器类

package com.example.adapters;

import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.R;
import com.example.models.AnswerStateModel;

public class AnswerKeyAdapter extends ArrayAdapter<AnswerStateModel> {

private Activity activity;
private List<AnswerStateModel> test;
Toast t = null;


public AnswerKeyAdapter(Activity activity, List<AnswerStateModel> temp) {
super(activity, R.layout.row_item_answer_key, temp);
this.activity = activity;
this.test = temp;
}

@Override
public int getCount() {
return test.size();
}

@Override
public AnswerStateModel getItem(int position) {
return test.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int currentPosition=position;
View view=null;
if (convertView == null) {
final ViewHolder holder = new ViewHolder();
view = LayoutInflater.from(activity).inflate(
R.layout.row_item_answer_key, parent,false);
holder.lblAnswerId = (TextView) view.findViewById(R.id.lblAnswerId);
holder.rdGroup = (RadioGroup)view.findViewById(R.id.rdGroup);
holder.btnA = (RadioButton) view.findViewById(R.id.btnA);
holder.btnB = (RadioButton) view.findViewById(R.id.btnB);
holder.btnC = (RadioButton) view.findViewById(R.id.btnC);
holder.btnD = (RadioButton) view.findViewById(R.id.btnD);
holder.btnE = (RadioButton) view.findViewById(R.id.btnE);
holder.btnWhat = (Button) view.findViewById(R.id.btnWhat);

view.setTag(holder);
holder.rdGroup.setTag(test.get(position));



holder.rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
AnswerStateModel model=(AnswerStateModel) holder.rdGroup.getTag();

switch (checkedId) {
case R.id.btnA:
model.setChecked(holder.btnA.isChecked());
model.setBtnId(holder.btnA.getId());
break;
case R.id.btnB:
model.setChecked(holder.btnB.isChecked());
model.setBtnId(holder.btnB.getId());
break;
case R.id.btnC:
model.setChecked(holder.btnC.isChecked());
model.setBtnId(holder.btnC.getId());
break;
case R.id.btnD:
model.setChecked(holder.btnD.isChecked());
model.setBtnId(holder.btnD.getId());
break;
case R.id.btnE:
model.setChecked(holder.btnE.isChecked());
model.setBtnId(holder.btnE.getId());
break;
default:
break;
}
}
});


} else{

view=convertView;
((ViewHolder) view.getTag()).rdGroup.setTag(test.get(position));

}


final ViewHolder holder = (ViewHolder) view.getTag();

AnswerStateModel model=(AnswerStateModel)getItem(currentPosition);

holder.btnA.setChecked(false);
holder.btnB.setChecked(false);
holder.btnC.setChecked(false);
holder.btnD.setChecked(false);
holder.btnE.setChecked(false);

switch (model.getBtnId()) {
case R.id.btnA:
holder.btnA.setChecked(test.get(position).isChecked());
break;
case R.id.btnB:
holder.btnB.setChecked(test.get(position).isChecked());
break;
case R.id.btnC:
holder.btnC.setChecked(test.get(position).isChecked());
break;
case R.id.btnD:
holder.btnD.setChecked(test.get(position).isChecked());
break;
case R.id.btnE:
holder.btnE.setChecked(test.get(position).isChecked());
break;
default:
break;
}

holder.lblAnswerId.setText(position+1+"");

holder.btnWhat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.isSelected())
v.setSelected(false);
else
v.setSelected(true);
}
});

return view;
}

private static class ViewHolder {
private TextView lblAnswerId;
private RadioButton btnA;
private RadioButton btnB;
private RadioButton btnC;
private RadioButton btnD;
private RadioButton btnE;
private RadioGroup rdGroup;
private Button btnWhat;
}

void showToast(String text) {

if (t != null)
t.cancel();

t = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
t.show();
}
}

Pojo类

package com.example.models;

public class AnswerStateModel {

private boolean isChecked=false;
private int btnId=0;
private int currentPosition=0;
private String correctAns="";
private int btnPosition=0;



public int getBtnPosition() {
return btnPosition;
}
public void setBtnPosition(int btnPosition) {
this.btnPosition = btnPosition;
}
public String getCorrectAns() {
return correctAns;
}
public void setCorrectAns(String correctAns) {
this.correctAns = correctAns;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public int getBtnId() {
return btnId;
}
public void setBtnId(int btnId) {
this.btnId = btnId;
}
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}



}

最佳答案

解决了!二手SparseIntArray

这是完整的代码。

/**
*
*/

package com.example.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.R;
import com.example.models.AnswerStateModel;
import com.example.utils.Config;

public class AnswerKeyAdapter extends ArrayAdapter<AnswerStateModel> {

private Activity activity;
private ArrayList<AnswerStateModel> mAnswerList;
Toast t = null;
private SparseIntArray mSpCheckedState=new SparseIntArray();

public AnswerKeyAdapter(Activity activity, ArrayList<AnswerStateModel> mAnswerList) {
super(activity, R.layout.row_item_answer_key, mAnswerList);
this.activity = activity;
this.mAnswerList = mAnswerList;
}

@Override
public int getCount() {
return mAnswerList.size();
}

@Override
public AnswerStateModel getItem(int position) {
return mAnswerList.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.row_item_answer_key, parent, false);
holder.lblAnswerId = (TextView) convertView.findViewById(R.id.lblAnswerId);
holder.rdGroup = (RadioGroup) convertView.findViewById(R.id.rdGroup);
holder.btnA = (RadioButton) convertView.findViewById(R.id.btnA);
holder.btnB = (RadioButton) convertView.findViewById(R.id.btnB);
holder.btnC = (RadioButton) convertView.findViewById(R.id.btnC);
holder.btnD = (RadioButton) convertView.findViewById(R.id.btnD);
holder.btnE = (RadioButton) convertView.findViewById(R.id.btnE);
holder.btnWhat = (Button) convertView.findViewById(R.id.btnWhat);
convertView.setTag(holder);

} else {
holder=(ViewHolder)convertView.getTag();
}

holder.rdGroup.setOnCheckedChangeListener(null);
holder.rdGroup.clearCheck();

Config.error("Spars "+mSpCheckedState.get(position));
if(mSpCheckedState.indexOfKey(position)>-1){
holder.rdGroup.check(mSpCheckedState.get(position));
}else{
holder.rdGroup.clearCheck();
}

holder.rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId>-1){
mSpCheckedState.put(position, checkedId);
}else{
if(mSpCheckedState.indexOfKey(position)>-1)
mSpCheckedState.removeAt(mSpCheckedState.indexOfKey(position));
}
}
});



Config.error("Current Position " + position);
holder.lblAnswerId.setText(position + 1 + "");

holder.btnWhat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.isSelected())
v.setSelected(false);
else
v.setSelected(true);
}
});

return convertView;
}

private static class ViewHolder {
private TextView lblAnswerId;
private RadioButton btnA;
private RadioButton btnB;
private RadioButton btnC;
private RadioButton btnD;
private RadioButton btnE;
private RadioGroup rdGroup;
private Button btnWhat;
}

void showToast(String text) {

if (t != null)
t.cancel();

t = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
t.show();
}
}

关于android - ListView 中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28214305/

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