0

ユーザーが接続を維持できる時間を決定するために使用したい TimePicker があります。ユーザーが 11:00 を選択した場合、現在の時刻が 10:00 であるとしましょう。以下のソース コードで、現在の時刻と選択された時刻の間に 60 分があることを確認し、それを文字列/ロング (分)をテキストビューとして表示しました。

あるべきだと思ったようにすべてをコーディングしましたが、テキストビューは分の値で更新されないようです。データを表示しようとするたびに、タイムピッカーの設定に関係なく、値が 0 になります。

誰にも提案はありますか?私は現時点で困惑しており、他に何を試すべきかわかりません。

ADDEDITDEVICE.JAVA (タイムピッカーと分の決定が行われる場所)

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 (タイムピッカーによって設定された分のデータが表示される場所)

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();
    }

}
4

2 に答える 2