本文介绍了TimePicker保存零值在SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想用它来确定的时间长度,用户可以保持连接其中TimePicker。比方说,现在的时间是上午10点,如果用户选择11:00 - 我想下面的源$ C ​​$ C,以确定有当前时间在60分钟 - 和选择的时间,并设置为一个字符串然后我已显示为一个TextView /长(分钟)。

我有codeD的一切,我认为这应该是 - 但是TextView的似乎永远不会有分钟数进行更新。每次我试图查看数据 - 我得到一个0值都无所谓什么timepicker设置为

任何人有什么建议?我此刻难倒,我不知道什么尝试。

ADDEDITDEVICE.JAVA(其中timepicker和分钟决心发生)

 公共类AddEditDevice延伸活动{     私人长期ROWID;
     私人的EditText nameEt;
     私人的EditText卡佩;
     私人的EditText codeET;
     私人TimePicker timeEt;
     私人TextView的ssidTextView;
     日期日期=新的日期();
     TimePicker TP;
      // @覆盖
       公共无效的onCreate(捆绑savedInstanceState)
       {
          super.onCreate(savedInstanceState);
          的setContentView(R.layout.add_country);
          WifiManager无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
          WifiInfo信息= wifi.getConnectionInfo();
          串ssidString = info.getSSID();          如果(ssidString.startsWith(\\)及与放大器; ssidString.endsWith(\\)){
              ssidString = ssidString.substring(1,ssidString.length() - 1);
          // TextView的ssidTextView =(的TextView)findViewById(R.id.wifiSSID);
          ssidTextView =(的TextView)findViewById(R.id.wifiSSID);
          ssidTextView.setText(ssidString);          nameEt =(EditText上)findViewById(R.id.nameEdit);
          卡佩=(EditText上)findViewById(R.id.capEdit);
          codeET =(EditText上)findViewById(R.id. codeEdit的);
          timeEt =(TimePicker)findViewById(R.id.timeEdit);
          捆绑额外= getIntent()getExtras()。          如果(临时演员!= NULL)
          {
             ROWID = extras.getLong(ROW_ID);
             nameEt.setText(extras.getString(名字));
             capEt.setText(extras.getString(上限));
             codeEt.setText(extras.getString(code));
             字符串时间= extras.getString(时间);
             的String [] =零件time.split(:);
             timeEt.setCurrentHour(Integer.valueOf(部件[0]));
             timeEt.setCurrentMinute(Integer.valueOf(地区[1]));
             timeEt.setIs24HourView(假);             date.setMinutes(tp.getCurrentMinute());
             date.setHours(tp.getCurrentHour());
             Long.toString(分钟);
          }          按钮saveButton =(按钮)findViewById(R.id.saveBtn);
          saveButton.setOnClickListener(新OnClickListener(){              公共无效的onClick(视图v)
              {
                 如果(nameEt.getText()。长度()!= 0)
                 {
                    AsyncTask的<对象,对象,对象> saveContactTask =
                       新的AsyncTask<对象,对象,对象>()
                       {
                          @覆盖
                          保护对象doInBackground(对象... PARAMS)
                          {
                             saveContact();
                             返回null;
                          }                          @覆盖
                          保护无效onPostExecute(对象结果)
                          {
                             完();
                          }
                       };                    saveContactTask.execute((Object []对象),NULL);
                 }                 其他
                 {
                    AlertDialog.Builder警报=新AlertDialog.Builder(AddEditDevice.this);
                    alert.setTitle(R.string.errorTitle);
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton,NULL);
                    alert.show();
                 }
              }
         });}
       }       长期分钟=((新的Date())的getTime() - date.getTime())/(1000 * 60);
           私人无效saveContact()
           {
              DatabaseConnector dbConnector =新DatabaseConnector(本);              如果(getIntent()。getExtras()== NULL)
              {
// Log.i(测试空,+ dbConnector ++ nameEt ++卡佩++ timeEt ++ codeET ++ ssidTextView);
                  dbConnector.insertContact(nameEt.getText()。的toString()
                          capEt.getText()。的toString()
                          。timeEt.getCurrentHour()的toString()+:
                              + timeEt.getCurrentMinute()。的toString()
                          codeEt.getText()的toString()
                          Long.toString(分),
                       。ssidTextView.getText()的toString());              }
              其他
              {
                 dbConnector.updateContact(ROWID,
                         nameEt.getText()。的toString()
                            capEt.getText()。的toString()
                              。timeEt.getCurrentHour()的toString()+:
                                  + timeEt.getCurrentMinute()。的toString()
                              codeEt.getText()的toString()
                              Long.toString(分),
                              。ssidTextView.getText()的toString());
              }
           }
}

