Android Hashmap return value from other class - android

i will like to pass the value from DatabaseHandler.java lets say "name" to mainActivity and show it on textview.
DatabaseHandler.java
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
MainActivity.java
TextView lblid;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lblid = (TextView) findViewById(R.id.showid);
HashMap<string, string=""> details = getUserDetails();
String nametext = details.getString("name");
lblid.setText(nametext);
}
I am having trouble writing correct code to make it works, i keep getting "Syntax error on token". Please help me out.
Thanks.

this line cause problem
HashMap<string, string=""> details = getUserDetails();
replace it with
HashMap<string, string> details = getUserDetails();
Usage exaple
Main.java
public class MainActivity extends Activity {
...
private DatabaseHandler dbh;
...
public void onCreate(Bundle) {
dbh = new DatabaseHandler(...);
...
}
...
HashMap<String, String> details = dbh.getUserDetails();
...
String nametext = details.get("name");

Related

listview-SQLite updating the wrong row

When I update the selected row it updates the wrong row for example if I select second row, it will update the first row.
Here is my code inside db controller
Public boolean updateContact (String id, String category, String code, String description, String unit, String quantity) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.Col_2 , category);
values.put(FeedReaderContract.FeedEntry.Col_3 , code);
values.put(FeedReaderContract.FeedEntry.Col_4 , description);
values.put(FeedReaderContract.FeedEntry.Col_5 , unit);
values.put(FeedReaderContract.FeedEntry.Col_6 , quantity);
// updating row
database.update(TABLE_NAME ,values , FeedReaderContract.FeedEntry.Col_1 + " = ?" , new String[]{id} );
return true;
}
public ArrayList<HashMap<String, String>> getAllProducts() {
ArrayList<HashMap<String, String>> proList;
proList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM Countsheet";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Category", cursor.getString(1));
map.put("Code", cursor.getString(2));
map.put("Description", cursor.getString(3));
map.put("Unit", cursor.getString(4));
map.put("Quantity", cursor.getString(5));
proList.add(map);
} while (cursor.moveToNext());
cursor.close();
}
return proList;
}
Here is my button onclick to update the listview:
Public void adddata(){
btnadddata.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v) {
boolean i = myDb.updateContact(passid , passedVar , passcode, passdesc, passunit , quantitys.getText().toString());
if (i == true)
Toast.makeText(Main2Activity.this , "Data Inserted" , Toast.LENGTH_SHORT).show();
else
Toast.makeText(Main2Activity.this , "Data Not Inserted" , Toast.LENGTH_SHORT).show();
}
}
);
}
I think you are missing 'passid' .
put 'passid' in your HashMap while fetching from database and then retrieve.
HEY GUYSSSS THANK YOU FOR ANSWERING I APPRECIATE YOU ALL , I JUST SUDDENLY GOT THE ANSWER WICH IS I JUST ADD 1 TO THE STRING.VALUEOF(ID) AND IT JUST WORKED OUT THANKS YOU GUYSSS!
//here is my code
intent.putExtra(ID_EXTRA1 , String.valueOf(id + 1));
startActivity(intent);

Is it possible to get both Interger and String value from one HashMap method?

I want to merge my two Hashmap methods. One method is for getting user ID and the other is for getting user String information. But I want to get all information from one hashmap method. Is it possible? If it is, how can I do this in my getuserdetails hashmap method?
Method 1:
public HashMap<String, Integer> getUid() {
HashMap<String, Integer> uid = new HashMap<String,Integer>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
Log.d("database", "before uid put");
if (cursor.getCount() > 0) {
uid.put("uid",cursor.getInt(0));
}
cursor.close();
// return user
return uid;
}
Method 2:
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("created_at", cursor.getString(3));
}
cursor.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
The cleanest approach here would be to create a User object with fields for id, name, email and created_at, something like this:
public class User {
private int id;
private String name;
private String email;
private Date createdAt; // Or use string here, whichever you want
// create or generate getters and setters for above fields
}
You can then get the data from the database using your query and create a user:
public User getUserDetails() {
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
User user = new User();
if (cursor.getCount() > 0) {
user.setId(cursor.getInt(0));
user.setName(cursor.getString(1));
user.setEmail(cursor.getString(2));
user.setCreatedAt(cursor.getString(3));
}
else {
cursor.close();
return null; // There wasn't a user to be found
}
cursor.close();
return user;
}
Now your method creates a User object, containing all required information.
Make a class with uid and name veriable And Create a hashmap method as a return type of this class. Hope this will work.
you can wrap together in a class like
public class Info {
public int uid;
public String name;
}
and have a HashMap<String, Info>

