Android-value not able to set in textview - android

I have a SQLite database add2cart which stores details of products added to cart.I need to display the number of products each user have in their cart,in a text view. crtno is my textview.I want to display the number in textview when each user clicks add2cart button.
code
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dtls);
name = (TextView) findViewById(R.id.txtPr_name);
price = (TextView) findViewById(R.id.txtprice);
specification=(TextView)findViewById(R.id.txtPr_spec);
feature=(TextView)findViewById(R.id.txtPr_feature);
crtno=(TextView)findViewById(R.id.crtno);
add2cart=(Button)findViewById(R.id.add2cart);
DataBaseHandler dbh = new DataBaseHandler(this);
SQLiteDatabase db = dbh.getWritableDatabase();
Intent in = getIntent();
Bundle bn = in.getExtras();
Bundle bun=in.getExtras();
final String dtl=bun.getString("key");
nme = bn.getString("name");
Cursor cr = db.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String pr1price = cr.getString(cr.getColumnIndex("pprice"));
String prspc=cr.getString(cr.getColumnIndex("pspec"));
String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
pname = name;
prprice = pr1price;
pspec=prspc;
pfeature=prfeature;
}
name.setText(pname);
price.setText("Rs " +prprice + "/-");
specification.setText(pspec);
feature.setText(pfeature);
add2cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean incart=false;
String nm=name.getText().toString();
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"' AND usr='"+dtl+"'",null);
if (cur.moveToFirst()){
String prdname=cur.getString(cur.getColumnIndex("pnme"));
if (nm.equals(prdname)){
add2cart.setText("Already in Cart");
incart=true;
}
}
if(incart==false){
mydb.execSQL("INSERT INTO add2cart (usr,pnme,prate)VALUES('"+dtl+"','"+nm+"','"+prprice+"')");
Toast.makeText(getApplicationContext(),"added to cart",Toast.LENGTH_SHORT).show();
Cursor crsr=mydb.rawQuery("select pnme from add2cart where usr='"+dtl+"'", null);
// int count=0;
Boolean val=false;
int count=crsr.getCount();
Toast.makeText(getApplicationContext(),"count"+count, Toast.LENGTH_LONG).show();
crtno.setText(Integer.toString(count));
// abc=crtno.getText().toString();
if (crtno!=null) {
val=true;
crtno.setText(Integer.toString(count));
}
if (val=false) {
crtno.setText("0");
}
}
}
});
}
}

change condition.(make sure you have done above on click listner crtno = findviewbyId....)
if(count != null && count > 0){
crtno.setText(count+"");
}else{
crtno.setText("0");
}

Related

Android-Null pointer Exception in setText

I have a SQLite database to store items in cart. I need to get the count of products in cart corresponding to each user and set it to a textview,each time the user clicks addtocart button.But my code throws an null point exception.please help me.
I am getting nullpointer Exception in line 109: if (!crtno.getText().toString().equals(""))
code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dtls);
crtno=(TextView)findViewById(R.id.crtno);
imgbtn=(ImageButton)findViewById(R.id.cartimg);
add2cart=(Button)findViewById(R.id.add2cart);
DataBaseHandler dbh = new DataBaseHandler(this);
SQLiteDatabase db = dbh.getWritableDatabase();
Intent in = getIntent();
Bundle bn = in.getExtras();
Bundle bun=in.getExtras();
final String dtl=bun.getString("key");
nme = bn.getString("name");
Cursor cr = db.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String pr1price = cr.getString(cr.getColumnIndex("pprice"));
String prspc=cr.getString(cr.getColumnIndex("pspec"));
String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
pname = name;
prprice = pr1price;
pspec=prspc;
pfeature=prfeature;
}
name.setText(pname);
price.setText("Rs " +prprice + "/-");
specification.setText(pspec);
feature.setText(pfeature);
add2cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean incart=false;
String nm=name.getText().toString();
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"' AND usr='"+dtl+"'",null);
if (cur.moveToFirst()){
String prdname=cur.getString(cur.getColumnIndex("pnme"));
if (nm.equals(prdname)){
add2cart.setText("Already in Cart");
incart=true;
}
}
if(incart==false){
mydb.execSQL("INSERT INTO add2cart (usr,pnme,prate)VALUES('"+dtl+"','"+nm+"','"+prprice+"')");
Toast.makeText(getApplicationContext(),"added to cart",Toast.LENGTH_SHORT).show(); Cursor crsr=mydb.rawQuery("select pnme from add2cart where usr='"+dtl+"'", null);
int count=0;
if (!crtno.getText().toString().equals("")) {
count=crsr.getCount();
}
crtno.setText(Integer.toString(count));
}
}
});
}
}
Try like
crtno.getText().toString().equals("")
instead of
crtno.equals("")
Because crtno.getText().toString().equals("") is the command for checking equality of the text inside the textview.
At first you should first pull string displayed in textview and it's done as by:
getText()
Return the text the TextView is displaying.
so your way should be
crtno.getText().toString().equals("")
also try this
crtno.getText().toString().equals(null)

