按钮onClick上的Android增量计数器值

时间:2015-09-14 05:29:11

标签: android auto-increment

我想将数据库中的列表保存为List1,List2,-----,List(n + 1)。它正在保存我的列表,但每次保存列表时它都从List1开始。我想通过选择最后保存的列表来保存我的列表并将其递增1.如何执行此操作?

代码:

btn_save_list.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
                ++counter;
                showSavelistDialog(counter);

        }
    });
private void showSavelistDialog(int counter) {
if (!list.isEmpty()) {

        String draftName=  "Draft"+counter;
        SaleDraftDatabase saleDraftDatabase = new SaleDraftDatabase(
                mActivity);
        saleDraftDatabase.open();

        boolean nameAvailable = saleDraftDatabase
                .checkDraftNameAvailable(userId,draftName);
        if (nameAvailable) {
            boolean saveCheck = saleDraftDatabase.createEntry(list,
                    userId,draftName);
            saleDraftDatabase.close();
            if (saveCheck) {
                SalesReturnDatabase saleDatabase = new SalesReturnDatabase(
                        mActivity);
                saleDatabase.open();
                saleDatabase.deleteAllEntriesOfSingleUser(userId);
                saleDatabase.close();
                list.clear();
                barcodeList.clear();
                Intent i = new Intent(mActivity,
                        Activity_Sales_Return.class);
                i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                finish();
                Toast.makeText(
                        Activity_Sales_Return.this,
                        draftName
                                + " sale saved as draft.",
                        Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(Activity_Sales_Return.this,
                        "Some error occured", Toast.LENGTH_SHORT)
                        .show();
        } else {

            Toast.makeText(mActivity,
                    "Name already in use. Choose another name.",
                    Toast.LENGTH_SHORT).show();
        }
    }

1 个答案:

答案 0 :(得分:4)

使用静态counter值。

private static int counter = 0;

使所有对象的增量相同。

或者,将计数器值保存在SharedPreference