Reputation: 1840
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>
Upvotes: 0
Views: 232
Reputation: 2252
change comparison of string if(name1=="Lim X Y") to if (name1.equalsIgnoreCase("Lim X Y"))
Upvotes: 0
Reputation: 1457
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?
Also you use name1.equals
or name1.equalsIgnoreCase
for String
comparison.
Upvotes: 1