Update list view from 2nd activity | Android - android

hi i have RecyclerView in that when user click on one row i am making changes in ui and updating view now i want when user click one one icon of that row it opening 2nd activity in that i am passing object of that position . and on 2nd activity if tick it i am not able to update view of that RecyclerView .. any way ? ya by doing static arreylist i can do it but cant do static as there is some problem .. part form static other way
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent n = new Intent(mContext, PlayerInfo.class);
Players mData = (Players) v.getTag();
((Activity) mContext).startActivityForResult(n, 1111);
}
});

As you code shows,you can start second activity by call startActivityForResult and pass your data to it.
when data changed in 2nd activity,you save the new data in one object and when the activity finishes,you pass it to first activity,and you also need to add some code to handle the new object from 2nd activity in onActivityResult,just copy data to your old data for specific position.

Related

Android show single item from a list in next page

I am new to android and developing a mail like application. In the application, from a JSON string from server creating a list ( creating rows in a table view, which is embedded in a scroll view ). Now I am trying to show a single item in detail on clicking an item.
On click, getting the id of the corresponding item and load new page using the following page. My code looks like
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("id", 124);
b.putInt("message", "Message");
intent.putExtras(b);
startActivity(intent);
finish();
But my problem is the listing page get cleared on return back from that page.
ie, on click delete button in the child page, I want to delete the same item in the list page also. But after I deleted the item from the child page, using this code to redirect to the parent page.
Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
Bundle b = new Bundle();
b.putInt("id", 124);
intent.putExtras(b);
startActivity(intent);
finish();
How to delete the single item with out page refresh if coming from the child view and load the fresh page if coming directly.
Thank you for your time
Please help
Thanks in advance
Well you just remove the desired item from the list using the remove() method of your ArrayAdapter.
A possible way to do that would be:
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
Another way would be to modify the ArrayList and call notifyDataSetChanged() on the ArrayAdapter.
arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
There is another option you can use singleton class object .
Just declare a list in singleton class and use it wherever you want like following example it will be updated for everyplace
public class Singleton {
private static Singleton instance;
private static List<Product> itemInfos = new ArrayList<Product>();
Singleton() {
}
}
when you comming to back activity just notify the adapter in onResume()

Can't handle Intent properly in Android

I'm developing a Data Entry system wherein the user can save, search and update data. In this activity, I'm using EditTexts, spinners and a button (to search for Clinic Names). When the user is finish filling those EditTexts and spinners, he can search for Clinic using the Button. This button will trigger the intent to another activity.
The other activity has a EditText(with TextFilters and can search for Clinic) and a ListView which shows the results upon search. When the result shows up, the user can click the ListView Items and that will trigger to the previous activity and will fill-out the Clinic Name to the EditText assigned to it.
My problem is when the user clicked the ListView item, and goes to the previous activity, those data from the EditTexts and Spinners were cleared up!
I want to happen is that, when I search for Clinic Name and goes to the another activity, those data that was already filled-outwill not be cleared or lost.
MainActivity.java
//Getting clinic address
txt_DClinic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(getApplicationContext(), SearchClinicActivity.class);
startActivity(myIntent);
}
});
SearchClinicActivity.java
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
TextView tv_clinicId = (TextView) view.findViewById(R.id.textviewId);
String clinicID = tv_clinicId.getText().toString();
Intent intent = new Intent(view.getContext(), MainActivity.class);
intent.putExtra("clinic_id", clinicID);
intent.putExtra("signal", "2");
intent.putExtra("from", "SearchClinic");
intent.putExtra("LoginOrSearch", "ImSearchClinic");
finish();
view.getContext().startActivity(intent);
}
});
When you are clicking on list item and opening intent. set flag to intent.
intent.setFlag(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
and see if its working or not.
if its not than try one thing is that. you are passing extras in intent. at that time pass the values for edittext and item number of spinner. and in onCreate method of Main activity set text of edittext with that extra. and set selected value for spinner.
edtTxt.settext(getIntent().getExtras().get("Text"));
sp.setSelection(getIntent().getExtras().get("Value"));
Hope it Helps!!!
Call setSaveEnabled(true) or add android:saveEnabled="true" to layout of Views you want data retained for.
MyEditText.setSaveEnabled(true);
or
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="true"/>
Same for Spinner , or any other View you need to retain data of.

Using dynamically created buttons in android to take user to another activity with dynamically generated content

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);
}
}

