2015-01-09 88 views
-1

我有使用共享首选项动态创建的复选框。每个复选框的标签存储在array.xml中。如何计算勾选的复选框总数并将总数存储在变量中,并进一步用于其他计算 - (total/totalCheckboxes)* 100?计算勾选的总复选框并执行计算(Android Java)

这里的Java类的一个片段: -

public class CheckBoxSharedPreferences extends Activity { 
    ListView myList; 
    Button getChoice, clearAll, button1; 
    SharedPreferences sharedpreferences; 
    public static final String MyPREFERENCES = "MyUserChoice"; 
    ArrayList<String> selectedItems = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     myList = (ListView) findViewById(R.id.list); 
     getChoice = (Button) findViewById(R.id.getchoice); 
     clearAll = (Button) findViewById(R.id.clearall); 
     button1 = (Button) findViewById(R.id.button1); 


     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, 
       // code snippet to retrieve array values in ArrayAdapter 
       getResources().getStringArray(R.array.Questionnaire)); 
     myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     myList.setAdapter(adapter); 
     sharedpreferences = getSharedPreferences(MyPREFERENCES, 
       Context.MODE_PRIVATE); 
     if (sharedpreferences.contains(MyPREFERENCES)) { 
      LoadSelections(); 
     } 

     getChoice.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String selected = ""; 
       int cntChoice = myList.getCount(); 

       SparseBooleanArray sparseBooleanArray = myList 
         .getCheckedItemPositions(); 
       for (int i = 0; i < cntChoice; i++) { 
        if (sparseBooleanArray.get(i)) { 
         selected += myList.getItemAtPosition(i).toString() 
           + "\n"; 
         System.out.println("Checking list while adding:" 
           + myList.getItemAtPosition(i).toString()); 
         SaveSelections(); 
        } 

       } 

       Toast.makeText(CheckBoxSharedPreferences.this, selected, 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 

     //listener for clear all button 
     clearAll.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       ClearSelections(); 
      } 
     }); 

     //listener for button1 (that transfers the activity to another intent 
     button1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(CheckBoxSharedPreferences.this, 
         result.class); 
       startActivity(intent); 
      } 
     }); 
    } 

    private void SaveSelections() { 
     // save the selections in the shared preference in private mode for the 
     // user 

     SharedPreferences.Editor prefEditor = sharedpreferences.edit(); 
     String savedItems = getSavedItems(); 
     prefEditor.putString(MyPREFERENCES.toString(), savedItems); 
     prefEditor.commit(); 
    } 

    private String getSavedItems() { 
     String savedItems = ""; 
     int count = this.myList.getAdapter().getCount(); 
     for (int i = 0; i < count; i++) { 
      if (this.myList.isItemChecked(i)) { 
       if (savedItems.length() > 0) { 
        savedItems += "," + this.myList.getItemAtPosition(i); 
       } else { 
        savedItems += this.myList.getItemAtPosition(i); 
       } 
      } 
     } 
     return savedItems; 
    } 

    private void LoadSelections() { 
     // if the selections were previously saved load them 

     if (sharedpreferences.contains(MyPREFERENCES.toString())) { 

      String savedItems = sharedpreferences.getString(
        MyPREFERENCES.toString(), ""); 
      selectedItems.addAll(Arrays.asList(savedItems.split(","))); 

      int count = this.myList.getAdapter().getCount(); 

      for (int i = 0; i < count; i++) { 
       String currentItem = (String) myList.getAdapter().getItem(i); 
       if (selectedItems.contains(currentItem)) { 
        myList.setItemChecked(i, true); 
        Toast.makeText(getApplicationContext(), 
          "Current Item: " + currentItem, Toast.LENGTH_LONG) 
          .show(); 
       } else { 
        myList.setItemChecked(i, false); 
       } 

      } 
     } 
    } 

和array.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string-array name="Questionnaire"> 

     <item>abc</item> 
     <item>jkm</item> 
     <item>xyz</item> 
     <item>abc2</item> 
     <item>jkm2</item> 
     <item>xyz2</item> 
     <item>abc3</item> 
     <item>jkm3</item> 
     <item>xyz3</item> 

</resources> 

提前感谢!

+0

这个片段是无用的,请在其中创建复选框添加代码。 – Rohit5k2

+0

@ Rohit5k2添加了代码。 – antsemot

+0

请看我的anser – Rohit5k2

回答

0

使用此代码,你想要得到检查的项目总数和进行计算

int checkedCount = myList.getCheckedItemCount(); 
int totalCount = myList.getAdapter().getCount(); 
int calculatedValue = (totalCount/checkedCount) * 100; 
+0

谢谢@ Rohit5k2。我已经编辑了一些代码,计算似乎工作正常,但它没有给我输出,我需要。我会为此打开另一个问题。 – antsemot

+0

当然。那样就好了。 – Rohit5k2