查看COUNTRY.JAVA(其中分钟的数据由timepicker设置应该是可见的)

 公共类ViewCountry扩展NfcBeamWriterActivity {    私有静态最后弦乐TAG = ViewCountry.class.getName();    受保护的消息消息;    NfcAdapter mNfcAdapter;
    私有静态最终诠释MESSAGE_SENT = 1;
    私人长期ROWID;
    私人TextView的nameTv;
    私人TextView的capTv;
    私人TextView的codeTV;
    私人TextView的timeTv;
    私人TextView的ssidTv;
    私人TextView的结合;
    私人的TextView分钟;
    //字符串timetest =300;
    //一个字符串=\\;
    //字符串B =\\;    //字符串MESSAGE1 = A + ssidTv.getText()。的toString()+,+
    // capTv.getText()的toString()+ B。    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.view_country);
    共享preferences preFS = GET preferences(Context.MODE_PRIVATE);
        共享preferences.Editor编辑器= prefs.edit();
        editor.putBoolean(名,真正的);
        editor.putBoolean(上限,真正的);
        editor.putBoolean(code,真正的);
        editor.putBoolean(时代,真正的);
        editor.putBoolean(SSID,真正的);
        editor.putBoolean(分钟,真正的);
        editor.putBoolean(timetest,真正的);
        editor.commit();        setDetecting(真);
        startPushing();        setUpViews();
        捆绑额外= getIntent()getExtras()。
        ROWID = extras.getLong(CountryList.ROW_ID);
    }    私人无效setUpViews(){
        nameTv =(的TextView)findViewById(R.id.nameText);
        capTv =(的TextView)findViewById(R.id.capText);
        timeTv =(的TextView)findViewById(R.id.timeEdit);
        codeTV =(的TextView)findViewById(R.id. codeTEXT);
        ssidTv =(的TextView)findViewById(R.id.wifiSSID);
        分钟=(的TextView)findViewById(R.id.Minutes);
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        新LoadContacts()执行(ROWID)。
    }    私有类LoadContacts扩展的AsyncTask<长,对象,光标> {
        DatabaseConnector dbConnector =新DatabaseConnector(ViewCountry.this);        @覆盖
        保护光标doInBackground(长... PARAMS){
            dbConnector.open();
            返回dbConnector.getOneContact(PARAMS [0]);
        }        @覆盖
        保护无效onPostExecute(光标结果){
            super.onPostExecute(结果);            result.moveToFirst();
            INT nameIndex = result.getColumnIndex(名称);
            INT capIndex = result.getColumnIndex(上限);
            INT codeINDEX = result.getColumnIndex(code);
            INT timeIndex = result.getColumnIndex(时间);
            INT ssidIndex = result.getColumnIndex(SSID);            nameTv.setText(result.getString(nameIndex));
            capTv.setText(result.getString(capIndex));
            timeTv.setText(result.getString(timeIndex));
            codeTv.setText(result.getString(codeINDEX));
            ssidTv.setText(result.getString(ssidIndex));            result.close();
            dbConnector.close();
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        super.onCreateOptionsMenu(菜单);
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.view_country_menu,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例R.id.editItem:
            意图addEditContact =新意图(这一点,AddEditDevice.class);            // addEditContact.putExtra(CountryList.ROW_ID,ROWID);
            // addEditContact.putExtra(名,nameTv.getText());
            // addEditContact.putExtra(上限,capTv.getText());
            // addEditContact.putExtra(code,codeTv.getText());
            startActivity(addEditContact);
            返回true;        案例R.id.user1SettingsSave:
            意向点击=新意图(这一点,Tap.class);
            startActivity(TAP);
            返回true;        案例R.id.deleteItem:
            deleteContact();
            返回true;        默认:
            返回super.onOptionsItemSelected(项目);
        }
    }    私人无效deleteContact(){        AlertDialog.Builder警报=新AlertDialog.Builder(ViewCountry.this);        alert.setTitle(R.string.confirmTitle);
        alert.setMessage(R.string.confirmMessage);        alert.setPositiveButton(R.string.delete_btn,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释按钮){
                        最后DatabaseConnector dbConnector =新DatabaseConnector(
                                ViewCountry.this);                        AsyncTask的<长,对象,对象> DeleteTask活动=新的AsyncTask<长,对象,对象>(){
                            @覆盖
                            保护对象doInBackground(长... PARAMS){
                                dbConnector.deleteContact(PARAMS [0]);
                                返回null;
                            }                            @覆盖
                            保护无效onPostExecute(对象结果){
                                完();
                            }
                        };                        deleteTask.execute(新长征[] {ROWID});
                    }
                });        alert.setNegativeButton(R.string.cancel_btn,NULL).show();
    }}


