问题
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);
回答1:
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...
来源:https://stackoverflow.com/questions/24872896/conditions-on-radiobuttons-in-custom-listview