Android的动态复选框 [英] android checkbox dynamically

查看:198
本文介绍了Android的动态复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复选框添加到的LinearLayout动态。我用下面的code,

 私有类ListAdapters扩展ArrayAdapter<&ApplicationBean中GT; {
私人的ArrayList<&ApplicationBean中GT;项目;
私人诠释的位置;公共ListAdapters(上下文的背景下,INT textViewResourceId,
        ArrayList的<&ApplicationBean中GT; mTitleList){
    超(背景下,textViewResourceId,mTitleList);
    this.items = mTitleList;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    视图V = convertView;
    this.position =位置;
    如果(V == NULL){
        LayoutInflater吹气=(LayoutInflater)mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        V = inflater.inflate(R.layout.applicationlistitem,NULL);
    }    最后ApplicationBean中O =(ApplicationBean中)items.get(位置);    如果(O!= NULL){        txtAppName =(TextView中)v.findViewById(R.id.app_name);
        txtAppName.setText(+ o.getAppName());
        launchButton =(按钮)v.findViewById(R.id.launch_btn);
        launchButton.setTag(位置);
        launchButton.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                最终软件包管理系统下午= mContext.getPackageManager();
                意向LaunchIntent = PM
                        .getLaunchIntentForPackage(项目
                                获得(的Integer.parseInt(v.getTag()
                                        的ToString()))getPname())。
                mContext.startActivity(LaunchIntent);            }
        });        rdgPassFail =(RadioGroup中)v.findViewById(R.id.status_group);
        rdgPassFail.setTag(位置);        单选passBtn =(单选)V
                .findViewById(R.id.pass_btn);
        passBtn.setTag(位置);
        单选failbtn =(单选)V
                .findViewById(R.id.fail_btn);
        failbtn.setTag(位置);        rdgPassFail
                .setOnCheckedChangeListener(新OnCheckedChangeListener(){                    @覆盖
                    公共无效onCheckedChanged(RadioGroup中组,
                            INT checkedId){
                        ApplicationBean中O =(ApplicationBean中)项目
                                获得(的Integer.parseInt(group.getTag()
                                        的ToString()));                        开关(checkedId){
                        案例R.id.fail_btn:
                            Log.e(故障按钮,点击);
                            o.setFailState(真);
                            o.setPassState(假);
                            numOptions = 0;
                            Log.e(故障按钮 - 1,点击);                            打破;
                        案例R.id.pass_btn:
                            Log.e(通过按钮,点击);                            o.setFailState(假);
                            o.setPassState(真);
                            Log.e(通过按钮----- 1,点击);                            打破;
                        }
                        items.set(的Integer.parseInt(group.getTag()
                                的ToString()),O);
                    }                });            Log.i(checkBoxFlag,checkBoxFlag不是真正的+位置);
            的LinearLayout featuresTable =(LinearLayout中)V
                    .findViewById(R.id.failure_reasonslist);
            的for(int i = 0; I< = 5;我++){
                复选框特征1 =新的复选框(this.getContext());
                featuresTable.addView(特征);
                Log.i(内部循环,创造复选框+位置);
            }
            checkBoxFlag =真;        txtDescription =(EditText上)V
                .findViewById(R.id.description_text);
        txtDescription.setTag(位置);
        如果(txtDescription.isFocused()){
            InputMethodManager inputManager =(InputMethodManager)mContext
                    .getSystemService(INPUT_METHOD_SERVICE);
            inputManager.restartInput(txtDescription);
        }        txtDescription
                .setOnFocusChangeListener(新View.OnFocusChangeListener(){                    @覆盖
                    公共无效onFocusChange(视图V,布尔hasFocus){
                        如果(!hasFocus){                            最终的EditText标题=(EditText上)V;
                            o.setDescription(Caption.getText()
                                    的ToString());                        }                    }
                });
        uninstallButton =(按钮)v.findViewById(R.id.uninstall_btn);
        uninstallButton.setTag(位置);
        // uninstallButton.setVisibility(View.INVISIBLE);
        o.setUninstallVisible(假);
        uninstallButton.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                乌里packageUri = Uri.parse(包
                        + items.get(
                                的Integer.parseInt(v.getTag()。的toString()))
                                .getPname());
                意图uninstallIntent =新意图(
                        Intent.ACTION_DELETE,packageUri);
                uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(uninstallIntent);
                mTitleList.remove(items.get((整数)v.getTag()));                mListView.setAdapter(新ListAdapters(mContext,
                        R.id.app_name,mTitleList));
                ((BaseAdapter)mListView.getAdapter())
                        .notifyDataSetChanged();
                isUninstallclicked = TRUE;            }
        });        提交按钮=(按钮)v.findViewById(R.id.submit_btn);
        submitButton.setTag(txtDescription);
        submitButton.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                EditText上电视=(EditText上)v.getTag(); //获取的EditText
                                                        //对象                txtDescription =电视;
                如果(txtTesterName.getText()。的toString()。等于()){
                    的ShowDialog(请输入测试仪的名义,
                            mContext);
                }否则如果(numOptions == 0){
                    的ShowDialog(请选择失败的原因,mContext);
                }否则如果(tv.getText()。的toString()。等于()){
                    的ShowDialog(请输入描述,mContext);
                }否则如果(!isNetworkAvailable()){                    的ShowDialog(
                            无网络connection.Report不会提交
                            mContext);
                }其他{                    如果(!o.isUninstallVisible()){
                        uninstallButton.setVisibility(View.VISIBLE);
                        o.setUninstallVisible(真);
                        mListView.invalidate();
                    }
                    PostRequest P =新PostRequest(的Integer.parseInt(电视
                            。.getTag()的toString()));
                    p.execute();                }
            }        });    }
    返回伏;
}@覆盖
公众诠释getItemViewType(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}@覆盖
公众诠释getViewTypeCount(){
    // TODO自动生成方法存根
    返回items.size();
}}

