Android database application not responding - android

I am making an application to Store textfield values to database.After entering values when I press the button to submit it, it just hanged and give no response.
my onCreate code is given below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final DBAdapter db = new DBAdapter(this);
//---add 2 titles---
db.open();
final long d_id ;
final EditText input1 = (EditText) findViewById(R.id.isbn);
final String isbn = input1.getText().toString();
final EditText input2 = (EditText) findViewById(R.id.titleText);
final String tText = input2.getText().toString();
final EditText input3 = (EditText) findViewById(R.id.publisherText);
final String pText = input3.getText().toString();
this.btn = (Button)this.findViewById(R.id.button1);
this.btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
db.insertTitle(isbn, tText, pText);
}
});
db.close();
}
}
isbn,titleText and publisherText are my id for Textfield.
insertTitle is function to insert the values.
Whats wrong with my code?
thanks.

You are reading the values of your text fields as the app is being created, not when the button is pushed. You will always enter the initial values of your EditTexts with your current code. Try this:
db.insertTitle(input1.getText().toString(), input2.getText().toString(), input3.getText().toString());
You will have to move where you close your database, otherwise you will get an error for attempting to access a closed database. Try putting it in onPause() or onDestroy().
If this isn't the solution please post your logcat errors so we don't have to guess what is happening.

try this
this.btn = (Button)this.findViewById(R.id.button1);
this.btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
final EditText input1 = (EditText) findViewById(R.id.isbn);
final String isbn = input1.getText().toString();
final EditText input2 = (EditText) findViewById(R.id.titleText);
final String tText = input2.getText().toString();
final EditText input3 = (EditText) findViewById(R.id.publisherText);
final String pText = input3.getText().toString();
db.insertTitle(isbn, tText, pText);
}
});

I think your problem is open() and close() they are in onclick method or about your insertTitle() method. Check your values and types.
ContentValues cv = new ContentValues();
cv.put(KEY_WORK_OUT, workout);
cv.put(KEY_NAME, name);
cv.put(KEY_REP,rep);
cv.put(KEY_WEIGHT, weight);
ourDataBase.insert(DATABASE_TABLE, null, cv);

Related

Android Studio get data from sqlite