Android SQLite Database table - Created table.But error log says no such table

I am developing a shopping cart app.But got an error in adding item details to database, on
clicking ADD TO CART button. Error log says there is no such database table.and force me to SHUTTING DOWN VM. Please help me out.
I have two database in this activity.problem is with add2cart table in addcart database
Product_Detail.java
public class Product_Details extends Activity{
TextView name,price,specification,feature
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dtls);
image=(ImageView)findViewById(R.id.pr_img);
name = (TextView) findViewById(R.id.txtPr_name);
price = (TextView) findViewById(R.id.txtprice);
specification=(TextView)findViewById(R.id.txtPr_spec);
feature=(TextView)findViewById(R.id.txtPr_feature);
add2cart=(Button)findViewById(R.id.add2cart);
by_nw=(Button)findViewById(R.id.buy_nw);
Intent in = getIntent();
Bundle bn = in.getExtras();
nme = bn.getString("key");
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(img BLOB,pnme varchar,prate varchar,pqty varchar,ptotl vachar)");
mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
Cursor cr = mydb.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String pr1price = cr.getString(cr.getColumnIndex("pprice"));
String prspc=cr.getString(cr.getColumnIndex("pspec"));
String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
pname = name;
prprice = pr1price;
pspec=prspc;
pfeature=prfeature;
}
name.setText(pname);
price.setText("Rs " +prprice + "/-");
specification.setText(pspec);
feature.setText(pfeature);
add2cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nm=name.getText().toString();
String rate=price.getText().toString();
mydb.execSQL("INSERT INTO add2cart VALUES('"+nm+"','"+rate+"')");
Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
Intent in=new Intent(Product_Details.this,add2cart.class);
startActivity(in);
}
});
by_nw.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(Product_Details.this,buy_nw.class);
startActivity(in);
}
});
}
}
I opened my addcart database in setOnClickListner and my code worked.
Problem was the place where i opened the addcart database.It consider only the last opened database in Main.
Corrected code
public class Product_Details extends Activity{
TextView name,price,specification,feature;
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dtls);
image=(ImageView)findViewById(R.id.pr_img);
name = (TextView) findViewById(R.id.txtPr_name);
price = (TextView) findViewById(R.id.txtprice);
specification=(TextView)findViewById(R.id.txtPr_spec);
feature=(TextView)findViewById(R.id.txtPr_feature);
add2cart=(Button)findViewById(R.id.add2cart);
by_nw=(Button)findViewById(R.id.buy_nw);
Intent in = getIntent();
Bundle bn = in.getExtras();
nme = bn.getString("key");
mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
Cursor cr = mydb.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String pr1price = cr.getString(cr.getColumnIndex("pprice"));
String prspc=cr.getString(cr.getColumnIndex("pspec"));
String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
pname = name;
prprice = pr1price;
pspec=prspc;
pfeature=prfeature;
}
name.setText(pname);
price.setText("Rs " +prprice + "/-");
specification.setText(pspec);
feature.setText(pfeature);
add2cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nm=name.getText().toString();
String rate=price.getText().toString();
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pnme,prate)VALUES('"+nm+"','"+rate+"')");
Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
Intent in=new Intent(Product_Details.this,add2cart.class);
startActivity(in);
}
});
You use this line mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null); to reset the mydb,now the mydb is the batabase products

