how to save the result of a query in a int variable? - android

Hi I develop an application on android ADT. I am using sqlite to validate the registration. When the user registers are saved in a database and the database I want to check if a record already exists for the activity log is not open. This is the code that I used, not shown error but does not function.
public void onContinue(){
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"pacman", null, 1);
SQLiteDatabase bd = admin.getReadableDatabase();
int total;
Cursor c = bd.rawQuery("SELECT * FROM numeros", null);
total = c.getCount();
Intent nu;
if(total == 1){
nu = new Intent (this, ContactListActivity.class);
startActivity(nu);
}
else
nu=new Intent(this, Bienvenido.class);
startActivity(nu);
finish();
}

EDIT2:
I believe you wanted to start 1 activity only but when (total == 1) you are starting 2.
You should change
if(total == 1){
nu = new Intent (this, ContactListActivity.class);
startActivity(nu);
}
else
nu=new Intent(this, Bienvenido.class);
startActivity(nu);
To
if(total == 1)
nu = new Intent (this, ContactListActivity.class);
else
nu = new Intent(this, Bienvenido.class);
startActivity(nu);

Related

Not able to get String array in onActivtyResult method Android

In my case there are two activities Activity A and B
in Activty A i am calling startActivtyForResult and starting Activty B.
In activty B i am putting String array as extra as below,
// set selected contacts on DONE button press
private void setSelctedcontacts() {
ArrayList<Contact> selectedListWithPhoneNo = new ArrayList<Contact>();
ArrayList<Contact> selectedListWithEmail = new ArrayList<Contact>();
ArrayList<String> emailAddressList=new ArrayList<String>();
String[] ToAdress=new String[100];
int j=0;
Intent intent = new Intent();
ArrayList<Contact> contactList = contactAdapter.originalList;
for (int i = 0; i < contactList.size(); i++) {
Contact contact = contactList.get(i);
if (contact.isSelected()) {
if(contact.getContactNumber()!=null)
selectedListWithPhoneNo.add(contact);
if(contact.getContactEmail()!=null)
{
ToAdress[j]=contact.getContactEmail();
emailAddressList.add(contact.getContactEmail());
selectedListWithEmail.add(contact);
//Toast.makeText(ContactManager.this, "email:"+contact.getContactEmail(), Toast.LENGTH_SHORT)
//.show();
j++;
}
}
}
if (selectedListWithPhoneNo.size() > 0) {
intent.putParcelableArrayListExtra("SELECTED_CONTACTS_NUMBER", selectedListWithPhoneNo);
setResult(RESULT_OK, intent);
} else if(selectedListWithEmail.size() > 0){
intent.putParcelableArrayListExtra("SELECTED_CONTACTS_EMAIL", selectedListWithEmail);
intent.putStringArrayListExtra("EMAIL_ADDRESS_LIST", emailAddressList);
intent.putExtra("TO_ADDRESS", ToAdress);
//intent.put
setResult(RESULT_OK, intent);
}else {
setResult(RESULT_CANCELED, intent);
}
finish();
//System.out.println("selected contacts:"+selectedList.size());
// Tip: Here you can finish this activity and on the Activty result of the calling activity you fetch the Selected contacts
}
onActivtyResult code in Activity A is,
if(data.getExtras().containsKey("SELECTED_CONTACTS_NUMBER")){
ArrayList<Contact> selectedList =(ArrayList<Contact>) data.getExtras().getSerializable("SELECTED_CONTACTS_NUMBER");
Toast.makeText(ScringoFindFriendsSub.this, "email:"+data.getStringArrayListExtra("EMAIL_ADDRESS_LIST"), Toast.LENGTH_SHORT)
.show(); //i am alwasys getting null here
}
i am always getting null in data.getStringArrayListExtra("EMAIL_ADDRESS_LIST")..
SELECTED_CONTACTS_NUMBER & SELECTED_CONTACTS_EMAIL is coming in onActivityResult but EMAIL_ADDRESS_LIST is not coming, i mean its null
At a glance, I don't think you want an 'else if' here:
} else if(selectedListWithEmail.size() > 0){
If a Contact has a phone and an email, you add him to both lists. But if there's anyone on the phone list, you won't put the email list into the result.

How to avoid if else?

Im trying to do an application that, on the first activity, user can mark several checkbox, from 1 to seven or eight (it depends on final version). When user have marked his options and he click on a "search" button, I want to show a list showing diferent objects depending on the marked items.
My objects are custom objects "ground",it have many characteristics, like strings or int-boolean (1 or 0), stored in a database sqlite.
In my main activity, I use a lot of if else to know how many of checkbox are checked, I put this information into intent via extras, and in my second activity (list) I retrieve this extras and I do another if else to know what list the app must show.
I think this method is such rudimentary because i have to do a lot of if else on both activities.
I have tried to make in the first activity a dbquery returning a list, and send this list via intent, but I have followed a lot of tutorials and always I have a list of correct number of objects but always the same overwrited object!
Any ideas of what to do?
Thank you in advance!
FIRST ACTIVITY
public class MainActivity extends Activity {
Button bafegeix, bcerca, bneteja;
ListView llista;
RadioGroup grup;
RadioButton casa, terreny;
CheckBox cbaigua, cblavabos, cbcobert, cbdutxes, cbcuina, cbadap;
protected Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prova__db);
bcerca = (Button) findViewById (R.id.cerca);
bneteja = (Button) findViewById (R.id.neteja);
grup = (RadioGroup)findViewById (R.id.grup);
casa = (RadioButton)findViewById (R.id.casa);
terreny = (RadioButton)findViewById (R.id.terreny);
cbaigua = (CheckBox)findViewById (R.id.aigua);
cblavabos = (CheckBox)findViewById (R.id.lavabos);
cbcobert = (CheckBox)findViewById (R.id.cobert);
cbdutxes = (CheckBox)findViewById (R.id.dutxes);
cbcuina = (CheckBox)findViewById (R.id.cuina);
cbadap = (CheckBox)findViewById (R.id.adaptada);
bcerca.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(terreny.isChecked()){
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if (cbaigua.isChecked()&& cblavabos.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()) {
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert","cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
} else if(cbaigua.isChecked()&&cblavabos.isChecked()&&cbcobert.isChecked()){
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert","cobert");
startActivity(iterreny);
}else if(cblavabos.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if(cbaigua.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if(cbaigua.isChecked()&&cblavabos.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if (cbaigua.isChecked()&&cblavabos.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
startActivity(iterreny);
SECOND ACTIVITY
public class LlistaTerrenys extends Activity {
ListView llista;
TextView numelements;
ArrayList<Terreny>list;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_list_contactos);
llista = (ListView)findViewById(R.id.listView1);
numelements = (TextView)findViewById(R.id.numelements);
Bundle extras = getIntent().getExtras();
String aigua = extras.getString("aigua");
String lavabos = extras.getString("lavabos");
String cobert = extras.getString("cobert");
String dutxes = extras.getString("dutxes");
String seleccio = extras.getString("noseleccio");
if (aigua!=null && lavabos!=null && cobert!=null && dutxes!=null) {
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
mydb.getAllDataTerrenyTotesCaracteristiques();
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyTotesCaracteristiques());
llista.setAdapter(aa);
} else if(aigua!=null && lavabos!=null && cobert!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabosCobert());
llista.setAdapter(aa);
}else if(lavabos!=null && cobert!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyLavabosCobertDutxes());
llista.setAdapter(aa);
}else if(aigua!=null && cobert!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaCobertDutxes());
llista.setAdapter(aa);
}else if(aigua!=null && lavabos!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabosDutxes());
llista.setAdapter(aa);
}else if (aigua!=null && lavabos!=null ){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabos());
llista.setAdapter(aa);
HERE DIFFERENT METHODS RETRIEVING DATA FROM SQL
public Cursor getAllDataTerrenyAigua () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where aigua = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyLavabos () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where lavabos = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyCobert () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where cobert = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyDutxes () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where dutxes = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyTotesCaracteristiques () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where aigua=? AND cobert=? AND dutxes=? AND lavabos=?");
return db.rawQuery(buildSQL, new String[]{"1","1","1","1"});
}

