gpt4 book ai didi

android - 单击列表中的项目 - 将数据传递给另一个 Activity 并使用 sqlite 数据库显示其他列

转载 作者:行者123 更新时间:2023-11-29 21:22:37 24 4
gpt4 key购买 nike

我在这里搜索了很多,但没有找到适合我的应用程序的东西。

我有 3 个 Activity ( Activity 1、 Activity 2 和 Activity 3)。

Activity 1 是从我的数据库中填充的 ListView

Activity 2 是从我数据库中的另一个表填充的 ListView (关于我在 Activity 1 中选择的项目)。

Activity 3 是一个 TextView ,我们可以在其中找到 Activity 2 中所选项目的描述。

我的问题是我可以获得所选项目的 ID,但我需要显示其他列的值。

Activity 2 代码:

public final static String ID_EXTRA="com.example.testdb5._ID";

String passedVar=null;
private TextView passedView=null;
private BooklistHelper dbBookHelper = null;
private Cursor ourCursor = null;
private BookAdapter adapter=null;




public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);

passedVar=getIntent().getStringExtra(Tutorial16.ID_EXTRA);

ListView myListView = (ListView)findViewById(R.id.myListView);

dbBookHelper=new BooklistHelper(this);

ourCursor=dbBookHelper.getBooksByAuthor(passedVar);

startManagingCursor(ourCursor);

adapter=new BookAdapter(ourCursor);

myListView.setAdapter(adapter);

myListView.setOnItemClickListener(onListClick);

}

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id)
{
Intent i=new Intent(Activity2.this, Activity3.class);

i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);

}
};


class BookAdapter extends CursorAdapter {
BookAdapter(Cursor c) {
super(Activity2.this, c);

}

public void bindView(View row, Context ctxt,
Cursor c) {
BookHolder holder=(BookHolder)row.getTag();
holder.populateFrom(c, dbBookHelper);

}

public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row2, parent, false);
BookHolder holder=new BookHolder(row);
row.setTag(holder);
return(row);
}


}

static class BookHolder {
private TextView name=null;

BookHolder(View row) {
name=(TextView)row.findViewById(R.id.bookText);
}

void populateFrom(Cursor c, BooklistHelper r) {
name.setText(r.getName(c));
}
}

Activity 3 代码:

public class Activity3 extends Activity{

String passedVar=null;
private TextView passedView=null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3);

passedVar=getIntent().getStringExtra(Activity2.ID_EXTRA);

passedView=(TextView)findViewById(R.id.passed);

passedView.setText("You Clicked item Id="+passedVar);


}

书单助手

public class BooklistHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.testdb5/databases/";
private static String DB_NAME = "booklist.db";
private static final String TABLE_NAME = "Authors";
private static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "author_name";

private static final String SECOND_TABLE_NAME = "Books";
private static final String SECOND_COLUMN_ID = "_id";
public static final String SECOND_COLUMN_TITLE = "book_name";




private SQLiteDatabase myDataBase;

private final Context myContext;

/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public BooklistHelper(Context context) {

super(context, DB_NAME, null, 1);
this.myContext = context;
}


public void createDataBase() throws IOException{

boolean dbExist = checkDataBase();

if(dbExist){
//do nothing - database already exist
}else{

//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}

/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{

//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;

//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

if(myDataBase != null)
myDataBase.close();

super.close();

}



@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
public Cursor getCursor() {

SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

queryBuilder.setTables(TABLE_NAME);

String[] asColumnsToReturn = new String [] { COLUMN_ID, COLUMN_TITLE};

//make sure get search by string pass correctly
Cursor mCursor = queryBuilder.query(myDataBase, asColumnsToReturn, null,
null, null, null, "author_name ASC");



return mCursor;

}

public String getName(Cursor c) {
return(c.getString(1));

}

public Cursor getBooksByAuthor(String id) {
String[] args={id};

return(getReadableDatabase()
.rawQuery("SELECT _id, book_name FROM Books WHERE author_id=?",
args));
}

}感谢您的帮助,如果您需要更多信息,请不要犹豫。

最佳答案

我不完全理解你的问题,但我认为你想传递的不仅仅是 ID 到 Activity 3,

好吧,您可以将正在使用的整个对象作为可序列化项传递,

只需实现Serializable(class xxx implement Serializable),就可以了,

intent.putExtra("key", object);

在 Activity 3中

yourObjectInstance = (YourObject) getIntent().getSerializableExtra("key");

希望这对您有所帮助,否则如果我不是您所需要的,请向我们提供更多信息,

关于android - 单击列表中的项目 - 将数据传递给另一个 Activity 并使用 sqlite 数据库显示其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20555239/

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