使用android系统中复选框阵列 [英] Using Checkbox array in android

查看:114
本文介绍了使用android系统中复选框阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让列表视图复选框 使用ListView控件内置的复选框方法。我走过了计算器后不见了,我发现它除了是一个问题,运行正常。

如果有在列表中四个项目和假设,我检查第二和第三项,onclicking,则显示所述第二和第三项作为needed..but如果我选择第一,然后第三然后第二项,并然后即时取消选中第一个,所以我必须保持与第二和第三为所需的输出。但它正在提供第一,第二和第三项作为输出。

任何人可以指导我这句话..?

这是java code:

 公共类TailoredtwoActivity扩展活动实现OnItemClickListener,OnClickListener {    按钮BTN1;
    ListView的mListView;
    String []数组=新的String [] {火腿,土耳其,面包};    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_tailoredtwo);        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_checked,数组);        mListView =(ListView控件)findViewById(R.id.listViewcity);
        mListView.setAdapter(适配器);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);        Button按钮=(按钮)findViewById(R.id.btn_tailortwo_submit);
        button.setOnClickListener(本);
    }    公共无效的onClick(查看视图){
        SparseBooleanArray位置= mListView.getCheckedItemPositions();
        INT大小= positions.size();
        对于(INT指数= 0;指数 - LT;大小;指数++){
            Toast.makeText(getApplicationContext(),数组[positions.keyAt(指数)的toString(),Toast.LENGTH_LONG。).show();
        }
    }
    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
        // TODO自动生成方法存根    }
}


解决方案

更改的onClick

Delcare以下为一类变量

  StringBuilder的建设者;

然后

 公共无效的onClick(查看视图){
    SparseBooleanArray位置= mListView.getCheckedItemPositions();
    建设者=新的StringBuilder();
    对于(INT指数= 0;指数 - LT; array.length;指数++){
         如果(positions.get(指数)==真)
         {
             builder.append(数组[指数]);
             builder.append(\\ n);
         }    }
    Toast.makeText(getApplicationContext(),建设者,Toast.LENGTH_LONG).show();
}

I am trying to make a listview with checkbox using listview's built in checkbox method. I have gone through a stackoverflow post and i found it is running properly except one problem.

If there are four items in list and assume, i checked the second and third item, onclicking, it is displaying the second and third item as needed..but if i am selecting first and then third and then second item, and then i m unchecking the first, so i must be left with second and third as desired output. But it is providing first second and third item as output.

can anybody guide me on that..?

This is the java code:

public class TailoredtwoActivity extends Activity implements OnItemClickListener, OnClickListener{

    Button btn1;
    ListView mListView;
    String[] array = new String[] {"Ham", "Turkey", "Bread"};

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);

        mListView = (ListView) findViewById(R.id.listViewcity);
        mListView.setAdapter(adapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        Button button = (Button) findViewById(R.id.btn_tailortwo_submit);
        button.setOnClickListener(this);
    }

    public void onClick(View view) {
        SparseBooleanArray positions = mListView.getCheckedItemPositions();
        int size = positions.size();
        for(int index = 0; index < size; index++) {
            Toast.makeText(getApplicationContext(), array[positions.keyAt(index)].toString(), Toast.LENGTH_LONG).show();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }
}

解决方案

Change your onClick to

Delcare the below as a class variable

StringBuilder builder;

Then

public void onClick(View view) {
    SparseBooleanArray positions = mListView.getCheckedItemPositions();
    builder = new StringBuilder();
    for(int index = 0; index <array.length; index++) {
         if(positions.get(index)==true)
         {
             builder.append(array[index]);
             builder.append("\n");
         }

    }
    Toast.makeText(getApplicationContext(),builder, Toast.LENGTH_LONG).show();
}

这篇关于使用android系统中复选框阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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