2013-07-09 19 views
0

我正在使用一個baseadapter用於我的自定義微調控件,其複選框允許用戶選擇多個值。我的應用程序中有一個更新按鈕,我需要在複選框中將數據庫中的值設置爲true。我的問題是我不知道該怎麼做。例如,我在我的微調中有[「A」,「B」,「C」,「D」]值,在我的數據庫中有B和D.當我打開活動時,如何自動檢查這些值。自動檢查與數據庫中的值對應的複選框

這裏是我的代碼填充我定製微調

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() { 
     @Override 
     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() { 
     @Override 
     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); 
} 

這裏是我的ConsumerSegmentListAdapter。列表視圖充當我的微調。

public class ConsumerSegmentListAdapter extends BaseAdapter { 
    private ArrayList<String> mListCustomerSegment; 
    private LayoutInflater mInflater; 
    private TextView mSelectedItems; 
    private static int selectedCount = 0; 
    private static String firstSelected = ""; 
    private ViewHolder holder; 
    private static String selected = ""; //shortened selected values representation 

    public static String getSelected() { 
     return selected; 
    } 

    public void setSelected(String selected) { 
     ConsumerSegmentListAdapter.selected = selected; 
    } 

    public ConsumerSegmentListAdapter(Context context, ArrayList<String> customerSegment, 
      TextView tv) { 
     mListCustomerSegment = new ArrayList<String>(); 
     mListCustomerSegment.addAll(customerSegment); 
     mInflater = LayoutInflater.from(context); 
     mSelectedItems = tv; 
    } 

    @Override 
    public int getCount() { 
     return mListCustomerSegment.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.drop_down_customersegment, null); 
      holder = new ViewHolder(); 
      holder.tv = (TextView) convertView.findViewById(R.DropDownList.SelectOptionCustomerSegment); 
      holder.chkbox = (CheckBox) convertView.findViewById(R.DropDownList.checkboxCustomerSegment); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.tv.setText(mListCustomerSegment.get(position)); 

     final int position1 = position; 

     //whenever the checkbox is clicked the selected values textview is updated with new selected values 
     holder.chkbox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       setText(position1); 
      } 
     }); 

     if(S_10th_IReportMain.checkSelectedConsumerSegment[position]) 
      holder.chkbox.setChecked(true); 
     else 
      holder.chkbox.setChecked(false);  
     return convertView; 
    } 


    /* 
    * Function which updates the selected values display and information(checkSelectedConsumerSegment[]) 
    * */ 
    private void setText(int position1){ 
     if (!S_10th_IReportMain.checkSelectedConsumerSegment[position1]) { 
      S_10th_IReportMain.checkSelectedConsumerSegment[position1] = true; 
      selectedCount++; 
     } else { 
      S_10th_IReportMain.checkSelectedConsumerSegment[position1] = false; 
      selectedCount--; 
     } 

     if (selectedCount == 0) { 
      mSelectedItems.setText(R.string.select_consumersegment); 
     } else if (selectedCount == 1) { 
      for (int i = 0; i < S_10th_IReportMain.checkSelectedConsumerSegment.length; i++) { 
       if (S_10th_IReportMain.checkSelectedConsumerSegment[i] == true) { 
        firstSelected = mListCustomerSegment.get(i); 
        break; 
       } 
      } 
      mSelectedItems.setText(firstSelected); 
      setSelected(firstSelected); 
     } else if (selectedCount > 1) { 
      for (int i = 0; i < S_10th_IReportMain.checkSelectedConsumerSegment.length; i++) { 
       if (S_10th_IReportMain.checkSelectedConsumerSegment[i] == true) { 
        firstSelected = mListCustomerSegment.get(i); 
        break; 
       } 
      } 
      mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more"); 
      setSelected(firstSelected + " & "+ (selectedCount - 1) + " more"); 
     } 
    } 

    private class ViewHolder { 
     TextView tv; 
     CheckBox chkbox; 
    } 
} 
+0

微調器在哪裏(我只能在代碼中觀察ListView)?另外,你可以分享ConsumerSegmentListAdapter嗎? – sandrstar

+0

我已更新我的帖子。 – lolliloop

+0

當前您只使用databaseHandler.setItemOnConsumerSeg()調用從數據庫獲取數據。假設你會從中獲得[「B」,「D」]。哪裏[「A」,「B」,「C」,「D」]可以來自哪裏? – sandrstar

回答

1

我認爲你需要管理另一個存儲選擇/未選定的項目,並將其移動到適配器。例如。它可以是HashSet<String>。然後,代碼看起來如下(注意,是我把它編譯的,因爲這是不可能的編譯的問題提供了一個):

public class S_10th_IReportMain extends Activity { 

    boolean expandedConsumerSegment; 
    ConsumerSegmentListAdapter mAdapter; 

    private static class DatabaseHandler { 

     List<String> setItemOnConsumerSeg() { 
      return Collections.emptyList(); 
     } 
    } 

    private static class BrandListAdapter { 

     static String getSelected() { 
      return "string"; 
     } 
    } 

