i want to show this array as a listview in a new screen when a button is clicked.
ArrayList<String> favorite = new ArrayList<String>();
this ListView is a small part of my class. i cant seem to figure out how to implement it with my code (i can figure out how to create a listview in a separate application, and set the onitemclicklistner just for that listview)
i want to display that listview when.
case R.id.ShowFavButton:
Your question isn't entirely clear.... but you would build a separate Activity, possibly subclassing ListActivity (documentation here), and then load it when you click on the button. If your Activity was named FavoritesActivity, it would be something like this:
Button fav = (Button)findViewById(R.id.ShowFavButton);
fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, FavoritesActivity.class));
}
});
If you want to return something from your FavoritesActivity to your FirstActivity (or whatever it is called), you can use startActivityForResult instead of just startActivity.
Related
I am doing a project in Android Studio. I will have 2/3 activities at most. On main activity I will have some buttons. There will be a second activity. So for each button that second activity will pop up and show some more buttons or imageButton but they will be different. The buttons on the second activity will also be meant for different actions but all of it will be on a third activity. The third activity will have some images and a text view. So image view and text view will show different data from the "XML resource" for different buttons. Any ideas how I can do it?I am using Android Studio.
Can it be done using base Adapter?
Also when all the data are stored on sql database, what different thing do I need to do?
Thank You!
When you go from main activity to the second activity, you must be using intent.
You can add extra info in your intent to to modify accordingly using putExtra()
Then in your second activity, you can simply retrieve the extra info and make changes accordingly.
in main activity,
//your first button
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load first set of images");
startActivity(newintent);
}
});
//your second button
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load second set of images");
startActivity(newintent);
}
});
in second activity,
String extrainfo=getIntent().getStringExtra("details");
if(extrainfo.equals("load first set of images"))
{
//make your changes accordingly
}
else if(extrainfo.equals("load second set of images"))
{
//make your changes accordingly
}
else
{
//make according changes
}
This is how you can achieve it.
Hope this helps!
This might sound a bit convoluted.
I have roughly 15 Spinners in one activity and made a distinct method for each of these spinners. I then initiate the methods in the onCreate method.
Method example:
//Relative Position Spinner
public void relativePositionSpinner() {
Spinner relativePositionSpinner = (Spinner) findViewById(R.id.spinner_relativePosition);
ArrayAdapter relativePositionAdapter = ArrayAdapter.createFromResource(this, R.array.relativePosition, R.layout.spinner_item);
relativePositionSpinner.setAdapter(relativePositionAdapter);
//what happens when selected
relativePositionSpinner.setOnItemSelectedListener(this);
}
OnCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_hand);
//initiate all Spinners
relativePositionSpinner();
absolutePositionSpinner();
etc.
Now what I want is to send the data of each Spinner to another Activity with the click of a Button. I know that I can do this with an intent and using putExtra in the Button method like this:
public void openHandSummary() {
//Find the Button that gives option to enter new hand
Button handInputButton = (Button) findViewById(R.id.hand_input_button);
//set a click listener on Hand Analyzer Button
handInputButton.setOnClickListener(new View.OnClickListener() {
//below code will be executed when the new Hand Button is clicked
#Override
public void onClick(View view) {
Intent handSummaryIntent = new Intent(NewHandActivity.this, HandSummaryActivity.class);
handSummaryIntent.putExtra("RelPosString", WHATTOENTERHERE??)
startActivity(handSummaryIntent);
}
});
}
However I do not know how to retrieve the value/variable out of my Spinners to put them into the Button/intent method? Because if I make a String in the Spinner method, then I can't access this in the Button method.
So I feel like I have too many methods? So is there a way to pass data from one method to another method, or do I have to cancel some methods? What would be the easiest way to set this up?
I also made an onItemSelected to make some toasts, which worked. Can I use OnItemSelected somehow to create variables or initiate a data transfer to another Activity?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
switch (parent.getId()) {
case R.id.spinner_relativePosition:
makeText(NewHandActivity.this, "Relative Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner_absolutePosition:
makeText(NewHandActivity.this, "Absolute Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
I'm very new to coding, and I just can't figure out the logic how I get the Spinner methods, Button/iniate method and OnItemSelected method to work together and exchange variables. Would appreciate if someone can point me in the right direction. Have already browsed the internet a day or so to find an answer, with no success.
I like your idea of separating out the code to create each spinner into its own method. However, if you are copying and pasting the whole method and making a few changes, you should step back and think about how you can do it even more easily. Often the changes you make after copy and paste give a hint that you should add some parameters to the method. If you do it correctly, you can write just a single method and then copy and paste the method call instead of the entire method and then make appropriate changes to the arguments.
As for your actual question, you are making this much more complicated than necessary. Specifically, you do not need to setOnItemSelectedListener() on any of the Spinners. Instead, the OnClickListener for the button should just get the selected item from each spinner and send it to the new activity in the Intent.
I have a three different activities all of whom switch to a common Activity via Intent, with only minor modifications in the appearance of the child or switched-to Activity. I want to change the text of the TextView, so that each time the child activity displays a different text based on which activity it was switched-to from.
I have tried using this:
Button startSendingLocation = (Button) findViewById(R.id.wmb_start_sending_button);
startSendingLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ll_wmb_Map.class);
startActivity(intent);
TextView bmo_map_toolbar_title = (TextView) findViewById(R.id.wmb_toolbar_title);
bmo_map_toolbar_title.setText(R.string.ll_bmo_toolbar_title);
}
});
But this doesn't update the text in the TextView. Am I doing it wrong ?
Should I create three different Activities with their own layouts to achieve this ?
Will I be able to add or update a few views, differently, based on the activity I am switching from ?
I thought switching to the same activity via Intent will be a good idea for code-reusability and some efficiency gains as the child activity makes use of a MapFragment in all the three cases.
ll_wmb_Map.class : The common activity that is being switched to, using Intent
wmb_toolbar_title : The id of the TextView I want to change
ll_bmo_toolbar_title : The text I want to set in the TextView
Fine, If you just want to enable/disable the view and change the textview content, you just need to use enable and disable functionality within onclick listeners instead of calling the same activity again and again.
startSendingLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bmo_map_toolbar_title.setText(R.string.ll_bmo_toolbar_title);
//enable/disable the views
}
});
I have a list of buttons in one of my activities that are dynamically generated, and I was wondering how i would get one of those buttons to, when clicked, open another activity and display text based on which button in the list was clicked.
I generate the buttons using a for loop (I've ommited details relating to TextViews in the loop for easier reading, it also used some variables defined elsewhere)
for (int i = 0; i < N; i++) {
// create a new Button
final Button rowButton = new Button(this);
// Set properties of rowButton
rowButton.setText("See Recipe");
rowButton.setId(RecipeArray.get(i));
// add the Button to the LinearLayout
myLinearLayout.addView(rowButton);
// save a reference to the Button for later
myButtons[i] = rowButton;
}
The buttons represent a certain recipe and when clicked they should take the user to a new activity "HowToMake" and generate a textview with the information relating to that recipe only. They are stored in an array at the bottom of the code snippet "myButtons[i] = rowButton" But I'm not sure how I would use this.
Thanks for any help.
You'll have to add an onclicklistener to each button as you add it. Then in the onclick event, you can call the startActivity method of your current Activity. When you create your Intent which will open the new activity, you can add "extras" to it (in other words, you can add some extra data which will be passed into the new activity). New data, such as the ID of the recipe that you want to open :)
Example (Untested, but should be along the right lines):
rowButton.setTag("the unique ID by which you can read back your recipe");
rowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(YourActivity.this, TheViewRecipeClass.class);
intent.putExtra("id", v.getTag());
startActivity(intent);
}
}
I want to show an empty list view, which is then populated by user input. I have the UI flow working, and I populate a list of my custom objects after the user enters some information via a view which is invoked through setContentView (i.e. no a new Activity).
I take the input and add it to a list, which I want to be summarised on the ListView. However, whenever I add to the list and/or the ArrayAdapter and call adapter.notifyDataSetChanged() it does not do what I want. The ListView is still empty. Argh! It's driving me insane!
#Override
public void onCreate(Bundle blah) {
ListView listView = (ListView) findViewById(R.id.results_list);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_add:
final Activity act = this;
setContentView(R.layout.record_details);// the sub-view that takes the user input
// the button on the form to 'add' details:-
((Button) findViewById(R.id.recored_details_add_btn))
.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
}
}
);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
break;
}
return true;
}
Please, save me from my misery and inform me of my stupidty?
Thanks in advance.
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
Is it possible that this setContentView in the onClick handler is creating a new instance of the list view widget (with no adapter) or reinitializing the list view (clearing the adapter)?
Try putting something in the list initially in onCreate and then see if it disappears when you hit the button.
I haven't seen any code (although I'm a relative newbie) that switches views within the activity's lifetime to bring up essentially bring up different pages - most use a separate activity.
Edit:
OP asks:
Thanks...So how can I get what I want? The list I'm backing the adapter with is static; should I just use activities instead and rely on onCreate loading from the static field?
Some options:
Use separate activities
Re-associate the adapter (call setAdapter again) - probably a bad idea
Declare both layouts in the same file. You'll hide one and unhide the other to switch between views rather can calling setContentView. This is similar to how ListView layout works (one for when the list is empty and one for when it is not). I think I've seen an example of this somewhere on the net, but I don't have a reference right now.
You could relaunch the same activity by using Intent.FLAG_ACTIVITY_SINGLE_TOP flag while creating the intent and override the onNewIntent() method.
Inside the onNewIntent() you create the adapter with updated data and call setAdapter.
I think this will give you the intended behaviour.