I am trying to open a list activity in two ways, (both from different activity A)
In one method I use putExtra(String name, String value) to populate the listActivity with sqlite.
This works perfectly awesome.
But now with another button, I am trying to open this list activity without putExtras (just viewing it) but i get a Force Close with Null Pointer Exception for this,
Can any one provide me some solutions for this, thanks in advance...
In your List activity, you're doing something like this yeah?
String passedValue = getIntent().getStringExtra("key");
In the case when you don't putExtras() , passedValue is null, so trying to use this string will give a NPE. So if you want to use passedValue, you might want to do this:
if(passedValue != null)
doSomethingWith(passedValue);
Related
How do I get getString in fragment, I have tried to solve it with snippets of code on this but it did not work to tap
chronology
-ok so in the uploaded image there are some 6 items in the recyclerview that line up the grid, I try to make it to the String of the name as I have included the code but the code I press [commant + tap] displays the message {public statuc int} , but it can't be tapped to go to the next page, but if I try to use the usual string as I have included the code, it works on the next page
get an error public static int
dataProduct.add(Products(R.drawable.ic_electricity, getString(R.string.electricity), true))
I've looked for a solution in this forum but it still hasn't worked, but if I use the code below it works
dataProduct.add(Products(R.drawable.ic_electricity, "Electricity", true))
maybe I'll send a picture, below, in this case I will take the string but can't be tapped like the image below
image getString
getString() is a method that comes from a context. In a fragment, this context is commonly provided by the activity hosting the fragment. To use the activity to get a resource, just call getActivity() (which in Kotlin can be written just as activity):
activity.getString(…)
First of, I'm a noob in Android development....
So, my problem is - I have a Activity with a EditText view, so someone can put his name in it.
And what I want to do with that input - I want it to be displayed in all activities in TextViews where it needs to be.
Example - Like in a book -> Input name: What are you doing kid? Kir: "I don't know..." Input name: "Just don't do it anymore..."ect.
And then in the next Activity by clicking a Button, the same, I want that same input name to display in ALL ACTIVITES , TextViews where it needs to be, but I don't know how to declare it, where to declare it, what code ect.
I hope you all will understand what i mean.
And thank you for your support.
One way is by passing data into intents before starting the activity.
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("key_name", "John Cena");
context.startActivity(intent);
Then retrieve the data in MyActivity by:
Bundle extras = getIntent().getExtras();
String name = extras.getString("key_name"); // name should be John Cena
Simillar functionality also exists for Fragments.
Basically you wants to Preserve the Value\Data. For such scenarios, it is best to use [Preferences] where you can save your data on disk in a private file and read it anytime and anywhere.
Android Preferences
I have two activities i.e. MainActivity and NewActivity.On click of button in MainActivity,i am moving to NewActivity using Intent and passing some data using intent to NewActivity.
intent.putExtra("PUBLISHEDAT",newses.get(position).getTitle());
In NewActivity,i am checking whether the data received from Intent is null or not.If it is null i am setting the TextView visibility to GONE.But textview won't disappear and displays null even if data received from intent is null.
if(getIntent().getStringExtra("PUBLISHEDAT")==null || getIntent().getStringExtra("PUBLISHEDAT")==""){
publishedAtTextView.setVisibility(GONE);
}
Try this:
getIntent().getStringExtra("PUBLISHEDAT").trim().equals("")
or
getIntent().getStringExtra("PUBLISHEDAT").trim().isEmpty()
You should not use == to check for empty string. == compares the reference, not the value. Use this:
getIntent().getStringExtra("PUBLISHEDAT").isEmpty()
I think in putExtra part you put wrong value, maybe "null" as a string !
Check that with some hard coded values, like
"" "Null" and "Test" check your main activity, because in new activity you did it right
I still am a beginner in Android development and will try to make my question as clear as possible with a schema of what I have in my mind.
Purpose of this application:
- I want the user to have the choice between a few buttons and when clicking on any of them, it would open a list view with different content according to the button.
ex : if you click on "Category_1" button, only elements with a fitting id will appear in the listview.
So far, I have :
- defined my "handler" class (extends SQLiteOpenHelper) : name/path of DB, definition of CRUD, .getReadableDatabase, etc.
- define a class for my table, in my case "Restaurants.java" with getters/setters and constructor.
- defined my MainActivity with empty listeners for my button.
- defined my "DatabaseAdapter.java" in which I want to define the methods/sql requests which will communicate with the database and get the information I want from it.
- defined my ListViewActivity with nothing to display so far.
Here is a schema of what I want with the idea of how to make it to try to optimize my application :
To sum up:
- I want a listener for each button setting a variable to a certain value (for example: "if you click on 1 then set the value of A to 1") and opening the ListViewActivity.
- There would be a method defined in "...Adapter.java" sending a request to the database and having the variable A defined earlier as an input.
- And then, when clicking on the button, the ListViewActivity will open and call the method from "..Adapter.java", and finally display the results.
So, here are my questions :
- First of all, is the design optimized enough to allow my application to run fast? I think it should as all the button open only one activity and there is only one method defined for all buttons.
- I have a hard time defining the method in "...Adapter.java" which will be called from my ListViewAcitivity. The input should be the variable obtained when clicking on the button but I don't really know how to get a variable in one activity, open a second activity where to display results by using the variable in a third activity... :s
Is it fine to set a variable to a certain value when we click on a button and use this variable in another class as an input for a method?
public findNameInTable(int A){
string sql = " select NAME from MY_TABLE where CAT1 = " + A;
c = database.rawQuery(sql, null); }
Thanks in advance for any indications, suggestions or links which could help me to make my application come true, and sorry if some questions really sounds newbie, I am starting !
Have a good day !
Part 1: The best way I have found to pass variables to other activities is with a putExtra(String, variable);. Say you change the variable name on a button press, you can then call:
YourNewActivityClassName var = new YourNewActivityClassName();
Intent i = new Intent(context, YourNewActivityClassName.class);
i.putExtra("name", name);
startActivity(i);
Then in the activity you just created, you can call this in the onCreate method:
Intent i = getIntent();
final String name = i.getStringExtra("name");
Of course this is assuming the variable was defined as a String before the putExtra was called.
If you want to use other variable types, there are different get***Extra commands you can call like getIntExtra(int, defaultval) but the putExtra will still be used to send it.
Part 2: For calling a method with a variable assigned in a button click, I have found the best way to do this is with a "holder class" this holder can be defined as a final, and a button press assigns a value to one of it's slots. Here is my holder for Integers:
public class holder {
int to;
public void setTo(int to){
this.to = to;
}
public int getTo(){
return to;
}
}
I instantiate my class as final within my on create final holder hold = new holder();
then call my hold.setTo(int); within a list click listener. When I want to get the data, I simply call hold.getTo(); and I have my integer.
Heres a similar post: Pass value outside of public void onClick
Hope this helps!
Mike
I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.