自定义Listview与CheckBox单选 [英] Custom Listview with CheckBox single selection

查看:134
本文介绍了自定义Listview与CheckBox单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我使用checkbox / RadioButton创建自定义列表视图。

我尝试使用这个 lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

$ c>但它不适用于我。有任何其他解决方案,然后请让我知道。



main.java

  private ImageAdapter适配器; 

private static String month [] = {January,February,March,April,May,
June,July,August ,September,
October,November,December};


/ **当第一次创建活动时调用。 * /
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView lstvw =(ListView)findViewById(R.id.listView);

adapter = new ImageAdapter(this,month);
lstvw.setAdapter(adapter);
lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

lstvw.setOnItemClickListener(this);
}



我不知道如何添加复选框的代码到适配器class.please检查适配器类如下。



ImageAdapter.class

  public class ImageAdapter extends BaseAdapter {

public String title [];
public String description [];
public Activity context;
public LayoutInflater充气器;

public ImageAdapter(Activity context,String [] title){
super();

this.context = context;
this.title = title;

this.inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount(){
// TODO自动生成的方法存根
return title.length;
}

@Override
public Object getItem(int position){
// TODO自动生成的方法存根
return null;
}

@Override
public long getItemId(int position){
// TODO自动生成的方法存根
return 0;
}

public static class ViewHolder
{

TextView txtViewTitle;

}

@Override
public View getView(int position,View convertView,ViewGroup parent){
// TODO自动生成方法存根

ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitem,null);

holder.txtViewTitle =(TextView)convertView.findViewById(R.id.lstvw_textView);
convertView.setTag(holder);
}
else
holder =(ViewHolder)convertView.getTag();

holder.txtViewTitle.setText(title [position]);

return convertView;
}

}

strong>



Listitem.xml

 <?xml version = 1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =fill_parent
android:orientation =horizo​​ntal>

< TextView
android:id =@ + id / lstvw_textView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentLeft =true
android:layout_toLeftOf =@ + id / itemCheckBox
android:padding =8dp
android:text =hello world/&



< CheckBox
android:id =@ + id / itemCheckBox
android:layout_width =wrap_content
:layout_height =wrap_content
android:layout_alignParentRight =true
android:gravity =right/>

解决方案>

Home.java(Activity)这是我每次只需要选择单个项目的方法。



  package com.lvcheck.activities; 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Home extends Activity
{
private ListView lvCheckBox;
private Button btnCheckAll,btnClearALl;
private String [] arr = {One,Two,Three,Four,Five,Six};

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnCheckAll =(Button)findViewById(R.id.btnCheckAll);
btnClearALl =(Button)findViewById(R.id.btnClearAll);

lvCheckBox =(ListView)findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCheckBox.setAdapter(new ArrayAdapter< String>(this,
android.R.layout.simple_list_item_multiple_choice,arr));


btnCheckAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
for(int i = 0; i< lvCheckBox.getAdapter()。getCount(); i ++)
{
lvCheckBox.setItemChecked(i,true);
}
}
});

btnClearALl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
for i = 0; i {
lvCheckBox.setItemChecked(i,false);
}
} $ b b});
}
}

和我的(main.xml)XML

;
< LinearLayout
xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =vertical
android:layout_width =fill_parent
android:layout_height =fill_parent>

< LinearLayout
android:layout_width =fill_parent
android:orientation =horizo​​ntal
android:gravity =center layout_height =wrap_content>

< Button
android:layout_width =wrap_content
android:text =Check All
android:layout_marginRight =7dip
android :id =@ + id / btnCheckAll
android:layout_height =wrap_content>
< / Button>
< Button
android:layout_width =wrap_content
android:text =Clear All
android:id =@ + id / btnClearAll
android :layout_height =wrap_content>
< / Button>
< / LinearLayout>

< ListView
android:layout_width =fill_parent
android:id =@ + id / lvCheckBox
android:fadingEdge =none
android:cacheColorHint =@ null
android:layout_height =fill_parent>
< / ListView>
< / LinearLayout>

因此输出将是这样的。



>



此处



如果您对此有任何麻烦,请告诉我们。



修改:查看此实用链接:自定义单一选项ListView < a>


Here I am creating custom listview with checkbox / RadioButton. I got this but I need the single selection for that.

I try using this lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE); but it not works for me. Is there any other solutions for that then please let me know.

main.java

  private ImageAdapter  adapter; 

private static String month[] = {"January","February","March","April","May",  
    "June","July","August","September",  
    "October","November","December"};  


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView lstvw = (ListView) findViewById(R.id.listView);

     adapter = new ImageAdapter(this, month);  
     lstvw.setAdapter(adapter);  
      lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

     lstvw.setOnItemClickListener(this);
}

I have no idea for the how to add code for the checkbox into the adapter class.please check the adapter class as below.

ImageAdapter.class

public class ImageAdapter extends BaseAdapter{

public String title[];  
public String description[];  
public Activity context;  
public LayoutInflater inflater;

public ImageAdapter(Activity context,String[] title) {  
    super();  

    this.context = context;  
    this.title = title;  

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

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

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

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

public static class ViewHolder  
{  

    TextView txtViewTitle;  

}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  

    ViewHolder holder;  
    if(convertView==null)  
    {  
        holder = new ViewHolder();  
        convertView = inflater.inflate(R.layout.listitem, null);  

        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.lstvw_textView);  
        convertView.setTag(holder);  
    }  
    else  
        holder=(ViewHolder)convertView.getTag();  

    holder.txtViewTitle.setText(title[position]);  

    return convertView;  
}   

}

EDIT:

Listitem.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/lstvw_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/itemCheckBox"
    android:padding="8dp"
    android:text="hello world" />



<CheckBox
    android:id="@+id/itemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right" />

解决方案

Here is what i would do if i need to select only single item at a time.

Home.java (Activity)

package com.lvcheck.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Home extends Activity 
{
    private ListView lvCheckBox;
    private Button btnCheckAll, btnClearALl;
    private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
        btnClearALl = (Button)findViewById(R.id.btnClearAll);

        lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
        lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvCheckBox.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, arr));


        btnCheckAll.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View arg0) 
            {
                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, true);             
                }
            }
        });

        btnClearALl.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View v) 
            {
                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, false);                
                }
            }
        });
    }
}

and my (main.xml) XML file should like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:text="Check All"
            android:layout_marginRight="7dip"
            android:id="@+id/btnCheckAll"
            android:layout_height="wrap_content">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:text="Clear All"
            android:id="@+id/btnClearAll"
            android:layout_height="wrap_content">
        </Button>
    </LinearLayout>

    <ListView
        android:layout_width="fill_parent"
        android:id="@+id/lvCheckBox"
        android:fadingEdge="none"
        android:cacheColorHint="@null"
        android:layout_height="fill_parent">
    </ListView>
</LinearLayout>

so the output will be like this way..

Source: here

let me know if you have any trouble regarding this.

Edit: check this useful link: Custom Single choice ListView

这篇关于自定义Listview与CheckBox单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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