2

我有一个文本视图,它是一个 DropDownList。在此文本视图下,将有多个选项旁边带有复选框。用户可以从选项中选择一个或多个。

在此处输入图像描述

我包含了这个下拉列表的代码,以及我是如何用我的数组填充它的。

MainActivity.java

private void initializeCustomerSegment()
{
    final ArrayList<String> consumerSegments = new ArrayList<String>();
    List<String> consumerSegment = databaseHandler.setItemOnConsumerSeg();
    consumerSegments.addAll(consumerSegment);

    checkSelectedConsumerSegment = new boolean[consumerSegments.size()];
    //initialize all values of list to 'unselected' initially
    for (int i = 0; i < checkSelectedConsumerSegment.length; i++) {
        checkSelectedConsumerSegment[i] = false;
    } 


    final TextView tv_ConsumerSegment = (TextView) findViewById(R.DropDownList.tv_ConsumerSegment);
    tv_ConsumerSegment.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(!expandedConsumerSegment){
                //display all selected values
            String selected = "";
            int flag = 0;
            for (int i = 0; i < consumerSegments.size(); i++) {
                if (checkSelectedConsumerSegment[i] == true) {
                     selected += consumerSegments.get(i);
                     selected += ", ";
                    flag = 1;
                }
            }
            if(flag==1)
                tv_ConsumerSegment.setText(selected);
            expandedConsumerSegment =true;
            }
            else{
                //display shortened representation of selected values
                tv_ConsumerSegment.setText(BrandListAdapter.getSelected());
                expandedConsumerSegment = false;
            }
        }
    });

     //onClickListener to initiate the dropDown list
    TextView tv_customerSegment = (TextView)findViewById(R.DropDownList.tv_ConsumerSegment);
    tv_customerSegment.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            initiatePopUpCustomerSegment(consumerSegments,tv_ConsumerSegment);
        }
    });
}

private void initiatePopUpCustomerSegment(ArrayList<String> customerSegments, TextView tv_CustomerSegment){
    LayoutInflater inflater = (LayoutInflater)S_10th_IReportMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //get the pop-up window i.e.  drop-down layout
    LinearLayout layoutCustomerSegment = (LinearLayout)inflater.inflate(R.layout.pop_up_window_customersegment, (ViewGroup)findViewById(R.id.PopUpView1));

    //get the view to which drop-down layout is to be anchored
    RelativeLayout layout4 = (RelativeLayout)findViewById(R.id.relativeLayout4);
    pwConsumerSegment = new PopupWindow(layoutCustomerSegment, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

    //Pop-up window background cannot be null if we want the pop-up to listen touch events outside its window
    pwConsumerSegment.setBackgroundDrawable(new BitmapDrawable());
    pwConsumerSegment.setTouchable(true);

    //let pop-up be informed about touch events outside its window. This  should be done before setting the content of pop-up
    pwConsumerSegment.setOutsideTouchable(true);
    pwConsumerSegment.setHeight(LayoutParams.WRAP_CONTENT);

    //dismiss the pop-up i.e. drop-down when touched anywhere outside the pop-up
    pwConsumerSegment.setTouchInterceptor(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                pwConsumerSegment.dismiss();
                return true;                    
            }
            return false;
        }
    });

    //provide the source layout for drop-down
    pwConsumerSegment.setContentView(layoutCustomerSegment);

    //anchor the drop-down to bottom-left corner of 'layout1'
    pwConsumerSegment.showAsDropDown(layout4);

    //populate the drop-down list
    final ListView listCustomerSegment = (ListView) layoutCustomerSegment.findViewById(R.DropDownList.dropDownCustomerSegment);
    ConsumerSegmentListAdapter adapter = new ConsumerSegmentListAdapter(this, customerSegments, tv_CustomerSegment);
    listCustomerSegment.setAdapter(adapter);
}

我也有这行代码,以便我保存数据......

String cSegment = checkSelected.toString();
Cursor rcSegment = databaseHandler.getReport_SubBrandCode(subBrand);
String SubBrandCode = rcSegment.getString(rcSegment.getColumnIndex(Constants.CONSUMERSEGMENT_CODE));

我的问题是,如何将这些多个数据保存在 SQLite 的单个列中?

4

1 回答 1

2

首先,您需要在多选微调器上获取所选项目的值。试试这个:

ArrayList<String> content = new ArrayList<String>();

                for (int j = 0; j < checkSelected.length; j++) 
                {
                    if(checkSelected[j]==true)
                    {
                        String values = BrandListAdapter.mListItems.get(j);
                        content.add(values);
                    }

                }

                Toast.makeText(getApplicationContext(), content.toString(), Toast.LENGTH_SHORT).show();

我敬酒 arraylist 内容只是为了检查它是否真的包含我选择的值。然后,当您在吐司上看到正确的值时,您可以将其保存在数据库中。希望能帮助到你!战车:D

于 2013-07-08T08:45:33.850 回答