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);
Related
Hi im currently building my First android app and i have a problem that i cant solve this past 3 days. so i have MainActivity and on it there is a Fragment with Recyclerview and ArrayList< Item > listOfItem, on this Fragment there is a Floating button that when i click it will take me to SecondActivity there is a edit text on this SecondaryActivity that i have to fill then i will pass the data back to listOfItem.My problem is what method can i call on SecondActivity to create/add an listOfItem when i go back to MainActivity Fragment? I dont want to make Adapter and listOfItem to be static. Is there a way? thanks
Simple, You can achieve this by using Shared Preference.
What you need to do is just save that Edit Text data to Model(POJO), save that Model to Preference and in your Main Activity - Fragment on Resume method show updated data from that Preference.
Yeah here you need to take care of
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
For more convient and efficient handling of data from any activity or fragement to any activity or fragment, you can use Event Bus.. Here is link where you will get the details information: https://github.com/greenrobot/EventBus
The best solution would be to start the second Activity for result.
startActivityForResult(ActivityBIntent)
Then checking the result overriding
onActivityResult() on the first activity and updating your list/adapter from it
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.
I'm learning android development and I need a hint on the inner working of the whole thing.
I'm using the code showed here
In
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
I want to know what "this" is
In the guide I read:
A Context as its first parameter (this is used because the Activity
class is a subclass of Context)
What does Context class do? How is it used? Why does Activity inherit from it?
Now the main question:
If you check the whole example, they start the other activity directly from the button with the sendMessage() method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?
And, is it really necessary editing the android manifest file by hand?
They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing every time I add an activity? I would like to edit the AndroidManifest.xml file more conscientiously, knowing what I'm typing in and why. In that guide all is put up mysteriously and they don't explain nothing.
I want to know what "this" is
To understand this, see What is the meaning of "this" in Java?.
What does Context class does?
A Context is the glue between your app and the operating system. It allows you to access resources on the device, such as images and databases.
If you check the whole example, they start the other activity directly from the button with the sendMessage method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?
android:onClick="sendMessage" in the XML for the button is a listener for the OnClick event. You can do whatever you wish in this method, including initializing variables.
And, it's really necessary editing the android manifest file by hand? They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing everytime I add an activity?
Yes, every activity must be registered in AndroidManifest.xml with an <activity> tag. At this point, it is probably unimportant to understand all the nuances. I suggest following the examples you see when you want to add more activities. Note that typically only one activity will have an <intent-filter>. Don't worry too much about these until you need to learn about them later.
I am working on my hobby project to learn more about android programming and stuffs. So i recently ran into a simple problem but couldn't find a solution for it.
My project for now have 2 activity [MainActivity], [CommentActivity].
In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. Everything works fine there. It will fetch all the comments and total comments for a certain post based on the post id. In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview.
When a user posted a comment on CommentActivity, it will save into the database via AsyncTask.
Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). So it will never reload the MainActivity again when back button is pressed. Also it will never update the total comment in the MainActivity. What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database.
Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity.
I believe there must be some easy way to do this. I can post codes if you guys need it to assists me.
Thank you.
Yes, there is an easy way.
Start your sencond activity using:
startActivityForResult(intent, RESULT_CODE)
use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database
override onActivityResult() in your first activity to check when the second activity returns properly
update the first adapter as needed.
Hope it helps.
In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity.
If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data
I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.