gpt4 book ai didi

java - 使用数据库进行 Intent 传递的问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:27 27 4
gpt4 key购买 nike

我的问题是如何使用另一个 putExtra 方法将评级栏值传递到另一个 Activity ?有人解决这个问题吗?这是我的第一个 Activity

public void giveRating(View v)


{
String Name = spinner.getSelectedItem().toString();
Float Rating = ratingBar.getRating();
String query = "insert into Rating values('"+Name+"',"+Rating+")";
try
{
sqLiteDatabase.execSQL(query);
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("DATA SAVED");
ab.show();
}catch (Exception e)
{
Log.e("InsertionException",""+e);
}
}
public void viewRating(View v)
{
try {
StringBuffer sb = new StringBuffer();
String query = "select * from Rating";
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
boolean res = cursor.moveToFirst();
if (res) {
do {
String Name = cursor.getString(0);
String Rating = cursor.getString(1);

sb.append(Name + ":" + Rating+"\n");
} while (cursor.moveToNext());
} else {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("No data To read");
ab.show();
}

String value = sb.toString();
Intent i = new Intent(MainActivity.this, Items.class);
i.putExtra("k1", value);
startActivity(i);


}
catch (Exception e)
{
Log.e("ReadDataEXception",""+e);
}

第二个 Activity

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items_layout);
textView = (TextView)findViewById(R.id.tv3);
textView1 = (TextView)findViewById(R.id.tv4);
Intent i = getIntent();
String name;
Bundle b = getIntent().getExtras();
name= b.getString("k1");
textView.setText(name);
textView.setTextSize(20);

数据库 Activity

 public Database(Context c)


{
super(c,DBNAME,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
String query = "create table Rating(Name TEXT,Rating REAL)";
db.execSQL(query);
}catch (Exception e)
{
Log.e("TableCreationException",""+e);
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

最佳答案

您可以简单地添加另一个带有 keyNameputExtra,例如

    Intent i = new Intent(MainActivity.this, Items.class);
i.putExtra("name", name);
i.putExtra("rating",rating);
startActivity(i);

并从 Items Activity 中获取

    Intent i = getIntent();
String name;
String rating;

Bundle b = getIntent().getExtras();

name= b.getString("name");
rating= b.getString("rating");

textView.setText(name);
textView1.setText(rating);

textView.setTextSize(20);

希望对您有帮助。

关于java - 使用数据库进行 Intent 传递的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794123/

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