计算勾选的总复选框并执行计算(Android Java)

Calculate total checkboxes ticked and perform calculation (Android Java)

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

这是 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>

提前致谢!

在您想获取选中项目总数并执行计算的地方使用此代码

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