我有一个连接到数据库的android ListView应用程序。我希望有人能够编辑他们点击的listView。当他们长时间点击时,它会为他们带来两个不同的活动,包括文本视图和保存按钮。我想要的是textview有正在编辑的项目的文本。
例如:
Intent b = new Intent(MainActivity.this, Test.class);
b.putExtra(Rd, String.valueOf(id));
获取数据库行标识并将其变为字符串^
// Puts Id Of Last Clicked List View Item Into String//
public final static String Rd = DBAdapter.KEY_ROWID;
取字符串并使其成为全局^
// Gets Id Of Last Clicked List View Item//
passedVar = getIntent().getStringExtra(MainActivity.Rd);
// Links Variable etTasks To Physical Edit Text In Layout//
etTasks = (EditText) findViewById(R.id.editTextTask);
etTasks.setText(passedVar);
然后允许在编辑文本^
中查看数字或ROW_ID我想做同样的事情,但是列表视图项中的文本内容不是数据库的行id。请帮助谢谢。
这是我的其余代码。
将对DBAdapter:
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging
database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_TASK = "task";
public static final String KEY_DATE = "date";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_TASK, KEY_DATE};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_TASK = 1;
public static final int COL_DATE = 2;
// DataBase info:
public static final String DATABASE_NAME = "dbToDo";
public static final String DATABASE_TABLE = "mainToDo";
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_TASK + " TEXT NOT NULL, "
+ KEY_DATE + " TEXT"
+ ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertRow(String task, String date) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TASK, task);
initialValues.put(KEY_DATE, date);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(String rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
public boolean updateRow(String rowId, String task, String date) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_TASK, task);
newValues.put(KEY_DATE, date);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
}
主要活动:
public class MainActivity extends ActionBarActivity implements
.OnClickListener {
// Connect To Database MyDb//
DBAdapter myDb;
// Defines Variable ListView MyList//
ListView mylist;
// Defines Variable ListView Adapter//
SimpleCursorAdapter myCursorAdapter;
// Defines Variable Database Cursor//
Cursor cursor;
// Defines Variable Connected To Database String//
String[] fromFieldNames;
// Defines Variable That Takes String From Database//
int[] toViewIDs;
// Puts Id Of Last Clicked List View Item Into String//
public final static String Rd = DBAdapter.KEY_ROWID;
// Puts Task Of Last Clicked List View Item Into String//
public final static String Sd = DBAdapter.KEY_TASK;
// What Happens When Activity Starts//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Links Variable MyList To Physical ListView In Layout//
mylist = (ListView) findViewById(R.id.listView);
// Links Variable Button2 to Physical Button In Layout//
Button option = (Button) findViewById(R.id.button2);
option.setOnClickListener(this);
// Controls What Happens When ListView Is Long Clicked//
mylist.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
Intent b = new Intent(MainActivity.this, Test.class);
b.putExtra(Rd, String.valueOf(id));
b.putExtra(Sd, String.valueOf(toViewIDs));
startActivity(b);
return true;
}
});
// Opens Database//
openDB();
// Shows Whats Stored In Database In A List View//
populateListView();
}
// What Happens When Said Button Is Clicked//
@Override
public void onClick(View v) {
switch (v.getId()) {
// What Happens When Button Two Is Clicked//
case R.id.button2:
// Open Activity Test//
Intent i = new Intent(this, Test.class);
startActivity(i);
break;
}
}
// Method To Open Database//
private void openDB() {
// Links Variable myDb To Database//
myDb = new DBAdapter(this);
// Opens Database//
myDb.open();
}
// Method To Populate ListViews From Database//
private void populateListView() {
// Gets All Rows Added To Database//
cursor = myDb.getAllRows();
// Puts Rows Stored On Database Into A String (DBAdapter.KEY_ROWID = Numbers Each Row In ListView, KEY_TASK = Information Shown By You//
fromFieldNames = new String[]{DBAdapter.KEY_ROWID, DBAdapter.KEY_TASK,};
// Takes String From Database And Sends It To Whatever Layout Widget You Want. Will Show Up In The Order String Is Made In//
toViewIDs = new int[]{R.id.textViewItemNumber, R.id.textViewItemTask};
// Creates ListView Adapter Which Allows ListView Items To Be Seen//
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);
// Sets Up Adapter Made Earlier / Shows Content From Database//
mylist.setAdapter(myCursorAdapter);
}
}
的Test.class:
public class Test extends ActionBarActivity {
// Sets Item In ListView To The Current Date It Mas Made//
Time today = new Time(Time.getCurrentTimezone());
// Connect To Database//
DBAdapter myDb;
// Defines Variable EditText//
public static EditText etTasks;
public static TextView passedView;
String passedVar = null;
String passedVar2 = null;
// Defines String Variable For Date In Update Task//
String date;
// Defines String Variable For Task In Update Task//
String task;
// Defines Variable Database Cursor//
Cursor cursor;
// What Happens When Activity Starts//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Gets Id Of Last Clicked List View Item//
passedVar = getIntent().getStringExtra(MainActivity.Rd);
// Gets Id Of Last Clicked List View Item//
passedVar2 = getIntent().getStringExtra(MainActivity.Sd);
// Links Variable etTasks To Physical Edit Text In Layout//
etTasks = (EditText) findViewById(R.id.editTextTask);
etTasks.setText(passedVar2);
//Opens DataBase//
openDB();
}
// Method To Open Database//
private void openDB() {
// Links Variable myDb To Database//
myDb = new DBAdapter(this);
// Opens Database//
myDb.open();
}
// What Happens When Add Button Is Clicked//
public void onClick_AddTask(View v) {
// Set New Database Entry To Current Time//
today.setToNow();
// Sets Time In Format Understandable To Humans//
String timestamp = today.format("%y-%m-%d %H:%M:%S");
// Takes Whats In Edit Text And Turns It Into A String//
if (!TextUtils.isEmpty(etTasks.getText().toString())) {
// Inserts It Into Database//
myDb.insertRow(etTasks.getText().toString(), timestamp);
}
// Sets PassedView To Null//
passedView = null;
// Starts Options Activity//
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
// What Happens When Add Button Is Clicked//
public void editListView(View v) {
updateTask(DEFAULT_KEYS_DIALER);
}
// Method That Updates Task//
private void updateTask(long id) {
// Gets Selected Row//
cursor = myDb.getRow(passedVar);
// Where New Updated Content Comes From//
task = etTasks.getText().toString();
// Set New Database Entry To Current Time//
today.setToNow();
// Sets Time In Format Understandable To Humans//
date = today.format("%y-%m-%d %H:%M:%S");
// Updates Specific Row In Database//
myDb.updateRow(passedVar, task, date);
// Closes Cursor//
cursor.close();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}