从SQLite提取数据到新类 [英] Fetch data from SQLite to new class
问题描述
我还是Android开发的新手,现在面临使用intent
从SQLite
到另一个class
提取数据的问题.我已经阅读了很多文档,但是仍然无法达到预期的效果. DisplayData.java
上没有数据显示.我有错过任何事情吗???以下是我的代码段.
I'm still new to android development and now facing problem on fetching data from SQLite
to another class
by using intent
. I have read a lot of documentation but still failing to attain the desired results. There are no data display on DisplayData.java
. Have I missed out anything??? Below are my coding snippet.
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);
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
dialog.dismiss();
}
});
builder.show();
}
});
}
DisplayData.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");
if(name1=="Lim X Y")
{
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)
{
cursor.moveToFirst();
cursor.getString(cursor.getColumnIndex("weather"));
cursor.getString(cursor.getColumnIndex("date"));
cursor.getString(cursor.getColumnIndex("status"));
}
}
}
}
InfoAPI.java
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.project.project.database.MyDatabaseHelper;
import android.content.Context;
import android.database.SQLException;
import android.content.ContentValues;
public class InfoAPI {
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status};
public InfoAPI(Context context)
{
dbHelper=new MyDatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public long insertTimeSheet(String name,String weather,String date,String status){
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Name,name);
values.put(MyDatabaseHelper.Weather,weather);
values.put(MyDatabaseHelper.Date,date);
values.put(MyDatabaseHelper.Status, status);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
Cursor cursor = database.rawQuery("SELECT MAX(ID) FROM "+ MyDatabaseHelper.TABLE_INFO, null);
if(cursor.getCount()>0){
cursor.moveToFirst();
return cursor.getLong(0);
}
return 0;
}
}
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>
</TableLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
推荐答案
在调用ts.insertTimeSheet(name, weather, date, status)
时是否调试并检查数据是否确实存储在数据库中以及变量name, weather, date
和status
是否包含有价值吗?
Did you debug and check whether data is actually being stored in the database when you are calling ts.insertTimeSheet(name, weather, date, status)
and that the variables name, weather, date
and status
contain some value?
还要使用name1.equals
或name1.equalsIgnoreCase
进行String
比较.
这篇关于从SQLite提取数据到新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!