I had some problem about get data from sqlite different database.One database that save user's name and phone and they can update as they like. While another is house information but also contain user's name and phone.
My problem is i just realize that the phone that had been update in user database is not same with phone that previous store in house database. So i wanna call the user's phone from user database and use for phone call function and message function. But it does not work.And i trying to match the fullname that in user database with house database to call the phone data but still cant.
here is my call and message function
public class HouseDetail extends AppCompatActivity {
EditText etAddress,etDetail,etPhone;
TextView txType,txProperty,txPrice,txState,txTitle,txOther,txSize,txFullname;
ImageButton bPhone,bMessage;
String type,property,price,state,address,title,other,size,detail,fullnames,fullname,phone;
HousesDB db = new HousesDB(this);
Houses houses;
DatabaseOperations Udb = new DatabaseOperations(this);
PersonalData profileInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_house_detail);
txType = (TextView) findViewById(R.id.txTypes);
txProperty = (TextView) findViewById(R.id.txProperty);
txPrice = (TextView) findViewById(R.id.txPrice);
txState = (TextView) findViewById(R.id.txState);
etAddress = (EditText) findViewById(R.id.etAddress);
txTitle = (TextView) findViewById(R.id.txTitle);
txOther = (TextView) findViewById(R.id.txOther);
txSize = (TextView) findViewById(R.id.txSize);
txFullname = (TextView) findViewById(R.id.txFullname);
etPhone = (EditText) findViewById(R.id.etPhone);
etDetail = (EditText) findViewById(R.id.etDetail);
bPhone = (ImageButton) findViewById(R.id.bPhone);
bMessage = (ImageButton) findViewById(R.id.bMessage);
fullnames = txFullname.getText().toString();
type = txType.getText().toString();
property = txProperty.getText().toString();
price = txPrice.getText().toString();
state = txState.getText().toString();
address = etAddress.getText().toString();
title = txTitle.getText().toString();
other = txOther.getText().toString();
size = txSize.getText().toString();
detail = etDetail.getText().toString();
phone = etPhone.getText().toString();
Intent i = getIntent();
property = i.getStringExtra("house_property");
txProperty.setText(property);
houses = db.getInfo(property);
txType.setText(houses.getTypes());
txFullname.setText(houses.getFullname());
txPrice.setText(houses.getPrice());
txState.setText(houses.getState());
etAddress.setText(houses.getAddress());
txTitle.setText(houses.getTitle());
txOther.setText(houses.getOther());
txSize.setText(houses.getSize());
etDetail.setText(houses.getDetail());
this.fullnames = fullname;
profileInfo = Udb.getNumber(fullname);
etPhone.setText(profileInfo.get_phone());
bPhone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + etPhone));
startActivity(callIntent);
}
});
bMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + etPhone)));
}
});
}
Here is my user database that get the phone number
public PersonalData getNumber(String fullname)
{
SQLiteDatabase SQ = this.getWritableDatabase();
String query = "select FullName,Phone from "+ Table_NAME;
Cursor CR = SQ.rawQuery(query,null);
String a;
PersonalData info = new PersonalData();
if (CR.moveToFirst())
{
do {
a = CR.getString(0);
if (a.equals(fullname)) {
info._phone = CR.getString(1);
break;
}
}while (CR.moveToNext());
}
return info;
}
try this:
public PersonalData getNumber(String fullName) {
SQLiteDatabase database = this.getReadableDatabase();
String table = "YOUR TABLE NAME";
String[] columns = {
"FullName" ,
"Phone"
};
Cursor cursor = database.query(table , columns , null , null , null , null , null);
cursor.moveToPosition(-1);
PersonalData info = new PersonalData();
while (cursor.moveToNext()) {
String name = cursor.getString(0);
if (name.equals(fullName)) {
info._phone = cursor.getString(1);
break;
}
}
cursor.close();
return info;
}
Replace
a = CR.getString(0);
if (a.equals(fullname)) {
info._phone = CR.getString(1);
break;
}
with
a = CR.getString(CR.getColumnIndex("Fullname"));
if (a.equals("FullName")) {
info._phone = CR.getString(CR.getColumnIndex("Phone"));
break;
}
your data gotten by query maybe not be ordered as you want,in your case that is, the result maybe not in "Fullname,Phone",so you should use getColumnIndex() to get data for each column.

Search in Android depends on value of spinner

I created an app that shows a list of places and i created a search dialog wherein the user will type in edittext so he/she will find the desired place. First, search is working when its only place and I try to add spinner with values which are the region of the places and I got problem on the line which i will post below.
GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString(),dbhelper.getPlaceSearch(placeRegion.getSelectedItem().toString()));
It says getPlaceSearch (String, string) in DatabaseHelper cannot be applied to (String)
This is my database helper
public List<PlaceModel> getPlaceSearch(String location, String region) {
List<PlaceModel> search = new ArrayList<PlaceModel>();
String selectQuery = "SELECT * FROM listing_place where province_name like '" + location + "' and region = '"+region+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
PlaceModel pm = new PlaceModel();
pm.setlisting_title(cursor.getString(cursor.getColumnIndex(KEY_PLACE)));
search.add(pm);
}
while (cursor.moveToNext());
}
cursor.close();
return search;
}
This is my main activity
final Spinner placeRegion = (Spinner) dialog.findViewById(R.id.spinnerregion);
ArrayAdapter<String> RegionAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.spinner_layout, db.getAllRegion());
RegionAdapter.setDropDownViewResource(R.layout.spinner_layout);
placeRegion.setAdapter(RegionAdapter);
placeLocationEditText = (EditText)dialog.findViewById(R.id.placelocation);
Button button = (Button) dialog.findViewById(R.id.btnplacesearch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
PlaceListView = findViewById(R.id.placelayout);
ViewGroup parent = (ViewGroup) PlaceListView.getParent();
parent.removeView(PlaceListView);
PlaceSearchView = getLayoutInflater().inflate(R.layout.searchresult_place, parent, false);
parent.addView(PlaceSearchView);
GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString(),dbhelper.getPlaceSearch(placeRegion.getSelectedItem().toString()));
lv2 = (ListView) findViewById(R.id.searchplace_list);
lv2.setAdapter(new ViewAdapterSearchPlace());
ok replace your code to below..
GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString(),placeRegion.getSelectedItem().toString());