Android - Passing database and id through activities

I have 2 android activity's
One where I have a room list, i.e. list of rooms.
The other the room contents. Where all the content of a selected room is displayed.
All I want is for the room id(r_id) to be passed through the room list class into the room overview class where I can retreive all its contents from the database.
I've been trying to figure out where i'm going wrong, I think its around line 94 - 101:
selectedRoom = (String) ((TextView) view).getText();
Cursor c2 = sampleDB.rawQuery("SELECT * FROM tbl_roomDesc WHERE roomName =?", new String[] {selectedRoom});
if (c2 != null ) {
if (c2.moveToFirst()) {
do {
r_id = c2.getString(c2.getColumnIndex("r_id"));
}while (c2.moveToNext());
}
}
c2.close();
Can anyone help me figure out what i'm doing wrong here?
Here is the source for the room list
Here is the source for the room overview
Thanks in advance!
To pass data between to Intents, use the putExtras() method.
Example:
Intent i = new Intent(...);
i.putExtras("roomid", "10");
startActivity(i);
final int roomId = r_id;
next = (ImageButton) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CreateBuilding.val = "Retrieve";
sampleDB.close();
Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class);
intent2.putExtra("roomId", roomId);
setResult(RESULT_OK, intent2);
finish();
startActivityForResult(intent2, 0);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
int roomId = data. getIntExtra("roomId", 0);
}
}
}
If I have not misunderstood you put the roomId inside the Intent and retrieve it inside the onActivityResult

