如何从android中的复选框中获取值
我想获取复选框中所选项目的名称。但我只获取了一些字母数字。如何从复选框中获取所选项目的名称如何从android中的复选框中获取值,android,Android,我想获取复选框中所选项目的名称。但我只获取了一些字母数字。如何从复选框中获取所选项目的名称 public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater();
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.customlistlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
enter code here
//System.out.println(itemname);
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
我得到了这样的东西:
CheckBox not = (CheckBox)findViewById(R.id.checkBox1);
not.setChecked(my.notifications);
not.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (((CheckBox) v).isChecked())
{
}
else
{
}
}
});
请尝试此示例代码。。实现你的代码
public class Planets extends Activity {
private ListView mainListView ;
private Planet[] planets ;
private ArrayAdapter<Planet> listAdapter ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// When item is tapped, toggle checked properties of CheckBox and Planet.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View item,
int position, long id) {
Planet planet = listAdapter.getItem( position );
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked( planet.isChecked() );
}
});
// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance() ;
if ( planets == null ) {
planets = new Planet[] {
new Planet("Mercury"),
new Planet("Venus"),
new Planet("Earth"),
new Planet("Mars"),
new Planet("Jupiter"),
new Planet("Saturn"),
new Planet("Uranus"),
new Planet("Neptune"),
new Planet("Ceres"),
new Planet("Pluto"),
new Planet("Haumea"),
new Planet("Makemake"),
new Planet("Eris")
};
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter( listAdapter );
}
/** Holds planet data. */
private static class Planet {
private String name = "" ;
private boolean checked = false ;
//public Planet() {}
public Planet( String name ) {
this.name = name ;
}
/*public Planet( String name, boolean checked ) {
this.name = name ;
this.checked = checked ;
}*/
public String getName() {
return name;
}
/*public void setName(String name) {
this.name = name;
}*/
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name ;
}
public void toggleChecked() {
checked = !checked ;
}
}
/** Holds child views for one row. */
private static class PlanetViewHolder {
private CheckBox checkBox ;
private TextView textView ;
//public PlanetViewHolder() {}
public PlanetViewHolder( TextView textView, CheckBox checkBox ) {
this.checkBox = checkBox ;
this.textView = textView ;
}
public CheckBox getCheckBox() {
return checkBox;
}
/* public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}*/
public TextView getTextView() {
return textView;
}
/*public void setTextView(TextView textView) {
this.textView = textView;
} */
}
/** Custom adapter for displaying an array of Planet objects. */
private static class PlanetArrayAdapter extends ArrayAdapter<Planet> {
private LayoutInflater inflater;
public PlanetArrayAdapter( Context context, List<Planet> planetList ) {
super( context, R.layout.simplerow, R.id.rowTextView, planetList );
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context) ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
Planet planet = (Planet) this.getItem( position );
CheckBox checkBox ;
TextView textView ;
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.simplerow, null);
textView = (TextView) convertView.findViewById( R.id.rowTextView );
checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
convertView.setTag( new PlanetViewHolder(textView,checkBox) );
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Planet planet = (Planet) cb.getTag();
planet.setChecked( cb.isChecked() );
if(cb.isChecked()){
String s = planet.getName();
System.out.println(s);
}
}
});
}
else {
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox() ;
textView = viewHolder.getTextView() ;
}
checkBox.setTag( planet );
checkBox.setChecked( planet.isChecked() );
textView.setText( planet.getName() );
return convertView;
}
}
public Object onRetainNonConfigurationInstance() {
return planets ;
}
}
公共类活动{
私有ListView主ListView;
私人行星[]行星;
专用阵列适配器列表适配器;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
//查找ListView资源。
mainListView=(ListView)findViewById(R.id.mainListView);
//点击项目时,切换复选框和行星的选中属性。
mainListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父项、视图项、,
内部位置,长id){
Planet Planet=listAdapter.getItem(位置);
planet.toggleChecked();
PlanetViewHolder=(PlanetViewHolder)项。getTag();
viewHolder.getCheckBox().setChecked(planet.isChecked());
}
});
//创建并填充行星。
行星=(行星[])GetLastNonfigurationInstance();
如果(行星==null){
行星=新行星[]{
新行星(“水星”),
新行星(“金星”),
新行星(“地球”),
新行星(“火星”),
新行星(“木星”),
新行星(“土星”),
新行星(“天王星”),
新行星(“海王星”),
新行星(“谷神星”),
新行星(“冥王星”),
新行星(“Haumea”),
新行星(“Makemake”),
新行星(“Eris”)
};
}
ArrayList planetList=新的ArrayList();
planetList.addAll(Arrays.asList(planets));
//将自定义阵列适配器设置为ListView的适配器。
listAdapter=新天文馆适配器(此,planetList);
mainListView.setAdapter(listAdapter);
}
/**保存行星数据*/
私人静态类行星{
私有字符串名称=”;
私有布尔值选中=false;
//公共行星(){}
公共行星(字符串名称){
this.name=名称;
}
/*公共行星(字符串名称,布尔值已选中){
this.name=名称;
this.checked=checked;
}*/
公共字符串getName(){
返回名称;
}
/*公共void集合名(字符串名){
this.name=名称;
}*/
公共布尔值已检查(){
退货检查;
}
公共void setChecked(布尔值已选中){
this.checked=checked;
}
公共字符串toString(){
返回名称;
}
public void toggleChecked(){
选中=!选中;
}
}
/**保存一行的子视图*/
专用静态类平面升降台{
私有复选框;
私有文本视图文本视图;
//公共平面视图文件夹(){}
公共平面视图文件夹(文本视图文本视图,复选框){
this.checkBox=复选框;
this.textView=textView;
}
公共复选框getCheckBox(){
返回复选框;
}
/*公共无效设置复选框(复选框){
this.checkBox=复选框;
}*/
公共文本视图getTextView(){
返回文本视图;
}
/*公共void setTextView(TextView TextView){
this.textView=textView;
} */
}
/**用于显示行星对象阵列的自定义适配器*/
专用静态类PlanetaryAdapter扩展ArrayAdapter{
私人充气机;
公共天文馆适配器(上下文上下文,列表天文馆列表){
super(context,R.layout.simplerow,R.id.rowTextView,planetList);
//缓存LayoutFlate以避免每次请求新的LayoutFlate。
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//要显示的行星
行星=(行星)this.getItem(位置);
复选框;
文本视图文本视图;
if(convertView==null){
convertView=充气机。充气(R.layout.simplerow,空);
textView=(textView)convertView.findViewById(R.id.rowTextView);
复选框=(复选框)convertView.findViewById(R.id.CheckBox01);
setTag(新的PlanetViewHolder(textView,复选框));
checkBox.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
复选框cb=(复选框)v;
行星=(行星)cb.getTag();
planet.setChecked(cb.isChecked());
if(cb.isChecked()){
字符串s=planet.getName();
系统输出打印项次;
}
}
});
}
否则{
PlanetViewHolder=(PlanetViewHolder)convertView.getTag();
checkBox=viewHolder.getCheckBox();
textView=viewHolder.getTextView();
}
复选框.setTag(行星);
checkBox.setChecked(planet.isChecked());
textView.setText(planet.getName());
返回视图;
}
}
公共对象OnRetainOnConfiguration实例(){
返回行星;
}
}
如果您想知道已(取消)选中了哪个复选框,您还可以为每个复选框注册一个新的侦听器实例
CheckBox one = (CheckBox)findViewById(R.id.checkBox1);
one.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d(TAG,"I am checkbox 1!");
}
});
CheckBox two = (CheckBox)findViewById(R.id.checkBox2);
two.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d(TAG,"I am checkbox 2!");
}
});
请尝试以下代码:
String str = yourCheckbox.getText();
为复选框设置标记,就像您为复选框指定的名称一样,也为该复选框设置标记,然后您可以访问setOnClickListener中的选项卡
viewHolder.checkbox.setTag("Write here name");
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
Log.v("Checked Name",buttonview.getTag().toString());
}
});