我有一个自定义列表视图,每行中的单选按钮,它工作正常,但我想从这个code到达选定的单选按钮的ID。例如,当我需要选择行的textview1价值,我该如何收集呢?提前致谢。这是我的code:
私有静态类适配器扩展了BaseAdapter {
私人LayoutInflater mInflater;
私人诠释mResourceId = 0;
私人单选mSelectedRB;
私人诠释mSelectedPosition = -1;
公共CitizenAdapter(上下文的背景下){
mInflater = LayoutInflater.from(上下文);
}
公众诠释getCount将(){
返回array.size();
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
最后ViewHolder持有人;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.table_row_citizen,NULL);
持有人=新ViewHolder();
holder.text1 =(TextView中)convertView.findViewById(R.id.TextView01);
holder.text2 =(TextView中)convertView.findViewById(R.id.TextView02);
holder.button =(单选)convertView.findViewById(R.id.radioButtonCitizen);
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text1.setText(array1.get(位置));
holder.text2.setText(array2.get(位置));
holder.button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果((位置= mSelectedPosition和放大器;!&安培;!mSelectedRB = NULL)){
mSelectedRB.setChecked(假);
}
mSelectedPosition =位置;
mSelectedRB =(单选)V;
}
});
如果(mSelectedPosition!=位置){
holder.button.setChecked(假);
}其他{
holder.button.setChecked(真正的);
如果(mSelectedRB = NULL和放大器;!&安培;!holder.button = mSelectedRB){
mSelectedRB = holder.button;
}
}
返回convertView;
}
静态类ViewHolder {
TextView的文本1;
TextView的文本2;
单选按钮;
}
}
解决方案
我想从这个code到达选定的单选按钮的ID。
您有 setTag和功放播放;得到getTag
属性来获取实际 ID
您选择的单选按钮,
holder.button.setTag(位置);
holder.button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
INT POS =(整数)v.getTag();
Log.i(令编辑@位置:的单选按钮ID+ POS);
字符串_Str1 = array1.get(POS);
字符串_Str2 = array2.get(POS);
}
});
此方式,您将得到的TextView的精确值从你行。
I have a custom listview and a radio button in each row, it works properly but i want to reach id of selected radio button from this code. For example when i need the textview1 value of selected row, how can i gather it? Thanks in advance. Here is my code:
private static class Adapter extends BaseAdapter {
private LayoutInflater mInflater;
private int mResourceId = 0;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
public CitizenAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return array.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.table_row_citizen, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
holder.button = (RadioButton)convertView.findViewById(R.id.radioButtonCitizen);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(array1.get(position));
holder.text2.setText(array2.get(position));
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if((position != mSelectedPosition && mSelectedRB != null)){
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton)v;
}
});
if(mSelectedPosition != position){
holder.button.setChecked(false);
}else{
holder.button.setChecked(true);
if(mSelectedRB != null && holder.button!= mSelectedRB){
mSelectedRB = holder.button;
}
}
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
RadioButton button;
}
}
解决方案
I want to reach id of selected radio button from this code.
You have to play with setTag & get getTag
property to fetch actual id
of your selected radio button,
holder.button.setTag(position);
holder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit @ position : " + pos);
String _Str1=array1.get(pos);
String _Str2=array2.get(pos);
}
});
This way you will get the exact values of textview from your rows.