gpt4 book ai didi

java - 如何从 TimePicker 创建两个时间值(currentHour 和 currentMinute),然后将它们连接在一起

转载 作者:行者123 更新时间:2023-12-01 14:44:27 25 4
gpt4 key购买 nike

我的源代码中有两个值 timeEt (稍后我会将其重命名为 hourEt)和 minEt,我想将它们组合在一起形成一个可以在 xml 布局中显示的字符串 - 我已经能够编译下面的代码,但我遇到了困难,我需要一些帮助,我想做的就是添加一点代码将 currentHour 和 currentMinute 组合成一个字符串,这样我就可以稍后在 xml 布局中从 TimePicker 调用时间)

AddEditCountry.java

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;

public class AddEditCountry extends Activity {

private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TimePicker minEt;
public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";

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

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


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"));
timeEt.setCurrentHour(extras.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);
minEt.setCurrentMinute(extras.getInt("min"));
}

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(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}


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

if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
minEt.getCurrentMinute().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
minEt.getCurrentMinute().toString(),/* Storing as String*/
codeEt.getText().toString());
}
}
}

更新了数据库连接器 (.java),并提供了 Bill Motes 的有用建议

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;



public class DatabaseConnector {

public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}

public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}

public void close()
{
if (database != null)
database.close();
}

public void insertContact(String name, String cap, String code, String time, String min)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("min", min);
newCon.put("code", code);
newCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
newCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);

open();
database.insert("country", null, newCon);
close();
}


public void updateContact(long id, String name, String cap,String code, String time, String min)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("min", min);
editCon.put("code", code);
editCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
editCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}


public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}

public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}

public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}

添加 Bill Mote 的有用建议后,它仍然崩溃。我更新了下面的 logcat 和上面的源代码。我觉得我更接近于集成一个字符串,该字符串从 TimePicker 调用组合的 currentHour 和 currentMinute 按钮 - 但我仍然陷入困境(非常欢迎来自任何 StackOverflow 成员的任何输入!)

日志:

03-24 00:48:57.779: D/Activity(6809): Activity.onPause(), editTextTapSensorList size: 0
03-24 00:48:57.959: I/Adreno200-EGLSUB(6809): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-24 00:48:57.959: D/memalloc(6809): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:54
03-24 00:48:58.049: D/memalloc(6809): ion: Mapped buffer base:0x5dc22000 size:614400 offset:0 fd:75
03-24 00:48:58.079: D/memalloc(6809): ion: Mapped buffer base:0x5dcb8000 size:614400 offset:0 fd:78
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5d4ce000 size:614400
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5d6f7000 size:614400
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5c929000 size:614400
03-24 00:49:10.623: W/dalvikvm(6809): threadid=12: thread exiting with uncaught exception (group=0x410889d8)
03-24 00:49:10.643: E/AndroidRuntime(6809): FATAL EXCEPTION: AsyncTask #2
03-24 00:49:10.643: E/AndroidRuntime(6809): java.lang.RuntimeException: An error occured while executing doInBackground()
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.lang.Thread.run(Thread.java:856)
03-24 00:49:10.643: E/AndroidRuntime(6809): Caused by: java.lang.NullPointerException
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry.saveContact(AddEditCountry.java:101)
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry.access$1(AddEditCountry.java:92)
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry$1$1.doInBackground(AddEditCountry.java:65)
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-24 00:49:10.643: E/AndroidRuntime(6809): ... 5 more
03-24 00:49:10.663: D/Activity(6809): Activity.onPause(), editTextTapSensorList size: 0
03-24 00:49:10.693: I/Adreno200-EGLSUB(6809): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-24 00:49:10.693: D/memalloc(6809): ion: Mapped buffer base:0x5d4cb000 size:614400 offset:0 fd:57
03-24 00:49:10.713: D/memalloc(6809): ion: Mapped buffer base:0x5d8f7000 size:614400 offset:0 fd:68
03-24 00:49:10.753: D/memalloc(6809): ion: Mapped buffer base:0x5e14e000 size:614400 offset:0 fd:72
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5ca41000 size:614400
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5dc22000 size:614400
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5dcb8000 size:614400

附注

谢谢大家!

最佳答案

在第一个示例中,您得到的是“time”+“min”,实际上是“timemin”,但在第二个示例中,您得到的是“time”。

所以,有几件事......

public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";

在调用 putExtra 的类中,使用 AddEditCountry.KEY_BUNDLE_TIME 作为键值对中的键。对于 AddEditCountry.KEY_BUNDLE_MIN 也是如此。这可以让被调用的 Activity/fragment 控制在包中传递的条目,并确保您不会创建拼写错误等。

此外,您可以在执行 get ...之前检查以确保 key 存在

if (extras.containsKey(KEY_BUNDLE_TIME) {
timeEt.setCurrentHour(extras.getInt(KEY_BUNDLE_TIME));
} else {
timeEt.setCurrentHour(0);
}

或者

timeEt.setCurrentHour(extras.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);

关于java - 如何从 TimePicker 创建两个时间值(currentHour 和 currentMinute),然后将它们连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com