Fetch data from SQLite only contains single data row

In my app i am storing data to SQLite, and now i am trying to fetch that data from SQLite to activity.
as per requirement i just have to store single data at a time and my table will contain only single data row not more than one row.
so I want if table has data row then fetch data and show in form in onCreate(..) of LoginActivity.java
Getting:
The method SelectData(String) in the type myDBClass is not applicable for the arguments ()
myDBClass.java:
// Select Data
public String[] SelectData(String strOperatorID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_NAME, new String[] { "*" },
"OperatorID=?",
new String[] { String.valueOf(strOperatorID) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
arrData[0] = cursor.getString(0); // DeviceID
arrData[1] = cursor.getString(1); // EmailID
arrData[2] = cursor.getString(2); // Event
arrData[3] = cursor.getString(3); // Operator
arrData[4] = cursor.getString(4); // EventOperator
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
LoginActivity.java:-
public class LoginActivity extends Activity {
.................
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_login);
btnLogout = (Button) findViewById(R.id.btnLogout);
btnCamera = (Button) findViewById(R.id.btnCamera);
btnGallery = (Button) findViewById(R.id.btnGallery);
txtDeviceID = (TextView) findViewById(R.id.txtDeviceID);
txtEmailID = (TextView) findViewById(R.id.txtEmailID);
txtEvent = (TextView) findViewById(R.id.txtEvent);
txtOperative = (TextView) findViewById(R.id.txtOperative);
txtEventOperator = (TextView) findViewById(R.id.txtEventOperator);
Intent intent = getIntent();
deviceID = intent.getStringExtra("deviceID");
emailID = intent.getStringExtra("emailID");
event = intent.getStringExtra("name");
operative = intent.getStringExtra("firstName");
txtDeviceID.setText(deviceID);
txtEmailID.setText(emailID);
txtEvent.setText(event);
txtOperative.setText(operative);
txtEventOperator.setText(event + " " + operative);
strEvent = txtEvent.getText().toString();
strOperative = txtOperative.getText().toString();
// Dialog
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
AlertDialog ad = adb.create();
// new Class DB
final myDBClass myDb = new myDBClass(this);
// Save Data
long saveStatus = myDb.InsertData(
txtDeviceID.getText().toString(),
txtEmailID.getText().toString(),
txtEvent.getText().toString(),
txtOperative.getText().toString(),
txtEventOperator.getText().toString()
);
if(saveStatus <= 0)
{
ad.setMessage("Error!! ");
ad.show();
return;
}
// Show Data
String arrData[] = myDb.SelectData();
if(arrData != null)
{
txtDeviceID.setText(arrData[1]);
txtEmailID.setText(arrData[2]);
txtEvent.setText(arrData[3]);
txtOperative.setText(arrData[4]);
txtEventOperator.setText(arrData[5]);
}
if(txtEvent.getText().toString().equals("") && txtOperative.getText().toString().equals(""))
{
Intent intentCall = new Intent(LoginActivity.this, LicenseListActivity.class);
startActivity(intentCall);
}
}
From the op requirement..
change your method like this..
public String[] SelectData() {
// TODO Auto-generated method stub
try {
String arrData[] = new String[5];
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_NAME, null, null, null, null,
null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
arrData[0] = cursor.getString(0); // DeviceID
arrData[1] = cursor.getString(1); // EmailID
arrData[2] = cursor.getString(2); // Event
arrData[3] = cursor.getString(3); // Operator
arrData[4] = cursor.getString(4); // EventOperator
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
Your SelectData method takes a String argument (strOperatorID) but you are calling it with no argument, so obviously it cannot be found.
By the way you should respect Java naming conventions for your methods (i.e. not starting with upper case character)
public String[] SelectData() {
// TODO Auto-generated method stub
try {
String arrData[] = new String[5];
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_NAME, null, null, null, null,
null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do{
arrData[0] = cursor.getString(0); // DeviceID
arrData[1] = cursor.getString(1); // EmailID
arrData[2] = cursor.getString(2); // Event
arrData[3] = cursor.getString(3); // Operator
arrData[4] = cursor.getString(4); // EventOperator
} while (cur.moveToNext());
}
}
return arrData;
} catch (Exception e) {
return null;
}finally{
cursor.close();
db.close();
}

Date is not displaying on x axis in android

I am saving the patient details in the sqlite database. Patient details includes BP,sugar, creatinin, date, time of admission. Regarding BP I am storing Systolic and Diastolic values in the database. I want to show the values and date entered by the user in the graph. I am trying to show the date in x axis which is stored in the database. But I am not getting the date on x-axis.
ViewBloodPressureActivity.java
public class ViewBloodPressureActivity extends Activity {
SQLiteDatabase db;
String pName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_blood_pressure);
Intent intent = getIntent();
final String action = intent.getAction();
String gName = intent.getExtras().getString("GroupName");
pName = intent.getExtras().getString("PatientName");
Cursor cursor = null;
TextView tvPatientName = (TextView) this.findViewById(R.id.textViewPatientNameViewBP);
TextView tvDate = (TextView) this.findViewById(R.id.textViewPatientBPDate);
TextView tvTime = (TextView) this.findViewById(R.id.textViewPatientBPTime);
TextView tvSystolic = (TextView) this.findViewById(R.id.textViewPatientBPSystolic);
TextView tvDiastolic = (TextView) this.findViewById(R.id.textViewPatientBPDiastolic);
TextView tvNumOfRecords = (TextView) this.findViewById(R.id.textViewPatientBPNumOfRecords);
Button back = (Button) this.findViewById(R.id.buttonPatientBPBack);
Button trend = (Button) this.findViewById(R.id.buttonPatientBPTrend);
tvPatientName.setText(pName);
openPatientBPDatabase();
cursor = db.rawQuery("SELECT * FROM PatntBP WHERE PatientName = '"+pName+"';", null);
int rows = cursor.getCount();
tvNumOfRecords.setText(" "+rows+" for this patient");
switch (rows) {
case 0:Toast.makeText(getApplicationContext(), "No records in Database", Toast.LENGTH_LONG).show();break;
case 1: { cursor.moveToFirst();
tvDate.setText(cursor.getString(cursor.getColumnIndex("date")));
tvTime.setText(cursor.getString(cursor.getColumnIndex("time")));
tvSystolic.setText(cursor.getString(cursor.getColumnIndex("systolic")));
tvDiastolic.setText(cursor.getString(cursor.getColumnIndex("diastolic")));
break;
}
default : {
Log.v("BPDB", "There are :"+cursor.getCount()+" records.");
cursor.moveToLast();
tvDate.setText(cursor.getString(cursor.getColumnIndex("date")));
tvTime.setText(cursor.getString(cursor.getColumnIndex("time")));
tvSystolic.setText(cursor.getString(cursor.getColumnIndex("systolic")));
tvDiastolic.setText(cursor.getString(cursor.getColumnIndex("diastolic")));
Toast.makeText(getApplicationContext(), "Use Charts to view the Data", Toast.LENGTH_LONG).show();
}
}
closePatientBPDatabase();
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ViewBloodPressureActivity.this, ListPatientForViewHDActivity.class);
intent.setAction(action);
startActivity(intent);
}
});
trend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ViewBloodPressureActivity.this, TrendAnalysisForBPActivity.class);
intent.setAction(action);
intent.putExtra("PatientName", pName);
startActivity(intent);
}
});
}
private void closePatientBPDatabase() {
// TODO Auto-generated method stub
db.close();
}
private void openPatientBPDatabase() {
// TODO Auto-generated method stub
db = openOrCreateDatabase("HCDatabase", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS PatntBP (GroupName VARCHAR, PatientName VARCHAR, systolic VARCHAR, diastolic VARCHAR, date VARCHAR, time VARCHAR);");
}
public void lineGraphHandler(View view) {
LineGraph lineGraph = new LineGraph();
Intent lineIntent = lineGraph.getIntent(this);
startActivity(lineIntent);
}
private class LineGraph {
public Intent getIntent(Context context) {
//SQLiteDatabase db = context.openOrCreateDatabase("HCDatabase", 0, null);
// Cursor cursor = db.rawQuery("SELECT * FROM PatntBP", new String[] { pName });
SQLiteDatabase db1 = openOrCreateDatabase("HCDatabase", MODE_PRIVATE, null);
String sqlQuery = "SELECT * FROM PatntBP where PatientName = '"+pName+"';";
Log.v("BP Query", sqlQuery);
Cursor cursor = db1.rawQuery(sqlQuery, null);
int rows = cursor.getCount();
int y[] = new int[rows];
int y1[] = new int[rows];
if (cursor.getCount() > 0) {
cursor.moveToFirst();
int i = 0;
do {
String name = cursor.getString(cursor.getColumnIndex("PatientName"));
String systolic = cursor.getString(cursor.getColumnIndex("systolic"));
String diastolic = cursor.getString(cursor.getColumnIndex("diastolic"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String[] ss = date.split("/");
Log.v("BP Query", "Row : "+name+" "+systolic+" "+diastolic+" "+ss[1]);
y[i] = Integer.parseInt(systolic);
y1[i] = Integer.parseInt(diastolic);
i++;
} while (cursor.moveToNext());
}
db1.close();
int x[] = {1,2,3,4,5,6,7,8,9,10};
int x1[] = {1,2,3,4,5,6,7,8,9,10};
TimeSeries series = new TimeSeries("Systolic");
for (int i = 0; i < y.length; i++) {
series.add(x[i], y[i]);
}
TimeSeries series1 = new TimeSeries("Diastolic");
for (int i = 0; i < y1.length; i++) {
series1.add(x1[i], y1[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(series1);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setColor(Color.RED);
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setPointStyle(PointStyle.DIAMOND);
renderer1.setColor(Color.BLUE);
mRenderer.setChartTitle("Blood Pressure Variation");
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(renderer1);
mRenderer.setYAxisMax(200);
mRenderer.setYAxisMin(40);
Intent intent = ChartFactory.getTimeChartIntent(context, dataset, mRenderer, "Blood Pressure Graph");
return intent;
}
}
}
EnterBloodPressureActivity.java
public class EnterBloodPressureActivity extends Activity {
SQLiteDatabase db;
Date date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_blood_pressure);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = new Date();
String dateStringAndTime = sdf.format(date);
String dateAndTime[] = dateStringAndTime.split(" ");
String dateString = dateAndTime[0];
String timeString = dateAndTime[1];
final EditText etSystolic = (EditText) this.findViewById(R.id.editTextSystolic);
final EditText etDiastolic = (EditText) this.findViewById(R.id.editTextDiastolic);
final EditText etDate = (EditText) this.findViewById(R.id.editTextBPDate);
etDate.setText(dateString);
final EditText etTime = (EditText) this.findViewById(R.id.editTextBPTime);
etTime.setText(timeString);
TextView patientName = (TextView) this.findViewById(R.id.textViewEnterBPDataPatientName);
Intent intent = getIntent();
final String action = intent.getAction();
patientName.setText(intent.getExtras().getString("PatientName"));
Button save = (Button) this.findViewById(R.id.buttonBPDataSave);
Button reset = (Button) this.findViewById(R.id.buttonBPDataReset);
Button back = (Button) this.findViewById(R.id.buttonBPDataBack);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openPatientBPDatabase();
//get GroupName and PatientName
Intent intent = getIntent();
String gName = intent.getExtras().getString("GroupName");
String pName = intent.getExtras().getString("PatientName");
// ensure input data is not null
String systolic = etSystolic.getText().toString();
String diastolic = etDiastolic.getText().toString();
String date = etDate.getText().toString();
String time = etTime.getText().toString();
if (!systolic.equals("") && !diastolic.equals("") && !date.equals("") && !time.equals("")) {
String sqlStmt = "INSERT INTO PatntBP VALUES('"+gName+"', '"+pName+"', '"+systolic+"', '"+diastolic+"', '"+date+"', '"+time+"');";
Log.v("BP DEBUG", sqlStmt);
db.execSQL(sqlStmt);
closePatientBPDatabase();
Intent intent1 = new Intent(EnterBloodPressureActivity.this, ListPatientForEnterHDActivity.class);
intent1.setAction(action);
Toast.makeText(getApplicationContext(), "Data successfully entered", Toast.LENGTH_LONG).show();
startActivity(intent1);
} else {
Toast.makeText(getApplicationContext(), "No blank values allowed", Toast.LENGTH_LONG).show();
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
etSystolic.setText("");
etDiastolic.setText("");
etDate.setText("");
etTime.setText("");
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(EnterBloodPressureActivity.this, ListPatientForEnterHDActivity.class);
intent.setAction(action);
startActivity(intent);
}
});
}
protected void closePatientBPDatabase() {
// TODO Auto-generated method stub
db.close();
}
protected void openPatientBPDatabase() {
// TODO Auto-generated method stub
db = openOrCreateDatabase("HCDatabase", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS PatntBP (GroupName VARCHAR, PatientName VARCHAR, systolic VARCHAR, diastolic VARCHAR, date VARCHAR, time VARCHAR);");
}
}

setting view data on new activity

So I have a ListView on which I click to start new Activity called MerchantView.
Between the activities I am passing the uid which is a unique identifier of a merchant.
Im then extracting merchant data from DB and want to view this data in this view.
Everything works (while debugging i can see that data is taken from DB and passed properly to setText methods) but the data does not show, am I doing this right?
public class MerchantView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.merchant);
String desc = "";
String name = "";
Bundle extras = getIntent().getExtras();
String uid = "0";
if(extras !=null) {
uid = extras.getString("uid");
}
// get merchant from database
if(Integer.valueOf(uid) > 0){
Cursor c = Utilities.db.query(mydb.TABLE_MERCHANT,
null,
"uid=?", new String[] {uid}, null, null, null);
if(c.moveToFirst() != false){
name = c.getString(c.getColumnIndex(MerchantsColumns.COLname));
desc = c.getString(c.getColumnIndex(MerchantsColumns.COLdesc));
}
// set values to UI
TextView descUI = (TextView) findViewById(R.id.merchantDescription);
descUI.setText(desc);
TextView nameUI = (TextView) findViewById(R.id.merchantName);
nameUI.setText(name);
}
else{
}
Button buttonMerchants = (Button) findViewById(R.id.buttonMerchants);
buttonMerchants.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
Data was set properly. The problem was in layout. The header (LinearLayout) at the top had two buttons and it was set to android:layout_height="fill_parent" which was taking whole space. After fixing that, data is showing properly.
Try Below code hope it helps
SQLiteDatabase myDB = this.openOrCreateDatabase("databasename.db", SQLiteDatabase.OPEN_READWRITE, null);
try{
Cursor c = myDB.rawQuery("select name, desc from abctable where uid="+uid, null);
int Column1 = c.getColumnIndex("name");
int Column2 = c.getColumnIndex("desc");
// Check if our result was valid.
c.moveToFirst();
if (c != null) {
int i = 0;
// Loop through all Results
do {
i++;
String name = c.getString(Column1);
String desc = c.getString(Column2);
TextView descUI = (TextView) findViewById(R.id.merchantDescription);
descUI.setText(desc);
TextView nameUI = (TextView) findViewById(R.id.merchantName);
nameUI.setText(name);
} while (c.moveToNext());
}
} catch (SQLiteException e) {
e.printStackTrace();
} finally {
if (myDB != null)
myDB.close();
}

Categories

Resources