OK I've been online trying to figure this out, and piecing together bits of code here and there. I still don't understand. All I want is to display three list items and on item click it would go to a new activity. What do I need to fix?
Edit:
I made a stupid mistake. Fixed now - thanks everyone!
I'm going to guess that you should replace this:
String item = (String) getListAdapter().getItem(position);
with this:
String item = view.getText().toString();
getListAdapter() is a function for a ListActivity but not a regular Activity.
Are you sure that you have your new activity classes in the android manifest? You need to add each activity you are going to launch to the manifest.
Check your manifest.xml and declare the activities and you should be good to go. Also change the way you get the info from the listener to so that you getString from the item instead of using the position. That way you can control exactly what is passed to your intent by specifying the textview on the item.
Sam and El Duderino both have valid points, but just as an amusing point nonetheless:
You're checking to see if the items are "Economy" "Basic" "Professional", etc., but your String array has only the months of the year ... :P
Also, when setting the Intents, you should not be using this, because you are in the onClick event, which means this is an onClickListener and not an Activity. So use atcList.this instead of just this.
You never set the onItemClickListener to the listview:
myList.setOnItemClickListener(this);
Related
I want to toggle a SwitchPreference from another activity (Main Activity).
I have an activity where I ask the user if they want to enable notifications. It contains two buttons and they either click yes or no, as shown below:
I want the answer to be saved and the SwitchPreference to either be toggled, On for yes or off for no. (as well as the SwitchPreference state)
I'm really stuck on how to do this.
Any advice or answers would be most appreciated?
You've got a couple options.
You could do as #0X0nosugar suggested where you simply store the new setting value in SharedPreferences and check the value when resuming the previous activity.
You could use startActivityForResult(intent, SOME_CONSTANT_IDENTIFIER) (vs just startActivity(intent)) in order to have your settings activity return a result to your calling activity. The concept here is that you're starting a new activity which will return a result back to the previous activity. You could then check the value, store it where needed, and update any relevant UI items. Here's an article on how this works https://developer.android.com/training/basics/intents/result.
I am new to android/java programming. I have two class, one is an activity and other normal class. In my activity class contains TextView. Can i update my TextView of one class from a editText(that the user enters) in a another class. I tried with random code, but it fails. Please help I've been looking forever
You might update the TextView from wherever in the java code by referring to the
findViewById(R.id.some_text_view_name).
Some what like this:
TextView textViewName = (TextView) findViewById(R.id.some_text_view_name);
textViewName.methodName();
Here methodName() refers to the Public methods listed here
Hope it helps. :)
You can start your second activity using startActivityForResult() instead of startActivity(). In the second activity you can set the result and its status using setResult() and get back to previous activity (via back press or something). In the first activity, this result will be received in onActvityResult(). From here, you can get the data set by second activity and update your textview.
This is the gist of what you are supposed to do. You can get code example here, here and here.
In the application I am building I have 2 listviews with, among other elements, 2 buttons on every row, one for eliminating that row and the other to pass to another activity using an Intent.
I am detecting clicks on both buttons by setting click listeners on both buttons on the getView method of the Adapter class.
The first button was something I couldn't figure out, because I needed to identify in which listview the button was clicked and the position of the clicked row which I couldn't realize how to do.
The second one I thought would be easier since all I needed to do was Intents.
However I needed to call a method from the activity class (had to instance it) since I couldn't make Intents in a non-activity class.
This last one threw a NullPointerException.
Will post all code & logcat in a while, be right back, any possible help will be appreciated.
I will need to see your code, but basically what worked for me for being able to call intents within the code of the adapter was to use the host activity.
Something like this:
item_new = (TextView) convertView.findViewById(R.id.my_view);
Activity host = (Activity) item_new.getContext();
Intent intent = new Intent(host, MyActivity.class);
host.startActivity(intent);
If you post the code maybe I can give more details
I'm not figure out the entire problem, but if you want to start an Activity from an Adapter you could simply pass the current activity to the adapter constructor (or cast the getContext() to Activity, if you used that as context of your adapter)
In a line of code:
Activity.class.cast(getContext()).startActivity(intent);
I am new to android and was starting out by making a list of button.i want when each button click launch a same activity for them with diffrent resource such image and raw file
what is big deal here?
call same intent twice :)
refer how to start new activity here
You can the same activity/intent multiple times, just if you want the code in said activity to do something else you might want give it some extra information by using intent.putInt(<Key>, <Value>); and others
I have an app that uses this RibbonMenu and I wanted to know if it was possible to use the menu items to change the ContentView of the Activity instead of another activity to reduce the size of my app. I've tried doing this already,and it changed the content view perfectly, but i was not able to press the home button. I'm in a bit of a rush here, but i'll try to post code if anyone asks for it. thank you!
Menu items is UI element. It does nothing by itself. It however can trigger some more actions in your code. As for changing activity layout, yes, you can call setContentView() at any time you want.
to reduce the size of my app
this is not the way optimalizations should be done