所以这个列表以这种方式工作,当我单击该项目时,该项目被删除。所以我想要它当我单击该项目时,它必须向我显示 TimeDialog(TimePicker)来更改日期和时间。
mList = (ListView) findViewById(id.list);
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mCursor.moveToPosition(position);
String rowId = mCursor.getString(0); //Column 0 of the cursor is the id
mDb.delete(MyDbHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
mCursor.requery();
mAdapter.notifyDataSetChanged();
*这是我的 MainActivity,我认为这会有所帮助!!还包括 Listview!*
class MyDbHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "TaskTbl";
public static final String COL_TXT = "Text";
public static final String COL_DATE = "Date";
public static final String COL_TIME = "Time";
private static final String STRING_CREATE = "CREATE TABLE "+TABLE_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+COL_TXT+" VARCHAR(250), "+COL_DATE+" VARCHAR(20), "+COL_TIME+" VARCHAR(20));";
public MyDbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(STRING_CREATE);
ContentValues cv = new ContentValues(2);
cv.put(COL_TXT, "New Entry");
db.insert(TABLE_NAME, null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
public class MainActivity extends ListActivity {
public static final int RESULT_CODE = 1;
MyDbHelper mHelper;
SQLiteDatabase mDb;
Cursor mCursor;
SimpleCursorAdapter mAdapter;
Intent weather;
Intent i;
String text;
String date;
String time;
ListView mList;
Intent nTaskScreen ;
public TextView timeText;
public TextView dateText;
public EditText taskText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_main);
timeText = (TextView) findViewById(R.id.timeTxt);
dateText = (TextView) findViewById(R.id.dateTxt);
taskText = (EditText) findViewById(R.id.editText1);
mHelper = new MyDbHelper(getApplicationContext());
Button showBtn = (Button) findViewById(R.id.shTask);
showBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
Button nTaskBtn = (Button) findViewById(R.id.nTask);
nTaskBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nTaskScreen = new Intent(MainActivity.this, new_task.class);
startActivityForResult(nTaskScreen, RESULT_CODE);
}
});
Button wthrBtn = (Button) findViewById(R.id.wthrBtn);
registerForContextMenu(wthrBtn);
mList = (ListView) findViewById(id.list);
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int hourOfDay = 0, minute = 0;
TimePickerDialog tp = new TimePickerDialog(MainActivity.this,
mTimeSetListener, hourOfDay, minute, false);
tp.show();
};
TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
timeText.setText("" + hourOfDay + "" + minute);
};
};
});
}
public void onResume() {
super.onResume();
mDb = mHelper.getWritableDatabase();
String[] columns = new String[] {"_id", MyDbHelper.COL_TXT, MyDbHelper.COL_TIME};
mCursor = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null, null, null, null, null);
String[] headers = new String[] {MyDbHelper.COL_TXT, MyDbHelper.COL_TIME};
mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
mList.setAdapter(mAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_CODE)
{
String text = data.getStringExtra("text");
String date = data.getStringExtra("date");
String time = data.getStringExtra("time");
Toast.makeText(this, "Registration was successfull! \nTask: " +text + "\nOn: " + date + "\nat:" + time, Toast.LENGTH_LONG).show();
ContentValues cv = new ContentValues(2);
cv.put(MyDbHelper.COL_TXT, text.toString());
//cv.put(MyDbHelper.COL_DATE, date); //Insert 'now' as the date
cv.put(MyDbHelper.COL_TIME, time);
mDb.insert(MyDbHelper.TABLE_NAME, null, cv);
mCursor.requery();
mAdapter.notifyDataSetChanged();
text = null;
//mDb.execSQL("INSERT INTO TaskTbl (text,time,date) VALUES(" + "'" + text + "', '" + time + "', '" + date + "');");
}
super.onActivityResult(requestCode, resultCode, data);
}
}