为什么我无法从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上显示。我该怎么做才能实现这一目标?
答案 0 :(得分:0)
点击“是”后,您将转到下一个活动并将名称传递给它:
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();
}