作为每code,应该为六复选框应该在那里在视图中。但是,当我检查了超过六复选框。请在下面找到日志(来自logcat中复制)。日志也即将到来的两倍。我在列表视图中的一些其他控件。那些在布局的xml限定。但我不能在设计时限制了这些复选框的数量。所以,我想动态加载。有人能帮忙吗?

  10:40:23.163:I /内部循环(440):创建复选框0
10:40:23.223:I /内部循环(440):创建复选框0
10:40:23.223:I /内部循环(440):创建复选框0
10:40:23.273:I /内部循环(440):创建复选框0
10:40:23.273:I /内部循环(440):创建复选框0
10:40:23.303:I /内部循环(440):创建复选框0
10:40:23.393:I / C
10:40:23.393:I /内部循环(440):创建复选框0
10:40:23.423:I /内部循环(440):创建复选框0
10:40:23.423:I /内部循环(440):创建复选框0
10:40:23.503:I /内部循环(440):创建复选框0
10:40:23.503:I /内部循环(440):创建复选框0
10:40:23.553:I /内部循环(440):创建复选框0


解决方案

你必须尝试以下code为创建动态复选框

  INT Array_Count = 0;
的String [] Str_Array;Array_Count = Str_Array.length;的LinearLayout my_layout =(的LinearLayout)findViewById(R.id.my_layout);的for(int i = 0; I< Array_Count;我++)
{
    连续的TableRow =新的TableRow(本);
    row.setId(ⅰ);
    row.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    复选框复选框=新的复选框(本);
    checkBox.setOnCheckedChangeListener(本);
    checkBox.setId(ⅰ);
    checkBox.setText(Str_Array [I]);
    row.addView(复选框);
    my_layout.addView(行);
}

I am trying to add check boxes to a LinearLayout dynamically. I have used the following code,