Refresh List From Another Activity

I have 3 activities and 1 class;
MobilEpostaActivity (Main Activity) gives the datas from the Gmail server intent these datas to ListeleActivity (Fill and show the ListView) and ListeleActivity fills these datas to the ListView and when item onClick on ListView the item's click datas is intent to the GoruntuleActivity (Show the onclick item's information). And also I have a class "Baglantı" (means Connection) and it has all other methods which are necessary for each.
My problem is in here: when the user click on to the "Delete" button. I want to refresh the listview and show the next mail to the user. when user clicks to the delete button, I can show the user next mail with add 1 to the position and show it. But it is not work properly, because of its attachments are complicated.
Now starting with the MobilEpostaActivity; I am taking the username and password from the user and pass them to Baglantı class for connection to the server. Then I am gettin the result body, from, subject in arraylists. Then I pass these body, from and subject to the ListeleActivity with intent.
public void epostaListeleme() throws MessagingException, IOException
{
final Intent intent = new Intent(this, ListeleActivity.class);
intent.putStringArrayListExtra(bodylistesi ,(ArrayList<String>) getBodyList());
intent.putStringArrayListExtra(konulistesi ,(ArrayList<String>) getKonuList());
intent.putStringArrayListExtra(kimdenlistesi ,(ArrayList<String>) getKimdenList());
startActivity(intent);
}
In the ListeleActivity():
Getting the datas from the MobilEpostaActivity with Bundle. Filling the listview with "from" data. And waiting for listItemOnClick. If Onclick Action is happen then pass the datas to the GoruntuleActivity.**
In the GoruntuleActivity():
Getting the datas from the ListeleActivity with Bundle. And (Here is the important part) waiting for the Delete Button OnClick. If On Onclick Action is happen FIRST) delete the position from the ListView.
arrayAdapter.remove(arrayAdapter.getItem(position));
listBaglanti.RefreshedPositions(position);
arrayAdapter.notifyDataSetChanged();
SECOND) Delete the position's datas from the bodylist , fromlist and subjectlist:
public void RefreshedPositions(int position)
{
list.remove(list.get(position));
kimdenlist.remove(kimdenlist.get(position));
konulist.remove(konulist.get(position));
}
THIRD) Hold these "changed" lists in arraylists:
bodylistRefresh = baglan.list;
kimdenlisteRefresh = baglan.kimdenlist;
konulisteRefresh = baglan.konulist;
FOURTH) Set these "changed" lists to the MobilEpostaActivity's intent (the one of ListeleAcitivity's):
MobilEpostaActivity mobilObject = new MobilEpostaActivity();
mobilObject.setKonuList(konulisteRefresh);
mobilObject.setKimdenList(kimdenlisteRefresh);
mobilObject.setBodyList(bodylistRefresh);
As you think the process should start over again. But it doesn't start because with the Fourth step, I don't think so I made a refresh for the ListView.
Maybe I went a way which one is hard. But I hope, you tell me the right thing for refresh the listview and show the next mails. Also my screenshots;
When I first open the application : There is 3 mails from Merve. Positions : 0,1,2*
After Click the Delete Button : The ListView is refresh and the position=1's item is deleted. But it is not deleted on GoruntuleActivity's Screen.
It looks like you have too many lists involved. I think that you should be able to just remove the item from the main list like so:
arrayAdapter.remove(position);
listBaglanti.RefreshedPositions(position);
arrayAdapter.notifyDataSetChanged();
public void RefreshedPositions(int position)
{
list.remove(position);
kimdenlist.remove(position);
konulist.remove(position);
}
Just pass in the position as an int value instead of retrieving and then removing the object.

passing data from a view to an activity

I am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject

Categories

Resources