自定义列表视图中的单选按钮上的条件

时间:2014-07-21 19:14:16

标签: android listview radio-button

我创建了一个自定义列表视图,其中包含一个按钮,一个单选按钮(一次选中一个),一个textview ...从数据库中更新...我正在尝试发送“文本”例如: - 如果在listview第一行的radiobutton被检查,那么它旁边的textview应该是需要发送的“文本”.... plz帮助我如何在无线电按钮上应用条件在自定义列表视图.....我被困在这里,无法找到类似的问题.....

到目前为止我用我的名单,radiobuttons& amp;适配器就像....

适配器类      package com.example.smarttext2;

public class adapterdemo extends BaseAdapter {
Context context;
String[] str;
LayoutInflater inflater;
 int selectedPosition =0;

public adapterdemo(Context con,String[] st1) {
    // TODO Auto-generated constructor stub
    context = con;
    str=st1;
    inflater = ((Activity)con).getLayoutInflater();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return str.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView( final int position, final View arg1, ViewGroup arg2) {
    // TODO Auto-generated method s tub
    View v = arg1;
    v = inflater.inflate(R.layout.customlist, null);
    TextView text = (TextView)v.findViewById(R.id.textView1);
    text.setText(str[position]);
    final RadioButton rdb = (RadioButton)v.findViewById(R.id.radio0);


   rdb.setChecked(position == selectedPosition );
   rdb.setTag(position);
    rdb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedPosition = (Integer)view.getTag();
            notifyDataSetInvalidated();
        }
    });

    if(position==selectedPosition)
    {
        rdb.setChecked(true);
    }
    else
    {
        rdb.setChecked(false);
    }

    v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            selectedPosition = position;
            notifyDataSetChanged();

        }
    });


    return v;
}





 }

主要

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.second);
   list =(ListView)findViewById(R.id.listView1);
   cursor = demo1.fetch();
   count = cursor.getCount();
   str = new String[count];

    cursor.moveToFirst();
    for(int i=0;i<count;i++)
    {   
        str[i] = cursor.getString(0);
        Toast.makeText(getApplicationContext(), str[i], 3000).show();
        cursor.moveToNext();
    }
     adapter= new adapterdemo(this, str);
     list.setAdapter(adapter);

     list.setChoiceMode(list.CHOICE_MODE_SINGLE);

1 个答案:

答案 0 :(得分:0)

我有类似情况的复选框......

如果你的问题正确,你需要: 这是方法getView(...)的内部 1. rdb.setTag(position); 2. rdb.setOnCheckedChangeListener(checkListener); 接下来是在任何方法之外:

OnCheckedChangeListener checkListener = new OnCheckedChangeListener(){RadioGroup group, int checkedId){
    int pos = Integer.parseInt(buttonView.getTag().toString());
    long id = getItemId(pos);
    DB mDB = new DB(context);
    RadioBatton rdb = (RadioButton)yourListView.getChildAt(pos);
    TextView tv = (TextView)yourListView.getChildAt(pos);

    if(rdb.isChecked){
      Cursor cursor = mDB.query(TABLE_NAME, null, ID + " = " + id, null, null, null);
      cursor.moveToFirst){
      String text = cursor.getString(cursor.getColumnIndex(COL_NAME));
      tv.setText(text);
    }else{
        //something else
    }
};

DB - 是您的数据库类,其中应该是方法mDB.query(parameters for sorting data);

希望这有所帮助...