private class ListAdapters extends ArrayAdapter<ApplicationBean> {
private ArrayList<ApplicationBean> items;
private int position;

public ListAdapters(Context context, int textViewResourceId,
        ArrayList<ApplicationBean> mTitleList) {
    super(context, textViewResourceId, mTitleList);
    this.items = mTitleList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    this.position = position;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.applicationlistitem, null);
    }

    final ApplicationBean o = (ApplicationBean) items.get(position);

    if (o != null) {

        txtAppName = (TextView) v.findViewById(R.id.app_name);
        txtAppName.setText("" + o.getAppName());
        launchButton = (Button) v.findViewById(R.id.launch_btn);
        launchButton.setTag(position);
        launchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                final PackageManager pm = mContext.getPackageManager();
                Intent LaunchIntent = pm
                        .getLaunchIntentForPackage(items
                                .get(Integer.parseInt(v.getTag()
                                        .toString())).getPname());
                mContext.startActivity(LaunchIntent);

            }
        });

        rdgPassFail = (RadioGroup) v.findViewById(R.id.status_group);
        rdgPassFail.setTag(position);

        RadioButton passBtn = (RadioButton) v
                .findViewById(R.id.pass_btn);
        passBtn.setTag(position);
        RadioButton failbtn = (RadioButton) v
                .findViewById(R.id.fail_btn);
        failbtn.setTag(position);

        rdgPassFail
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {
                        ApplicationBean o = (ApplicationBean) items
                                .get(Integer.parseInt(group.getTag()
                                        .toString()));

                        switch (checkedId) {
                        case R.id.fail_btn:
                            Log.e("Fail button", "Clicked");
                            o.setFailState(true);
                            o.setPassState(false);
                            numOptions = 0;
                            Log.e("Fail button--1", "Clicked");

                            break;
                        case R.id.pass_btn:
                            Log.e("Pass button", "Clicked");

                            o.setFailState(false);
                            o.setPassState(true);
                            Log.e("Pass button-----1", "Clicked");

                            break;
                        }
                        items.set(Integer.parseInt(group.getTag()
                                .toString()), o);
                    }

                });

            Log.i("checkBoxFlag", "checkBoxFlag not true " + position);
            LinearLayout featuresTable = (LinearLayout) v
                    .findViewById(R.id.failure_reasonslist);


            for (int i = 0; i <= 5; i++) {
                CheckBox feature1 = new CheckBox(this.getContext());
                featuresTable.addView(feature1);
                Log.i("Inside for loop", "creating check box " + position);
            }
            checkBoxFlag = true;

        txtDescription = (EditText) v
                .findViewById(R.id.description_text);
        txtDescription.setTag(position);
        if (txtDescription.isFocused()) {
            InputMethodManager inputManager = (InputMethodManager) mContext
                    .getSystemService(INPUT_METHOD_SERVICE);
            inputManager.restartInput(txtDescription);
        }

        txtDescription
                .setOnFocusChangeListener(new View.OnFocusChangeListener() {

                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {

                            final EditText Caption = (EditText) v;
                            o.setDescription(Caption.getText()
                                    .toString());

                        }

                    }
                });
        uninstallButton = (Button) v.findViewById(R.id.uninstall_btn);
        uninstallButton.setTag(position);
        // uninstallButton.setVisibility(View.INVISIBLE);
        o.setUninstallVisible(false);
        uninstallButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Uri packageUri = Uri.parse("package:"
                        + items.get(
                                Integer.parseInt(v.getTag().toString()))
                                .getPname());
                Intent uninstallIntent = new Intent(
                        Intent.ACTION_DELETE, packageUri);
                uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(uninstallIntent);
                mTitleList.remove(items.get((Integer) v.getTag()));

                mListView.setAdapter(new ListAdapters(mContext,
                        R.id.app_name, mTitleList));
                ((BaseAdapter) mListView.getAdapter())
                        .notifyDataSetChanged();
                isUninstallclicked = true;

            }
        });

        submitButton = (Button) v.findViewById(R.id.submit_btn);
        submitButton.setTag(txtDescription);
        submitButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText tv = (EditText) v.getTag(); // get edittext
                                                        // object

                txtDescription = tv;
                if (txtTesterName.getText().toString().equals("")) {
                    showDialog("Please enter the name of tester",
                            mContext);
                } else if (numOptions == 0) {
                    showDialog("Please select failure reason", mContext);
                } else if (tv.getText().toString().equals("")) {
                    showDialog("Please enter the description", mContext);
                } else if (!isNetworkAvailable()) {

                    showDialog(
                            "No network connection.Report won't be submitted",
                            mContext);
                } else {

                    if (!o.isUninstallVisible()) {
                        uninstallButton.setVisibility(View.VISIBLE);
                        o.setUninstallVisible(true);
                        mListView.invalidate();
                    }
                    PostRequest p = new PostRequest(Integer.parseInt(tv
                            .getTag().toString()));
                    p.execute();

                }
            }

        });

    }
    return v;
}

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

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return items.size();
}

}

As per the code, it should be six checkboxes should be there in the view. But when I checked its more than six check boxes. Please find the log (copied from logcat) below. The log is also coming twice. I have some other controls in the list view. Those are defined in the layout xml. But I cannot limit the number of these checkboxes during the time of design. So that I am trying to load dynamically. Could somebody help?

10:40:23.163: I/Inside for loop(440): creating check box 0
10:40:23.223: I/Inside for loop(440): creating check box 0
10:40:23.223: I/Inside for loop(440): creating check box 0
10:40:23.273: I/Inside for loop(440): creating check box 0
10:40:23.273: I/Inside for loop(440): creating check box 0
10:40:23.303: I/Inside for loop(440): creating check box 0
10:40:23.393: I/c
10:40:23.393: I/Inside for loop(440): creating check box 0
10:40:23.423: I/Inside for loop(440): creating check box 0
10:40:23.423: I/Inside for loop(440): creating check box 0
10:40:23.503: I/Inside for loop(440): creating check box 0
10:40:23.503: I/Inside for loop(440): creating check box 0
10:40:23.553: I/Inside for loop(440): creating check box 0

解决方案

you have to try following code for create dynamic checkbox

int Array_Count=0;
String[] Str_Array;

Array_Count=Str_Array.length;

LinearLayout my_layout = (LinearLayout)findViewById(R.id.my_layout);

for (int i = 0; i < Array_Count; i++) 
{
    TableRow row =new TableRow(this);
    row.setId(i);
    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    CheckBox checkBox = new CheckBox(this);
    checkBox.setOnCheckedChangeListener(this);
    checkBox.setId(i);
    checkBox.setText(Str_Array[i]);
    row.addView(checkBox);  
    my_layout.addView(row);
}

这篇关于Android的动态复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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