解决方案

  dbConnector.insertContact(nameEt.getText()。的toString()
capEt.getText()。的toString()
。timeEt.getCurrentHour()的toString()+:+ timeEt.getCurrentMinute()的toString(),
codeEt.getText()的toString()
分钟,
Long.toString(分),
。ssidTextView.getText()的toString());

您有code添加联系人,但在你的数据库连接器高你有:

 公共无效insertContact(字符串名称,
串帽,
字符串code,
字符串的时候,
长期分钟,
字符串的SSID,
字符串字符串){

我认为不匹配,所以这就是为什么不正确地插入。
顺便说一句,因为我需要50的声誉,我不能在此刻发表评论。

问候

I have a TimePicker which I'd like to use to determine a length of time a user can stay connected. Lets say the time now is 10:00 if the user selects 11:00 - I'd like the source code below to determine that there are 60 minutes between the current time - and the time selected and set that to a string/long (minutes) which I then have displayed as a textview.

I've coded everything as I thought it should be - however the textview never seems to update with minutes value. Everytime I attempt to view the data - I get a value of 0 not matter what the timepicker is set to.

Anyone have any suggestions? I'm stumped at the moment and I'm not sure what else to try.

ADDEDITDEVICE.JAVA (where the timepicker and minutes determination takes place)

public class AddEditDevice extends Activity {

     private long rowID;
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;
     private TextView ssidTextView;
     Date date = new Date();
     TimePicker tp;




      // @Override
       public void onCreate(Bundle savedInstanceState)
       {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add_country);
          WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          WifiInfo info = wifi.getConnectionInfo();
          String ssidString = info.getSSID();

          if (ssidString.startsWith("\"") && ssidString.endsWith("\"")){
              ssidString = ssidString.substring(1, ssidString.length()-1);


          //TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID);
          ssidTextView = (TextView) findViewById(R.id.wifiSSID);
          ssidTextView.setText(ssidString);

          nameEt = (EditText) findViewById(R.id.nameEdit);
          capEt = (EditText) findViewById(R.id.capEdit);
          codeEt = (EditText) findViewById(R.id.codeEdit);
          timeEt = (TimePicker) findViewById(R.id.timeEdit);






          Bundle extras = getIntent().getExtras();

          if (extras != null)
          {
             rowID = extras.getLong("row_id");
             nameEt.setText(extras.getString("name"));
             capEt.setText(extras.getString("cap"));
             codeEt.setText(extras.getString("code"));
             String time = extras.getString("time");
             String[] parts = time.split(":");
             timeEt.setCurrentHour(Integer.valueOf(parts[0]));
             timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
             timeEt.setIs24HourView(false);

             date.setMinutes(tp.getCurrentMinute());
             date.setHours(tp.getCurrentHour());
             Long.toString(minutes);


          }

          Button saveButton =(Button) findViewById(R.id.saveBtn);
          saveButton.setOnClickListener(new OnClickListener() {

              public void onClick(View v)
              {
                 if (nameEt.getText().length() != 0)
                 {
                    AsyncTask<Object, Object, Object> saveContactTask =
                       new AsyncTask<Object, Object, Object>()
                       {
                          @Override
                          protected Object doInBackground(Object... params)
                          {
                             saveContact();
                             return null;
                          }

                          @Override
                          protected void onPostExecute(Object result)
                          {
                             finish();
                          }
                       };

                    saveContactTask.execute((Object[]) null);
                 }

                 else
                 {
                    AlertDialog.Builder alert = new AlertDialog.Builder(AddEditDevice.this);
                    alert.setTitle(R.string.errorTitle);
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton, null);
                    alert.show();
                 }
              }
         });}
       }

       long minutes = ((new Date()).getTime() - date.getTime()) / (1000 * 60);


           private void saveContact()
           {
              DatabaseConnector dbConnector = new DatabaseConnector(this);

              if (getIntent().getExtras() == null)
              {
//                Log.i("Test for Null", ""+dbConnector+" "+nameEt+" "+capEt+" "+timeEt+" "+codeEt+" "+ssidTextView);
                  dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString(),
                          Long.toString(minutes),
                       ssidTextView.getText().toString());

              }
              else
              {
                 dbConnector.updateContact(rowID,
                         nameEt.getText().toString(),
                            capEt.getText().toString(),
                              timeEt.getCurrentHour().toString() + ":"
                                  + timeEt.getCurrentMinute().toString(),
                              codeEt.getText().toString(),
                              Long.toString(minutes),
                              ssidTextView.getText().toString());
              }
           }
}

