Checking the checkbox in recyclerview from a different activity - android

[Asking question for the first time on Stack Overflow]
I am making an application where the user will be shown with a list of locations in a recyclerView. RecyclerView has generic rows and each row has a cardview in which there is a checkbox and a textview. When the user clicks on a row, he is re-directed to a different activity where they are supposed to answer certain questions and click on save. Once clicked save, he will be re-directed to previous activity and I need that particular location's checkbox to be checked.
How can I do this?
Thanks for the help and sorry if I explained it badly.

Welcome to stack overflow. Have you read through the android documentation found here:https://developer.android.com/guide/ you are going to want to read through the activity and adapter sections of that. Also you can refer to a summarized version here https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView#attaching-event-handlers-within-adapter
There are many ways to accomplish what you are asking. Sounds like you are looking for startActivityForResult() this method simply takes an intent and says "hey when that activity is finished I want some information back so I can do something with the result"
When you want to pass a result back to your calling activity in this case, you will need to set some data before you finish the created activity like so
Intent data = new Intent();
data.putExtra("location", selectedLocation);
setResult(RESULT_OK, data);
finish();
By doing this you can receive the result in your calling activity with the method implementation of onActivityResult(int requestCode, int resultCode, Intent data)
In your case lets say a user taps a card and you start the new activity using startActivityForResult(intent, requestCode) when the user is done with this activity and the activity is going to finish you want to set some outstate data using the Intent data passing method I showed above. Maybe you want to send the location back. Simply attach the location as an extra and receive it in your onActivityResult() implementation and then make the appropriate changes in that view based on the data. Hope that helps.
In the future, it always helps to provide example code with your question. It will help users better assist you on your issue and more importantly make users want to help you out. You seem new to android development so make sure to read through the android docs whenever you can to better your understanding of android as a whole. Cheers!

Related

Sample code for list/detail Android Activities

My understanding is that best practices for a simple app with list and detail Activities would be a startActivityForResult(). Let's say I have an app with model Product (id, name, price) and ProductListActivity and ProductDetailActivity.
I am having a hard time understanding which of the activities would contain startActivityForResult() and which would have setResult() and what the result would be.
My understanding is that best practices for a simple app with list and detail >Activities would be a startActivityForResult().
I would not agree with that statement.
In my opinion, the best way would be to set up a [onListItemClickListener](http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)) in your ProductListActivity.
When an item in the list is clicked, the onListItemClick() method will be called, from the arguments you'll be able to figure out exactly which item in the list was clicked (for example using the second argument - position, which is the position of the clicked item in the list, starting from zero).
Once you figure out which item was clicked, you'll create an Intent and put the required extras in there (for example the position of the clicked item).
You'll then use that intent to start your ProductDetailActivity, by calling startActivity().
Then, in your ProductDetailActivity you'll get that intent using getIntent(), extract the extras from that intent and then use them to decide what to display in your activity (for example, display some text associated with a position in your listview from the ProductListActivity). You can get extras from an intent using getExtra*() methods.
You would use startActivityForResult() if you wanted to get some result back from that activity, which in your example doesn't seem to be the case, you simply want to display some details. Now imagine that your activity did some calculations and you wanted to get the final value back, then using startActivityForResult() would be one of the ways of passing that value back.

How can I pass data from one activity to another

I tried to look up something similar to what I want, but I can't find anything...
I want to pass data that I received from my database (echo json_encode($response); ) from one activity to another. The data is displayed in a listview and by clicking one item (in this case a 'category') it will take the user to second activity that displays a list of books (displayed in a listview).
My goal is to display results from received data (selected 'category' from first activity and selected 'book' from second activity) in a third activity (in here the user will have information about the book).
Any help will be great - a link to a tutorial that can help me solve this problem (well problem for me because I am new to all this), an example code or suggestions on how can I look it up...anything.
Thanks :)
You can use Intent for passing data from one activity to another activity. If you want to pass multiple types of data from one activity to another, Bundle will also be another choice for you. Look for tutorials regarding passing data through intent or bundle from one activity to another activity
Here is an Example to get you started
Intent i = new Intent(getApplicationContext(),toActivityName.class);
i.putExtra("Key","value");
startActivity(i);

Efficient way of finishing and updating the specific previous activities