Android: databasehandler return null

i have this method in my database class as shown below:
public HashMap<String, String> getUser() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + user;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("userid", cursor.getString(3));
}
cursor.close();
db.close();
return user;
}
and in my homeActivity I am able to fetch the value as shown below:
db = new DatabaseHandler(this);
HashMap<String, String> userdetails = db.getUser();
String name = userdetails.get("name");
userID = userdetails.get("userid");
username.setText(userID + " " + name );
and when a continue button is clicked, it will go to the next activity and i want to pass the userid value to the next activity
buttonContinue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent afterhome = new Intent(getApplicationContext(),
SecondActivity.class);
afterhome.putExtra("userid", userid);
startActivity(afterhome);
}
});
However, the value i got from the secondActivity is always 0.
my second Activity codes are as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
userid = intent.getStringExtra("userid");
}
Why is that so? Please advice. Thank you

Key value pairs from sqlite to spinner

Could someone show me the correct way to achieve this.
I have setup a hashmap from a sqlite query as follows
public HashMap<String, String> getData(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_DEVICES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("device_name", cursor.getString(1));
user.put("id", cursor.getString(3));
}
cursor.close();
db.close();
// return user
return user;
}
I then want a spinner where the device names are displayed but when selected its the id that is then used for the next action
I was thinking something like this , but I am not sure exactly how.
private void loadLockScreenSpinner() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
HashMap<String,String> hm = db.getData();
String device_name = hm.get("device_name");
String id = hm.get("id");
ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String,String>>(this, android.R.layout.simple_spinner_item);
adapter.add(hm);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
devicesSpinner.setAdapter(adapter);
devicesSpinner.setWillNotDraw(false);
}
Not sure what to put in the onItemSelected

Null Pointer Exception print in needs a message error from sqlite

I'm getting a NullPointerException: println needs a message Error when trying to start an activity. I can't see the error.
Here is my database concerning the error
public HashMap<String, String> getsaleInfo(String saleId) {
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM SUPERSALE where supersaleId='"+saleId+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
//HashMap<String, String> map = new HashMap<String, String>();
wordList.put("saleCust", cursor.getString(1));
wordList.put("saleName", cursor.getString(2));
wordList.put("saleTime", cursor.getString(4));
wordList.put("saleTax", cursor.getString(5));
wordList.put("saleNotes", cursor.getString(6));
//wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
And here is how I retrieve in the activity
saleCust = (TextView) findViewById(R.id.saleCust);
saleNotes = (TextView) findViewById(R.id.saleNotes);
saleName = (TextView) findViewById(R.id.saleName);
saleTime = (TextView) findViewById(R.id.saleTime);
saleTax = (TextView) findViewById(R.id.saleTax);
Intent objIntent = getIntent();
String saleId = objIntent.getStringExtra("supersaleId");
Log.d("Reading: ", "Reading all contacts..");
HashMap<String, String> saleList = controller.getsaleInfo(saleId);
Log.d("saleCust",saleList.get("saleCust"));
Log.d("saleName",saleList.get("saleName"));
Log.d("saleTime",saleList.get("saleTime"));
Log.d("saleTax",saleList.get("saleTax"));
Log.d("saleNotes",saleList.get("saleNotes"));
if(saleList.size()!=0) {
saleCust.setText(saleList.get("saleCust"));
saleName.setText(saleList.get("saleName"));
saleTime.setText(saleList.get("saleTime"));
saleTax.setText(saleList.get("saleTax"));
saleNotes.setText(saleList.get("saleNotes"));
}}
The error is coming from line
Log.d("saleCust",saleList.get("saleCust"));
Please post your logcat next time and your UI activity in question. Since you have textview from your layout, you could be using a Listview, ExpandedListView, etc. If so, android view's require that there is a column _id in your table and is retrieved in your query so that the view can keep in sync with the data adapter's data from your database cursor via the _id column.
Hope this helps.

Categories

Resources