2013-07-08 185 views
2

我有一個TextView,它是一個DropDownList。在這個textview下面,會有多個選項,旁邊有複選框。用戶可以從選項中選擇一個或多個。如何在Android中的SQLite數據庫中保存多個值?

enter image description here

我包括我的這個下拉列表的代碼和我怎麼會跟我的陣列來填充它。

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中單個列中的多個數據?

+0

很不清楚的問題,你的意思是你如何訪問SQLite數據庫?或者你的意思是你需要什麼查詢?或者你的意思是應該是什麼類型的列? – LuckyMe

回答

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

我爲了檢查數組是否真的包含了我選擇的值,然後當你在你的麪包上看到正確的值時,你可以將它保存在你的數據庫中。希望能幫助到你! charot:D

相關問題