0

这个问题有点类似于我的共享偏好问题,只是它涉及到 android 数据库。我想在他第一次启动应用程序时将一个人的工资存储在数据库中。他第二次启动它时,他存储的工资将在下一个活动中以 textview 的形式查看。

我大致知道如何跳过工资的提示,但不确定数据库。我的问题是,我如何从数据库中读取工资不为空并将其显示在下一个活动中?我知道它以某种方式涉及一个 if else 语句,例如>>(如果薪水/数据库不为空,则开始意图以 textview 进行下一个活动),但不确定如何对其进行编码。

(SavingsGuiderMenuActivity 包含一个指向 SavingsGuiderAppActivity 的链接,该链接询问薪水(目标),然后将显示到 SavingsGuiderInserActivity 活动。意味着用户只能访问一次 SavingsGuiderAppActivity,下次启动它时,从 SavingsGuiderMenuActivity 将直接转到 SavingsGuiderInsertActivity )

以下是我的代码 SavingsGuiderMenuActivity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    Bundle bundle = getIntent().getExtras();
    String name= bundle.getString("name");

TextView resultView = (TextView)findViewById(R.id.view_Name);

    resultView.setText("Welcome " + name);

    ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
    String[] items = { getResources().getString(R.string.start),
            getResources().getString(R.string.about),
            getResources().getString(R.string.help) };
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
    menuList.setAdapter(adapt);
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
            // Note: if the list was built "by hand" the id could be used.
            // As-is, though, each item has the same id
            TextView textView = (TextView) itemClicked;
            String strText = textView.getText().toString();
            if (strText.equalsIgnoreCase(getResources().getString(R.string.start))) {
                if(!dataExists())
                {
                    startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAppActivity.class));
                }
                 else
                 {
                    Intent intent = new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderInsertActivity.class);
                    Bundle extras = new Bundle();
                    extras.putDouble("target",double_target);
                    extras.putInt("interval",int_interval);
                    intent.putExtras(extras);
                    startActivity(intent);                 
                 }
               SavingsGuiderMenuActivity.this.finish();
           }                
            else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) {
                // Launch the Help Activity
                startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderHelpActivity.class));
            } 

            else if (strText.equalsIgnoreCase(getResources().getString(R.string.about))) {
                // Launch the Settings Activity
                startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAboutActivity.class));
            } 
        }//onitem click
    });   
            }//bundle

public boolean dataExists()
{

    DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());       
    Cursor cursor = null;

        dbAdaptor.open();       
        cursor = dbAdaptor.getAllSavings();

            if(cursor.moveToFirst())
            {
                double_target = cursor.getDouble(1);
                int_interval = cursor.getInt(2);
                return true;
            }
            else
            {
                return false;
            }

}

以下是我的代码 SavingsGuiderAppActivity:

public class SavingsGuiderAppActivity extends SavingsActivity {
/** Called when the activity is first created. */

String tag = "Savings Guider";
EditText targetEdit, intervalEdit;
Button insertButton = null;


protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.app);
    targetEdit=(EditText)findViewById(R.id.edit_target);
    intervalEdit=(EditText)findViewById(R.id.edit_interval);
    insertButton = (Button) findViewById(R.id.btn_submit);

    insertButton.setOnClickListener(new OnClickListener(){

        public void onClick(View v)
        {
            DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
            try{
                dbAdaptor.open();

                String target = targetEdit.getText().toString();                    
                String interval = intervalEdit.getText().toString();

                if(!target.equals("") && !interval.equals("")) {

                    try{

                    double target2 = Double.parseDouble(target);                    
                    int interval2 = Integer.parseInt(interval);
                    dbAdaptor.insertSavings(target2, interval2);
                    }
                    catch (NumberFormatException e) {
                        Context context = getApplicationContext();
                        CharSequence text = "Please enter valid value!";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }
                }                   

                else {
                    Context context = getApplicationContext();
                    CharSequence text = "Please do not leave any field blank!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }
            catch(Exception e){
                Log.d("Savings Guider: ",e.getMessage());
            }
            finally{
                if(dbAdaptor != null)
                    dbAdaptor.close();
            }
        }
    });
}
}

下面是我的 SavingsGuiderInsertActivity 代码。我觉得从 theSavingsGuiderAppActivity 页面显示我在数据库中插入的数据有问题。就像我不应该使用 Bundle Extras 一样:

public class SavingsGuiderInsertActivity extends SavingsActivity {
/** Called when the activity is first created. */

String tag = "Savings Guider";
DecimalFormat df = new DecimalFormat("#.##");

protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.insert);

    Bundle bundle = getIntent().getExtras();
    double target= bundle.getDouble("target");
    int interval= bundle.getInt("interval");

TextView targetView = (TextView)findViewById(R.id.view_target); TextView intervalView = (TextView)findViewById(R.id.view_interval);

    targetView.setText("$" + target);
    intervalView.setText("" + interval);

    double average = target/interval;

    TextView averageView = (TextView)findViewById(R.id.viewAverage);
    averageView.setText("You must save $" + df.format(average) + " everytime!");

}
4

1 回答 1

0

你可以这样做。

  1. 在第一次启动期间创建一个数据库表,但该表将为空。

  2. 检查表是否为空,什么也不做。否则从数据库中获取数据并显示它们。

您可以使用SQLiteOpenHelper管理 Android 数据库。

为了检查表是否为空,您可以执行以下操作

  public boolean isSalaryTableNotEmpty() {
      String sql = "SELECT COUNT(*) FROM SALARY_TABLE";
      Cursor cursor = database.rawQuery(sql, null);
      if(!cursor.moveToFirst()) {
          return false;
      }
      int count = cursor.getInt(0);
      cursor.close();
      return count > 0;
  }
于 2012-07-28T03:20:23.060 回答