whats wrong in my code, showing "null pointer exception" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am new at android. I am trying to take data from user and toast to screen... but I am facing null pointer exception... i am trying from long time to solve ...plzzz help me
here is my code:
public class MainActivity extends ActionBarActivity {
private EditText etName;
private EditText etEmail;
private EditText etPhone;
private EditText etDesignation;
DatabaseHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.etName);
findViewById(R.id.etEmail);
findViewById(R.id.etPhone);
findViewById(R.id.etDesignation);
//dbHelper = new DatabaseHelper(this);
}
public void save(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String phone = etPhone.getText().toString();
String designation = etDesignation.getText().toString();
Employee employee = new Employee(name, email, phone, designation);
Toast.makeText(getApplicationContext(), employee.toString(),
Toast.LENGTH_LONG).show();
}
I suggest you to check the basics first
replace this part:
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
etPhone = (EditText) findViewById(R.id.etPhone);
etDesignation = (EditText) findViewById(R.id.etDesignation);
dbHelper = new DatabaseHelper(this);
None your your fields are initialized. Doing just a findViewById(R.id.etName); is not enough as global members will be holding null values giving NullPointerException. Hence assignment is important
private EditText etName; // default - null
private EditText etEmail; // default - null
private EditText etPhone; // default - null
private EditText etDesignation; // default - null
DatabaseHelper dbHelper; // default - null
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// changes here, link layout fields to objects
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText)findViewById(R.id.etEmail);
etPhone = (EditText)findViewById(R.id.etPhone);
etDesignation = (EditText)findViewById(R.id.etDesignation);
//dbHelper = new DatabaseHelper(this);
}
public void save(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String phone = etPhone.getText().toString();
String designation = etDesignation.getText().toString();
Employee employee = new Employee(name, email, phone, designation);
Toast.makeText(getApplicationContext(), employee.toString(),
Toast.LENGTH_LONG).show();
}
Try this
private EditText etName;
private EditText etEmail;
private EditText etPhone;
private EditText etDesignation;
DatabaseHelper dbHelper;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
etName = (EditText)findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
etPhone = (EditText) findViewById(R.id.etPhone);
etDesignation = (EditText) findViewById(R.id.etDesignation);
//dbHelper = new DatabaseHelper(this);
}
public void save(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String phone = etPhone.getText().toString();
String designation = etDesignation.getText().toString();
Employee employee = new Employee(name, email, phone, designation);
Toast.makeText(context, employee.toString(),
Toast.LENGTH_LONG).show();
}
In onCreate
etName = (EditText)findViewById(R.id.etName);
etEmail = (EditText)findViewById(R.id.etEmail);
etPhone = (EditText)findViewById(R.id.etPhone);
etDesignation = (EditText)findViewById(R.id.etDesignation);
You forgot to initialize the views and you called getText() on null
It appears your fields are not being instantiated, this is usually the indication of a null pointer exception.Have you tried changing
findViewById(R.id.etName);
findViewById(R.id.etEmail);
findViewById(R.id.etPhone);
findViewById(R.id.etDesignation);
to
this.eName = (EditText)findViewById(R.id.etName);
this.etEmail = (EditText)findViewById(R.id.etEmail);
this.etPhone = (EditText)findViewById(R.id.etPhone);
this.etDesignation = (EditText)findViewById(R.id.etDesignation);
? Also are you certain that all if the R.id. are valid references?

