Android 使动态列表视图可单击以更改活动
我的android应用程序当前使用sqlite表的内容填充ListView(在MainActivity中)。我希望能够单击其中一个已创建的ListView项目,并使其将活动更改为我的EditNote活动,同时将与该ListView相关的数据库记录传递到EditNote中,并填充EditText My Main活动在加载时填充ListView:Android 使动态列表视图可单击以更改活动,android,sqlite,android-listview,Android,Sqlite,Android Listview,我的android应用程序当前使用sqlite表的内容填充ListView(在MainActivity中)。我希望能够单击其中一个已创建的ListView项目,并使其将活动更改为我的EditNote活动,同时将与该ListView相关的数据库记录传递到EditNote中,并填充EditText My Main活动在加载时填充ListView: public class MainActivity extends ListActivity{ DatabaseHelper dbh;
public class MainActivity extends ListActivity{
DatabaseHelper dbh;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbh = new DatabaseHelper(this);
dbh.open();
adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
ArrayList<String[]> searchResult = new ArrayList<String[]>();
//EditText searchTitle = (EditText) findViewById(R.id.searchC);
listItems.clear();
searchResult = dbh.fetchNotes("");
//searchResult = dbh.fetchNotes(searchTitle.getText().toString());
String title = "", note = "";
for (int count = 0 ; count < searchResult.size() ; count++) {
note = searchResult.get(count)[1];
title = searchResult.get(count)[0];
listItems.add(title);
}
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void addNote(View v){
Intent newActivity = new Intent (this, AddingNote.class);
startActivity(newActivity);
finish();
}
}
public类MainActivity扩展了ListActivity{
数据库辅助胸径;
ArrayList listItems=新的ArrayList();
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbh=新数据库助手(此);
胸径开放();
adapter=new ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,listItems);
setListAdapter(适配器);
ArrayList searchResult=新建ArrayList();
//EditText searchTitle=(EditText)findViewById(R.id.searchC);
listItems.clear();
searchResult=dbh.fetchNotes(“”);
//searchResult=dbh.fetchNotes(searchTitle.getText().toString());
字符串标题=”,注释=”;
对于(int count=0;count
用于创建表和select语句的My database类:
public class DatabaseHelper {
private static final String DATABASE_NAME = "noteDatabase";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "note";
private OpenHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context dbContext;
private static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"title TEXT NOT NULL, " +
"note TEXT NOT NULL); ";
public DatabaseHelper(Context ctx) {
this.dbContext = ctx;
}
public DatabaseHelper open() throws SQLException {
mDbHelper = new OpenHelper(dbContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public boolean createNote(String title, String note) {
ContentValues initialValues = new ContentValues();
initialValues.put("title", title);
initialValues.put("note", note);
return mDb.insert(TABLE_NAME, null, initialValues) > 0;
}
public boolean updateNote(long rowId, String title, String note) {
ContentValues args = new ContentValues();
args.put("title", title);
args.put("note", note);
return mDb.update(TABLE_NAME, args, "_id=" + rowId, null) > 0;
}
public void deleteAll() {
mDb.delete(TABLE_NAME, null, null);
}
public void deleteRecord(long rowID) {
mDb.delete(TABLE_NAME, "_rowId=" + rowID, null);
}
public ArrayList<String[]> fetchNotes(String title) throws SQLException {
ArrayList<String[]> myArray = new ArrayList<String[]>();
int pointer = 0;
Cursor mCursor = mDb.query(TABLE_NAME, new String[] {"_id", "title",
"note"}, null, null,
null, null, "_id");
int titleColumn = mCursor.getColumnIndex("title");
int noteColumn = mCursor.getColumnIndex("note");
if (mCursor != null){
if (mCursor.moveToFirst()){
do {
myArray.add(new String[3]);
myArray.get(pointer)[0] = mCursor.getString(titleColumn);
myArray.get(pointer)[1] = mCursor.getString(noteColumn);
//increment our pointer variable.
pointer++;
} while (mCursor.moveToNext()); // If possible move to the next record
} else {
myArray.add(new String[3]);
myArray.get(pointer)[0] = "NO RESULTS";
myArray.get(pointer)[1] = "";
}
}
return myArray;
}
public ArrayList<String[]> selectAll() {
ArrayList<String[]> results = new ArrayList<String[]>();
int counter = 0;
Cursor cursor = this.mDb.query(TABLE_NAME, new String[] { "id", "forename", "surname", "age" }, null, null, null, null, "surname desc");
if (cursor.moveToFirst()) {
do {
results.add(new String[3]);
results.get(counter)[0] = cursor.getString(0).toString();
results.get(counter)[1] = cursor.getString(1).toString();
results.get(counter)[2] = cursor.getString(2).toString();
results.get(counter)[3] = cursor.getString(3).toString();
counter++;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return results;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
公共类数据库助手{
私有静态最终字符串数据库\u NAME=“noteDatabase”;
私有静态最终int数据库_VERSION=1;
私有静态最终字符串表\u NAME=“note”;
私人OpenHelper mDbHelper;
私有数据库mDb;
私人最终语境;
私有静态最终字符串数据库\u创建=
“创建表格”+表格名称+”(“+
\u id整数主键自动递增+
标题文本不为空+
“注意文本不为空);”;
公共数据库助手(上下文ctx){
this.dbContext=ctx;
}
public DatabaseHelper open()引发SQLException{
mDbHelper=新的OpenHelper(dbContext);
mDb=mDbHelper.getWritableDatabase();
归还这个;
}
公众假期结束(){
mDbHelper.close();
}
公共布尔createNote(字符串标题、字符串注释){
ContentValues initialValues=新的ContentValues();
初始值。放置(“标题”,标题);
初始值。输入(“注释”,注释);
返回mDb.insert(表名,null,初始值)>0;
}
公共布尔updateNote(长行ID、字符串标题、字符串注释){
ContentValues args=新的ContentValues();
args.put(“标题”,标题);
参数put(“注释”,注释);
返回mDb.update(表名,参数,“\u id=“+rowId,null)>0;
}
public void deleteAll(){
mDb.delete(表名称,null,null);
}
公共无效删除记录(长rowID){
mDb.delete(表名,“\u rowId=“+rowId,null);
}
公共ArrayList fetchNotes(字符串标题)引发SQLException{
ArrayList myArray=新的ArrayList();
int指针=0;
Cursor mCursor=mDb.query(表名,新字符串[]{“\u id”,“title”,
“note”},null,null,
空,空,“_id”);
int titleColumn=mCursor.getColumnIndex(“标题”);
int noteColumn=mCursor.getColumnIndex(“注释”);
if(mCursor!=null){
if(mCursor.moveToFirst()){
做{
添加(新字符串[3]);
myArray.get(指针)[0]=mCursor.getString(标题列);
myArray.get(指针)[1]=mCursor.getString(noteColumn);
//递增指针变量。
指针++;
}while(mCursor.moveToNext());//如果可能,移动到下一条记录
}否则{
添加(新字符串[3]);
myArray.get(指针)[0]=“无结果”;
myArray.get(指针)[1]=“”;
}
}
返回myArray;
}
公共阵列列表selectAll(){
ArrayList结果=新建ArrayList();
int计数器=0;
Cursor Cursor=this.mDb.query(表_NAME,新字符串[]{“id”,“forename”,“姓氏”,“age”},null,null,null,“姓氏描述”);
if(cursor.mo)
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/container" >
</ListView>
getIntent().getStringExtra(...);
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(this, EditNote.class);
i.putExtra("listItem", listItems[position]);
startActivity(i);
}
});