gpt4 book ai didi

java - 从 SQLite 检索数据并在另一个类中按行显示

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

为什么我无法从 SQLite 检索数据?我是不是错过了什么?我阅读了很多文档并尝试解决它,但仍然失败。

WorkDetailsTable.java

  Button btn1=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder=new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
long ab = ts.insertTimeSheet(name, weather, date, status);
Toast.makeText(context, "Data Saved", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(WorkDetailsTable.this,DisplayData.class);
intent.putExtra("name",name); //name will be used in another class
startActivity(intent);

DisplayDate.java

public class DisplayData extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaydata);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
final String name1 = getIntent().getExtras().getString("name");
Toast.makeText(getApplicationContext(),name1, Toast.LENGTH_SHORT).show();

if(name1.equals("John"))
{
SQLiteDatabase db=(new MyDatabaseHelper(this)).getReadableDatabase();
Cursor cursor=db.rawQuery("SELECT Weather,Date,Status FROM Information WHERE Name = ?",new String[]{""+name1});
if(cursor.getCount()==1)
{
String weather=cursor.getString(cursor.getColumnIndex("Weather"));
String date=cursor.getString(cursor.getColumnIndex("Date"));
String status=cursor.getString(cursor.getColumnIndex("Status"));


}
db.close();

}

displaydata.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" >

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="|"
android:layout_marginBottom="25dp">


<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView10"
android:background="@drawable/cell_shape"
android:textSize="17sp"
android:text="weather"/>

<TextView android:id="@+id/textView111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:background="@drawable/cell_shape"
android:text="date"/>

<TextView android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:background="@drawable/cell_shape"
android:text="status"/>
</TableRow>
// <TableRow>

// <TextView android:id="@+id/textView1111"
// android:layout_width="wrap_content"
// android:layout_height="wrap_content"
// android:textSize="17sp"
// android:background="@drawable/cell_shape"/>


</TableRow>
</TableLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>

我也尝试过这个,但没有运气

 TextView tvTemp=(TextView)findViewById(R.id.textView1111);
tvTemp.setText(weather + date + status);

数据应该按行检索并显示在另一个类中,而不是在 editText 上。我该如何实现这一目标?

最佳答案

单击"is"后,您将转到下一个 Activity 并向其传递名称:

public class DisplayData extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaydata);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Bundle bundle=getIntent().getExtras();
String name="";
if(bundle!=null)
{
name=bundle.getString("name");
}

//check whether you get the name value here or not..(use log)


SQLiteDatabase db=(new MyDatabaseHelper(this)).getReadableDatabase();
Cursor cursor=db.rawQuery("SELECT Weather,Date,Status FROM Information WHERE Name = ?",new String[]{""+name1});
if(cursor.getCount()==1)
{
String weather=cursor.getString(cursor.getColumnIndex("Weather"));//give the proper index of weather and same for rest.
String date=cursor.getString(cursor.getColumnIndex("Date"));
String status=cursor.getString(cursor.getColumnIndex("Status"));


}
db.close();

}

关于java - 从 SQLite 检索数据并在另一个类中按行显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33034168/

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