How to store Multiline EditText into SQLiteDatabase? [ANDROID]

I'm working on an application where I've 2 Multiline EditText and I need to store them into database. i only know how to store the single line EditText.
Here is my codes for inserting into database:
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//startActivityForResult(new Intent(Intent.ACTION_PICK),PICK_CONTACT);
likeDB.open();
long likes_id;
Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NameValue = nameSpinner.getSelectedItem().toString();
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
String LikesValue = txtLikes.getText().toString();
likes_id = likeDB.insertLikes(NameValue, LikesValue);
likeDB.close();
dislikeDB.open();
long dislikes_id;
Spinner names = (Spinner) findViewById(R.id.nameSpinner);
String NamesValue = names.getSelectedItem().toString();
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
String DislikesValue = txtDislikes.getText().toString();
dislikes_id = dislikeDB.insertDislikes(NamesValue, DislikesValue);
Toast.makeText(getBaseContext(),
"Your information is saved successfully!", Toast.LENGTH_SHORT).show();
dislikeDB.close();
}
});
This the code where I've declared the LikeDBAdapter and DislikesDBAdapter above the onCreate() method:
LikesDBAdapter likeDB = new LikesDBAdapter(this);
DislikesDBAdapter dislikeDB = new DislikesDBAdapter(this);
I need help with this. Any help will be appreciated. Thanks!
=)
set a method on your dbHelper class and
use a ContentValues, e.g :
public boolean createNote(String VALUE_1 , String VALUE_2) {
ContentValues initValue = new ContentValues();
initValue.put(COLUMN_NAME_1 , VALUE_1);
initValue.put(COLUMN_NAME_2 , VALUE_2);
return mDb.insert(YOUR_TABLE_NAME , null, initValue) > 0;
}

Can't see data from db in my listview

I was trying to display my data dynamically in a listview. I first insert data into db then displaying them in listview. I don't understand why but I created db and listview, it doesn't show data when I try to display it.
I'm getting data from EditText fields but I insert and display the data in another activity. I first thought I coludn't get the data from first activity, but I'm sure that I wrote the right code.
The field for the data in listview always empty. I should create a new entry when I press a "Add" button. I can see that it creates a new row in the list with a new id but the rest is empty.
Why I don't see data there? This is how I get data and put inside db and list:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
String name = "";
String email = "";
String phone = "";
String address = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
name = extras.getString("name");
email = extras.getString("email");
phone = extras.getString("phone");
address = extras.getString("address");
}
listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.COLUMN_NAME, SQLiteAdapter.COLUMN_EMAIL,SQLiteAdapter.COLUMN_PHONE, SQLiteAdapter.COLUMN_ADDRESS};
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3, R.id.text4};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.insert(name,email,phone,address);
updateList();
}
This is my main activity
Button next = (Button) findViewById(R.id.add);
EditText nametext = (EditText) this.findViewById(R.id.NameText);
final String nt = nametext.getText().toString();
EditText emailtext = (EditText) this.findViewById(R.id.MailText);
final String et = emailtext.getText().toString();
EditText phonetext = (EditText) this.findViewById(R.id.PhoneText);
final String pt = phonetext.getText().toString();
EditText addresstext = (EditText) this.findViewById(R.id.AddressText);
final String at = addresstext.getText().toString();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getApplicationContext(), Activity2.class);
myIntent.putExtra("name",nt);
myIntent.putExtra("email",et);
myIntent.putExtra("phone",pt);
myIntent.putExtra("address",at);
startActivity(myIntent);
}
});
Here is my queueAll function:
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, COLUMN_NAME,
COLUMN_EMAIL,COLUMN_PHONE,COLUMN_ADDRESS};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}

Categories

Resources