Android Code - Radom number generation for calling activity [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
How can I generate random number in specific range in Android?
Here is my scenario
From the Mainactivity ; On click of a Button i want to generate a random number from 1 to 4
Based on the output, i want to write an if-else that will call 4 different activities
So on click, if 4 is generated, then call activity 4
Next time 1 can be generated and should call activity 1
and so on ....
Can someone please help me with this code?
myBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent;
if (index == 4) {
intent = new Intent(Activity.this, Activity4.class);
}
else if (index == 3) {
intent = new Intent(Activity.this, Activity3.class);
}
else if (index == 2) {
intent = new Intent(Activity.this, Activity2.class);
}
else
intent = new Intent(Activity.this, Activity1.class);
startActivity(intent);
}
});
public void test1(){
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent = null;
switch(index){
case 1: intent = new Intent(this, Activity1.class);
break;
case 2: intent = new Intent(this, Activity2.class);
break;
case 3: intent = new Intent(this, Activity3.class);
break;
case 4: intent = new Intent(this, Activity4.class);
break;
default:
Log.e("ERROR", "");
return;
}
if(intent != null){
this.startActivity(intent);
}
}

creating array list and passing it from one activity to another

I am a student and new to programming. It might be a simple error, can anybody help me fix it.I have to pass an array list from one activity to another. In this activity i have 5 radio buttons RB1, RB2.... and i want to pass the content of news[] it to another activity called display.
public void onClick(View v) {
String[] news;
news = new String[5];
news[0] = "bbc";
news[1] = "guardian";
news[2] = "yahoo";
news[3] = "sky";
news[4] = "fox news";
final ArrayList<String> arr = new ArrayList<String>();
if (RB1.isChecked() == true)
arr.add(news[0]);
if (RB2.isChecked() == true)
arr.add(news[1]);
if (RB3.isChecked() == true)
arr.add(news[2]);
if (RB4.isChecked() == true)
arr.add(news[3]);
if (RB5.isChecked() == true)
arr.add(news[4]);
if (v == Done)
{
Button done = (Button) findViewById(R.id.DONE);
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent myIntent = new Intent(Read.this, display.class);
myIntent.putExtra("pass",arr);
startActivity(myIntent);
}
});
}
the codes for next activity is as follows
Intent myintent = getIntent();
String[] Array = myintent.getStringArrayExtra("pass");
for (int i = 0; i < Array.length; i++)
Log.v(LOG_TAG, "THE website Is :" +Array[i]);
I am geting a java.lang.NullPointerException in the above two line i.e
for (int i = 0; i < Array.length; i++)
Log.v(LOG_TAG, "THE website Is :" +Array[i]);
can u pls tell me why ?
thanks in advance
First of all, some conventions: Start names of variables lowercase, so array instead of Array. This will save you from some confusion later.
Try it as follows, from this thread:
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
and
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Use Bundle like this:
Bundle bundle = new Bundle();
bundle.putStringArray(key, news);
myIntent.putExtra("pass",bundle);
startActivity(myIntent);

Categories

Resources