    DatabaseHandler databaseHandler = new DatabaseHandler(); 

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

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

      @Override 
      public void onClick(View v) { 
       if(!expandedConsumerSegment) { 
        // display all selected values 
        String selected = ""; 
        int flag = 0; 
        for (String segment : consumerSegments) { 
         if (mAdapter.isChecked(segment)) { 
          selected += segment; 
          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.id.tv_ConsumerSegment); 
     tv_customerSegment.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       initiatePopUpCustomerSegment(consumerSegments,tv_ConsumerSegment); 
      } 
     }); 
    } 

    PopupWindow pwConsumerSegment; 

    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, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.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(ViewGroup.LayoutParams.WRAP_CONTENT); 

     //dismiss the pop-up i.e. drop-down when touched anywhere outside the pop-up 
     pwConsumerSegment.setTouchInterceptor(new View.OnTouchListener() { 
      @Override 
      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.id.dropDownCustomerSegment); 
     ConsumerSegmentListAdapter adapter = new ConsumerSegmentListAdapter(this, customerSegments, tv_CustomerSegment); 
     listCustomerSegment.setAdapter(adapter); 
    } 

    public static class ConsumerSegmentListAdapter extends BaseAdapter { 
     private ArrayList<String> mListCustomerSegment; 
     private LayoutInflater mInflater; 
     private TextView mSelectedItems; 
     private static int selectedCount = 0; 
     private static String firstSelected = ""; 
     private ViewHolder holder; 
     private static String selected = ""; //shortened selected values representation 

     private HashSet<String> mCheckedItems; 

     public static String getSelected() { 
      return selected; 
     } 

     public void setSelected(String selected) { 
      ConsumerSegmentListAdapter.selected = selected; 
     } 

     public ConsumerSegmentListAdapter(Context context, ArrayList<String> customerSegment, 
              TextView tv) { 
      mListCustomerSegment = new ArrayList<String>(); 
      mListCustomerSegment.addAll(customerSegment); 
      mInflater = LayoutInflater.from(context); 
      mSelectedItems = tv; 
     } 

     /** 
     * Should be called then new data obtained from DB 
     * 
     * @param checkedItems array of strings obtained from DB 
     */ 
     public void setCheckedItems(final String[] checkedItems) { 
      mCheckedItems.clear(); 

      Collections.addAll(mCheckedItems, checkedItems); 

      notifyDataSetChanged(); 
     } 

     @Override 
     public int getCount() { 
      return mListCustomerSegment.size(); 
     } 

     @Override 
     public Object getItem(int arg0) { 
      return null; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      return 0; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.drop_down_customersegment, null); 
       holder = new ViewHolder(); 
       holder.tv = (TextView) convertView.findViewById(R.id.SelectOptionCustomerSegment); 
       holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkboxCustomerSegment); 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      final String text = mListCustomerSegment.get(position); 
      final boolean checked = isChecked(text); 

      holder.tv.setText(mListCustomerSegment.get(position)); 

      //whenever the checkbox is clicked the selected values textview is updated with new selected values 
      holder.chkbox.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        setText(position, checked, text); 
       } 
      }); 

      if(checked) { 
       holder.chkbox.setChecked(true); 
      } else { 
       holder.chkbox.setChecked(false); 
      } 

      return convertView; 
     } 

     /* 
     * Function which updates the selected values display and information(checkSelectedConsumerSegment[]) 
     * */ 
     private void setText(int position, boolean checked, String text){ 
      if (!checked) { 
       mCheckedItems.add(text); 
       selectedCount++; 
      } else { 
       mCheckedItems.remove(text); 
       selectedCount--; 
      } 

      if (selectedCount == 0) { 
       mSelectedItems.setText(R.string.select_consumersegment); 
      } else if (selectedCount == 1) { 
       firstSelected = mCheckedItems.iterator().next(); 
       mSelectedItems.setText(firstSelected); 
       setSelected(firstSelected); 
      } else if (selectedCount > 1) { 
       firstSelected = mCheckedItems.iterator().next(); 
       mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more"); 
       setSelected(firstSelected + " & "+ (selectedCount - 1) + " more"); 
      } 
     } 

     /** 
     * @param segment to be checked 
     * 
     * @return true if the segment is checked 
     */ 
     public boolean isChecked(final String segment) { 
      return mCheckedItems.contains(segment); 
     } 

     private class ViewHolder { 
      TextView tv; 
      CheckBox chkbox; 
     } 
    } 
} 

,那麼你獲得數據庫中的新數據,應檢查你可以只致電mAdapter.setCheckedItems()

0

我已經做到了。通過這樣的:

for (int j=0; j<brands.size(); j++) 
    { 
     for(String chosenElement : Brands) 
     { 
      int index = brands.indexOf(chosenElement); 
      checkSelected[index] = true; 

     } 
     } 

我所做的是我去找我選擇的ArrayList到我的飛旋的適配器的索引,並設置該複選框指數爲真。那很簡單。無論如何感謝這個想法。

+0

它有兩個循環,所以性能不會很好。 – sandrstar

相關問題