在自定义列表视图单选按钮条件 [英] conditions on radiobuttons in custom listview

查看:85
本文介绍了在自定义列表视图单选按钮条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了是有一个按钮,自定义列表视图,一个单选按钮(一次一个被选中),它会从数据库中更新一个TextView ... ..事情是我试图发送文本在TextView中以特定数字。对于-例如: - 如果列表视图中的第一行的单选按钮被选中然后TextView的旁边应该是文本需要被发送.... plz帮助我如何在申请单选框条件,被夸大自定义列表视图.....我来到这里,无法坚持找到类似这样的问题.....

东西我有我的名单,单选按钮和放大器迄今所做的;适配器就像....

适配器类
     包com.example.smarttext2;

 公共类adapterdemo延伸BaseAdapter {
上下文语境;
的String []海峡;
LayoutInflater吹气;
 INT selectedPosition = 0;公共adapterdemo(上下文CON,字符串[] ST1){
    // TODO自动生成构造函数存根
    上下文= CON;
    海峡= ST1;
    吹气=((活动)CON).getLayoutInflater();
}@覆盖
公众诠释的getCount(){
    // TODO自动生成方法存根
    返回str.length;
}@覆盖
公共对象的getItem(INT为arg0){
    // TODO自动生成方法存根
    返回将arg0;
}@覆盖
众长getItemId(INT为arg0){
    // TODO自动生成方法存根
    返回将arg0;
}@覆盖
公共查看getView(最终诠释的位置,最后查看ARG1,ARG2的ViewGroup){
    // TODO自动生成方法S浴缸
    视图V = ARG1;
    V = inflater.inflate(R.layout.customlist,NULL);
    TextView的文本=(TextView的)v.findViewById(R.id.textView1);
    text.setText(STR [位置]);
    最后单选RDB =(单选)v.findViewById(R.id.radio0);
   rdb.setChecked(位置== selectedPosition);
   rdb.setTag(位置);
    rdb.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            selectedPosition =(整数)view.getTag();
            notifyDataSetInvalidated();
        }
    });    如果(位置== selectedPosition)
    {
        rdb.setChecked(真);
    }
    其他
    {
        rdb.setChecked(假);
    }    v.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            selectedPosition =位置;
            notifyDataSetChanged();        }
    });
    返回伏;
} }

主要

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.second);
   名单=(ListView控件)findViewById(R.id.listView1);
   光标= demo1.fetch();
   数= cursor.getCount();
   海峡=新的String [统计]    cursor.moveToFirst();
    的for(int i = 0; I<计数;我++)
    {
        海峡[I] = cursor.getString(0);
        Toast.makeText(getApplicationContext(),STR [I],3000).show();
        cursor.moveToNext();
    }
     适配器=新adapterdemo(这一点,STR);
     list.setAdapter(适配器);     list.setChoiceMode(list.CHOICE_MODE_SINGLE);


解决方案

我曾与复选框类似的情况...

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

  OnCheckedChangeListener checkListener =新OnCheckedChangeListener(){RadioGroup中组,诠释checkedId){
    INT POS =的Integer.parseInt(buttonView.getTag()的toString());
    长ID = getItemId(POS)
    DB = MDB新DB(背景);
    RadioBatton RDB =(单选)yourListView.getChildAt(POS)
    TextView的电视=(TextView中)yourListView.getChildAt(POS)    如果(rdb.isChecked){
      光标光标= mDB.query(TABLE_NAME,空,ID +=+ ID,NULL,NULL,NULL);
      cursor.moveToFirst){
      字符串文本= cursor.getString(cursor.getColumnIndex(COL_NAME));
      tv.setText(文本);
    }其他{
        //别的东西
    }
};

DB - 是你的数据库所在班级应该是方法 mDB.query(用于排序的数据参数);

希望这有助于在某种程度上...

I have created a custom listview which is having a button,a radiobutton(one is selected at a time),a textview...which gets updated from database.. the thing is i am trying to send the "text" in textview to particular number .For-example:- if in listview first row's radiobutton is checked then textview beside it should be the "text" which needs to be send....plz help me how to apply conditions on radiobutton which gets inflated on custom listview.....i got stuck here and unable to find problems similar to this.....

things i have done so far with my list,radiobuttons & adapter are like....

adapter class 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;
}





 }

main

    @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);

解决方案

I had similar situation with checkboxes...

if got your question correctly you need: this is inside of the method getView(...) 1. rdb.setTag(position); 2. rdb.setOnCheckedChangeListener(checkListener); Next is outside of the any method:

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 - is your database class where should be the method mDB.query(parameters for sorting data);

Hope this helps somehow...

这篇关于在自定义列表视图单选按钮条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