如何在android中获取复选框值并存储到字符串数组中
在下面的代码中,通过使用ischecked,我想返回我想返回的复选框值数组 但是如何在creategroup方法中只传递选中的值,而不是检查必须作为参数传递的字符串 GroupList.java如何在android中获取复选框值并存储到字符串数组中,android,Android,在下面的代码中,通过使用ischecked,我想返回我想返回的复选框值数组 但是如何在creategroup方法中只传递选中的值,而不是检查必须作为参数传递的字符串 GroupList.java public class GroupList extends ListActivity { boolean[] checkBoxState; boolean isChecked; String check; ListView users; int positi
public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String check;
ListView users;
int position;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friend = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friend = friends;
}
public int getCount() {
return friend.length;
}
public FriendInfo getItem(int position) {
return friend[position];
}
public long getItemId(int position) {
return 0;
}
@SuppressWarnings("unused")
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friend[position].userName);
holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
final ArrayList<String> checkedFriends = new ArrayList<String>();
checkBoxState = new boolean[friend.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
check=friend[position].userName;
}
}
});
return convertView;
}
public ArrayList<FriendInfo> getCheckedItems(){
ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
if(checkBoxState!=null){
for (int i = 0; i < checkBoxState.length; i++) {
if(checkBoxState[i]){
result.add(getItem(i));
}
}
}
return result;
}
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.group_list_screen);
Button create=(Button)findViewById(R.id.create);
create.setOnClickListener(new OnClickListener() {
@SuppressWarnings({ "unused", "unchecked" })
@Override
public void onClick(View v) {
String groupname = getIntent().getStringExtra("nick");
try {
String result1 = imService.CreateGroup(groupname,imService.getUsername(),check);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();
}
});
friendAdapter = new FriendListAdapter(this);
friendAdapter.getCheckedItems();
}
公共类组列表扩展了ListActivity
{
布尔[]checkBoxState;
布尔值被检查;
字符串检查;
ListView用户;
内部位置;
私有IAppManager imService=null;
私有FriendListAdapter FriendListAdapter;
公共字符串ownusername=新字符串();
私有类FriendListAdapter扩展了BaseAdapter
{
@抑制警告(“未使用”)
类视图持有者{
文本查看文本;
图像视图图标;
复选框1;
}
私人停车场;
私有位图mOnlineIcon;
私人电视台;
private FriendInfo[]friend=null;
公共FriendListAdapter(上下文){
超级();
mInflater=LayoutInflater.from(上下文);
mOnlineIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.greenstar);
mOfflineIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.redstar);
}
public void setFriendList(FriendInfo[]朋友)
{
这个。朋友=朋友;
}
public int getCount(){
返回friend.length;
}
public FriendInfo getItem(内部位置){
归还朋友[职位];
}
公共长getItemId(int位置){
返回0;
}
@抑制警告(“未使用”)
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终持票人;
if(convertView==null)
{
convertView=mInflater.flate(R.layout.grouplist,null);
holder=新的ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
holder.check1=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
}
否则{
holder=(ViewHolder)convertView.getTag();
}
holder.text.setText(友人[position].userName);
holder.icon.setImageBitmap(friend[position].status==status.ONLINE?mOnlineIcon:mOfflineIcon);
final ArrayList checkedFriends=新ArrayList();
checkBoxState=新布尔值[friend.length];
holder.check1.setChecked(checkBoxState[位置]);
holder.check1.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
checkBoxState[位置]=已检查;
如果(已检查){
check=朋友[职位]。用户名;
}
}
});
返回视图;
}
公共ArrayList getCheckedItems(){
ArrayList结果=新建ArrayList();
如果(checkBoxState!=null){
对于(int i=0;i9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
}
setContentView(R.layout.group\u list\u屏幕);
按钮创建=(按钮)findViewById(R.id.create);
create.setOnClickListener(新的OnClickListener(){
@SuppressWarnings({“未使用”、“未选中”})
@凌驾
公共void onClick(视图v){
字符串groupname=getIntent().getStringExtra(“尼克”);
试一试{
字符串result1=imService.CreateGroup(groupname,imService.getUsername(),check);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
Toast.makeText(getApplicationContext(),“成功创建组”,Toast.LENGTH_LONG.show();
}
});
friendAdapter=新的FriendListAdapter(此);
getCheckedItems();
}
您可以将此方法添加到适配器:
public ArrayList<FriendInfo> getCheckedItems(){
ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
if(checkBoxState!=null){
for (int i = 0; i < checkBoxState.length; i++) {
if(checkBoxState[i]){
result.add(getItem(i));
}
}
}
return result;
}
String result1=imService.CreateGroup(groupname,imService.getUsername(),check);而不是检查如何从好友列表中传递唯一选中的值上述方法将仅返回好友列表中的选中值(具有checkBoxState[i]==true的值)。使用此方法获取好友列表(它返回ArrayList)并将其传递给imService.CreateGroup()方法。--请注意,必须在创建.setOnClickListener()friendAdapter.getCheckedItems()之前创建friendAdapter;它将为NPebeCheckboxState[]提供空值,您必须调用getCheckedItems();创建适配器后(friendAdapter=new FriendListAdapter(this))并将您的对象放在上面。阅读我在上面的评论中提到的内容。查看我的代码主要问题是,在朋友中选择两个朋友后,我想使用如下字符串result1=imService.CreateGroup(groupname,imService.getUsername(),check)作为参数传递;而不是check,我想传递字符串数组,即哪个字符串[]我要传递给此的名称。为什么不能在
public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String check;
ListView users;
int position;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friend = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friend = friends;
}
public int getCount() {
return friend.length;
}
public FriendInfo getItem(int position) {
return friend[position];
}
public long getItemId(int position) {
return 0;
}
@SuppressWarnings("unused")
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friend[position].userName);
holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
final ArrayList<String> checkedFriends = new ArrayList<String>();
checkBoxState = new boolean[friend.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
check=friend[position].userName;
}
}
});
return convertView;
}
public ArrayList<FriendInfo> getCheckedItems(){
ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
if(checkBoxState!=null){
for (int i = 0; i < checkBoxState.length; i++) {
if(checkBoxState[i]){
result.add(getItem(i));
}
}
}
return result;
}
}
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.group_list_screen);
Button create=(Button)findViewById(R.id.create);
friendAdapter = new FriendListAdapter(this);
/*
* here you must load list items before calling
* "getCheckedItems()", I think you use "setFriendList(...)" for that
* so you need to do friendAdapter.setFriendList(YOUR_LIST). Otherwise you will have an empty array
* because there is no items, so no checked items too.
*/
friendAdapter.getCheckedItems();
create.setOnClickListener(new OnClickListener() {
@SuppressWarnings({ "unused", "unchecked" })
@Override
public void onClick(View v) {
String groupname = getIntent().getStringExtra("nick");
try {
/*
* you can also call "getCheckedItems()" here,
* to send the checkedItems to "imService"
*/
String result1 = imService.CreateGroup(groupname,imService.getUsername(),check);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();
}
});
}