2

I have a textView which is a DropDownList. Under this textview, there will be multiple options with checkboxes beside it. The User can choose one or more from the options.

enter image description here

I include my code for this dropdownlist and how did I populate it with my array.

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);
}

I also have this line of code in order for me to save the data...

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

My question is, how can I save those multiple data in a single column in SQLite?

1
  • Very unclear question, you mean how do you access the SQLite database? or do you mean what is the query that you need? or do you mean what type of a column should it be? Commented Jul 8, 2013 at 8:43

1 Answer 1

2

first, you need to get the value of the selected item on your multi select spinner. Try this:

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();

I toast the arraylist content just to check if it really contains the value I select. Then when you see the right value on your toast then you could save it on your database. Hope it helps! charot :D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.