As I mentioned in the title, I have two questions.
1) I have user details' form in three activities.
Activities Heirarchy:
ActivityOne ---> ActivityTwo--> ActivityDetails1---> ActivityDetails2---->ActivityDetails3---> ResultActivity.
In this hierarchy, details form starts from ActivityDetails1. Submit button is in ActivityDetails3. So when I click submit button, I am submitting all the details that are entered in 3 activities to a database.If the submission is successfull, I am going to other activity(ResultActivity) by intent.
If it fails it stays on the same ActivityDetails3. Whenever submission is sucessfull, I need to finish the 3 of the ActivityDetails activities besides going to other activity. For this I am finishing these 3 activities by accessing contexts by making them static as the marked answer in this link. But this seems inefficient as the static contexts may cause memory leaks. Can some one suggest me an efficient way to do this?
2) After the submission is successful, I have to update even ActivityOne and ActivityTwo apart from going to other activity(ResultActivity) by setting the actionbar items title with one of the details submitted by the user. I am able to make the later activities update the title, but not the previous activities. Can someone please guide me how to refresh the previous activities which are in stack, so that my title of the action item on the actionbar would be changed on previous activities too.
I would be thankful, if someone can help me on my above two questions. Relevant code snippets are appreciated.
Dont type any large paragraphs.Because no one had a patient to read a large paragraph
Type your issues,problemms,what you want in brief,simple,easy to understand.
Refer the below scenarios
Activity A and Activity B.
From A go to B.If B do some operation and finishes then the result is updated in A.
In the above scenario you use following techniques
Put protected void onActivityResult(int requestCode, int resultCode, Intent data)
and handled the updation after exit of B
in A and in B before call to finish() set the result code as you required

Using Primitive Data Types in another Class and the res/menu/.xml file

I'm a very new to Java. I thought I was doing okay but have now hit a brick wall, so to speak.
The idea is I use the 'home button' '_menu' for the user to choose one of 26 formats. The format they choose has 3 variables. Those 3 variables are then used in the first xml view, along with 2 user inputs to calulate another variable. This variable is then used in a second xml view, along with a third user input here, to calculate the final answer.
I have created the 26 choices and if for example I choose option 5, on the emulator screen I see all the correct values associated with this choice. I don't think those values are getting stored anywhere. I say this because if I come out of that view and return back into it, it's not showing, as in my example, choice 5. It's showing its initial state, as though I was going into it the first time. I assume it's something to do with launching this activity from the start but is there anyway around this. Or really where do I start.
My second question is with the integer variables that I created from this choice. I need to pass them into another java file for the first set of calculations. I've tried to pass the variables/data with the, 'new intent putExtra' but can't get it to work. But I don't think I can use this anyway since the I don't want to launch the second view directly from the res/menu/ .xml view.
Not sure if this is making sense to anyone. Can someone help point me in the right direction?
I don't quite understand your first question, but it sounds like you're launching a new activity and when you quit and come back to it, everything is reset. If you're launching a new activity after selecting the options from your phone's menu button, you should implement a method that saves data to the shared preferences of the main activity. This method should be called on the activities onPause(), onDestroyed(), or onStop() method. You can also add a method on onResume() where the activity checks if there's any data stored in shared preferences and if so, make the desired changes.
As for your second question...I kinda don't understand it either. new intent and putextra is used when you're starting a new activity and want to pass data to it. Views are not "launched" they are only inflated and brought on display whenever you want. I did an app once where I had everything in one activity and just using setContentView() method all the time. In the long run, it just complicated everything. It is easier to keep things simple and launch activities. Here is an example of some variables being passed to a new activity:
On my main activity (FirstActivity) I have:
(when newActivityButton is clicked)
case R.id.newActivityButton:
Intent mIntent = new Intent(FirstActivity.this,SecondActivity.class);
String[] luckyNumbers = {
luckyNumber[0].getText().toString(),
luckyNumber[1].getText().toString(),
luckyNumber[2].getText().toString(),
luckyNumber[3].getText().toString(),
luckyNumber[4].getText().toString(),
luckyNumber[5].getText().toString()};
mIntent.putExtra("luckyNumbers", luckyNumbers);
mIntent.putExtra("message", messageField.getText().toString());
FirstActivity.this.startActivity(mIntent);
break;
luckyNumbers[] is an array of textviews.
Then on my NewActivity onCreate(), I have:
message = getIntent().getExtras().getString("message");
Log.i("TAG", message);
luckyNumbers = getIntent().getExtras().getStringArray("luckyNumbers");
cv.setLuckyNumbers(this.luckyNumbers);
cv.setMessage(this.message);
where cv is just a custom view I created with its own methods

Application crashes when user presses Back Button

Good day all, First of all, maybe i went about this the wrong way in the first place. Situation is, i have a listview that when an item is clicked, it creates an intent and calls startActivityForResult() to another activity, to be able to edit the entries in the row. and that other activity after editing the required values, returns back to the calling activity, when the user clicks on a save button. The activity then uses the row Id to update the item.My problem now is that, if the user presses the BACK button instead, the application crashes. i have been looking around and see solutions like using Shared Preferences or onSavedInstanceState(), but i don't know where exactly i should be putting the code? Any help as usual, will be greatly appreciated. Thank you.
p.s: when i look at logcat, its gives a NullPointerException for this line in onActivityResult.
Bundle result = data.getExtras();
If you are assuming you will have a result on the parent thread if the user quits the activity via Back then that is most likely the issue. Try testing if result is null before attempting to use it.

Categories

Resources