VIEW COUNTRY.JAVA (where the minutes data set by the timepicker should be visible)

public class ViewCountry extends NfcBeamWriterActivity {

    private static final String TAG = ViewCountry.class.getName();

    protected Message message;

    NfcAdapter mNfcAdapter;
    private static final int MESSAGE_SENT = 1;
    private long rowID;
    private TextView nameTv;
    private TextView capTv;
    private TextView codeTv;
    private TextView timeTv;
    private TextView ssidTv;
    private TextView combined;
    private TextView minutes;
    //String timetest = "300";


    // String a="\"";
    // String b="\"";

    // String message1 = a + ssidTv.getText().toString() +"," +
    // capTv.getText().toString()+b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_country);


    SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putBoolean("name", true);
        editor.putBoolean("cap", true);
        editor.putBoolean("code", true);
        editor.putBoolean("time", true);
        editor.putBoolean("ssid",true);
        editor.putBoolean("minutes",true);
        editor.putBoolean("timetest",true);
        editor.commit();



        setDetecting(true);
        startPushing();

        setUpViews();
        Bundle extras = getIntent().getExtras();
        rowID = extras.getLong(CountryList.ROW_ID);
    }

    private void setUpViews() {
        nameTv = (TextView) findViewById(R.id.nameText);
        capTv = (TextView) findViewById(R.id.capText);
        timeTv = (TextView) findViewById(R.id.timeEdit);
        codeTv = (TextView) findViewById(R.id.codeText);
        ssidTv = (TextView) findViewById(R.id.wifiSSID);
        minutes = (TextView) findViewById(R.id.Minutes);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new LoadContacts().execute(rowID);
    }

    private class LoadContacts extends AsyncTask<Long, Object, Cursor> {
        DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);

        @Override
        protected Cursor doInBackground(Long... params) {
            dbConnector.open();
            return dbConnector.getOneContact(params[0]);
        }

        @Override
        protected void onPostExecute(Cursor result) {
            super.onPostExecute(result);

            result.moveToFirst();
            int nameIndex = result.getColumnIndex("name");
            int capIndex = result.getColumnIndex("cap");
            int codeIndex = result.getColumnIndex("code");
            int timeIndex = result.getColumnIndex("time");
            int ssidIndex = result.getColumnIndex("ssid");

            nameTv.setText(result.getString(nameIndex));
            capTv.setText(result.getString(capIndex));
            timeTv.setText(result.getString(timeIndex));
            codeTv.setText(result.getString(codeIndex));
            ssidTv.setText(result.getString(ssidIndex));

            result.close();
            dbConnector.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.view_country_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.editItem:
            Intent addEditContact = new Intent(this, AddEditDevice.class);

            // addEditContact.putExtra(CountryList.ROW_ID, rowID);
            // addEditContact.putExtra("name", nameTv.getText());
            // addEditContact.putExtra("cap", capTv.getText());
            // addEditContact.putExtra("code", codeTv.getText());
            startActivity(addEditContact);
            return true;

        case R.id.user1SettingsSave:
            Intent Tap = new Intent(this, Tap.class);
            startActivity(Tap);
            return true;

        case R.id.deleteItem:
            deleteContact();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void deleteContact() {

        AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);

        alert.setTitle(R.string.confirmTitle);
        alert.setMessage(R.string.confirmMessage);

        alert.setPositiveButton(R.string.delete_btn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int button) {
                        final DatabaseConnector dbConnector = new DatabaseConnector(
                                ViewCountry.this);

                        AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {
                            @Override
                            protected Object doInBackground(Long... params) {
                                dbConnector.deleteContact(params[0]);
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Object result) {
                                finish();
                            }
                        };

                        deleteTask.execute(new Long[] { rowID });
                    }
                });

        alert.setNegativeButton(R.string.cancel_btn, null).show();
    }

}
解决方案
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString() + ":" + timeEt.getCurrentMinute().toString(),
codeEt.getText().toString(),
minutes,
Long.toString(minutes),
ssidTextView.getText().toString());

You have that code to add a contact, BUT in your database conector you have:

public void insertContact(String name,
String cap,
String code,
String time,
long minutes,
String ssid,
String string){

I think that don't match, so this is why don't insert correctly.BTW i can't comment at the moment because i need 50 reputation.

Regards

这篇关于TimePicker保存零值在SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:16