Android show single item from a list in next page - android

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()

Related

How to pass data from clicked item in recyclerview to viewmodel and open new activity?

As I wrote, I'm looking the solution to that problem. How to correctly in MVVM in Android pass texts and id clicked an item to ViewModel and open new activity?
The new activity is a detail of item. So when I click on the item I want to display new activity with data from the clicked item and I need item id to edit the object in the item.
Using the RecyclerView.Adapter to populate your data, implement OnClickListener to viewHolder.
At onClick Method (overridden) you can type your code to start new activity,
lets say you have model class called Test, and array list called testList. then:
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra(KEY, testList.get(getAdapterPosition()));
mContext.startActivity(intent);
Note that getAdapterPosition() will return the position where you clicked, mContext is context passed to adapter.
You will need your model class to implement Parcelable to allow model to be transferred between activities.
https://developer.android.com/guide/components/activities/parcelables-and-bundles

Update list view from 2nd activity | 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.

Displaying updated ArrayList items properly within a fragment

basically my problem is this. I am trying to display a list view within a fragment set within the xml file, this list view gets its data from an ArrayList that gets its data from a form I created in another activity using Edit Texts which are put into an ArrayList and passed as a Parcelable Array List to the fragment class.
At the start of the fragment activity there of course are no values stored so I bring up the form activity for data input upon checking that there is no intent stored with the id I specify the passed Parcelable ArrayList as. The problem I have is that whenever I try populate the list view with more than one item (having the first item showing up, returning to the form to add another item in), it seems to only display the one item I add each time and not the items I added previously.
public class MasterFragment extends ListFragment {
private DetailFragment ingredDetails;
private ArrayList<Ingrediants> ingredList = new ArrayList<Ingrediants>();
private ArrayList<String> test2 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) { //If extras are present
boolean data = extras.getBoolean("ingrediant", true);
if (data) {//Data present from form, retrieve from ArrayList<Ingrediants> and populate the ArrayList<String>
ingredList = getActivity().getIntent().getParcelableArrayListExtra("ingrediant");
test2.add(ingredList.get(i).ingrediantName);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test2));
}//END OF IF DATA
}//END OF IF EXTRAS
else {//No data found, bring up the FormActivity
Intent intent3 = new Intent(getActivity(), FormActivity.class);
startActivity(intent3);
}//END OF ELSE
}//END OF onCreate()
I think it has something to do with how I am creating the list view and populating it with the ArrayList with the data got from the Parcelable ArrayList intent as it could be recreating the list view each time and only displaying the data supplied to it which in this case will be one item at a time. I've been stuck on this for a while now and was wondering if anyone has any ideas ? Thanks much.
The ArrayList you use (test2) to keep the data does not persist. You can either save the ingredients in sqlite db and call all items onCreate in your fragment, or move the test2 to the main activity and populate items before passing it with Intent bundle.

when the particular list in the item is clicked .it loads the data in the particular list

when the particular list in the item is clicked .it loads the data in the particular list and send that data to the another activity and set that data in edittext in that activity .how it can be done?pls help me??
Nobody will do your work, post some code and we'll help you where you get stuck.
In general you do this with an Intent in the OnClick method
Example:
private static final int REQUEST_EDIT = 1;
...
Intent intent = new Intent(this, MyEditActivity.class);
intent.putExtra("module", m);
intent.putExtra("position", listPosition);
startActivityForResult(intent, REQUEST_EDIT);
...

passing data from list view to another activity

i want to create an activity, in which, i would like to have a listview, there can be about 20-30 items in a list view,on tapping any particular value in listview, it should move to another activity, with the data of list view.
Just wanna pass data from listview to another activity.
Suggestion plz
Regards
Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.
Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.
Now use this code:
Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
startActivity(anotherActivityIntent);
and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");
Now you have your data in myVal variable. you can use any other data type, whichever you like.
You can pass data from one activity to another activity:
see this link
and to get data for ListView you have to first implement getListView.setOnItemClickListener(),
and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object obj = this.getListAdapter().getItem(position);
String value= obj.toString();
Intent intent= new Intent(CurrrentClass.this,NextClass.class);
intent.putExtra("value", value);
startActivity(intent);
}
Hope this will help you.
String selectedItem =arrayAdapter.getItem(position);
Intent intent = new Intent(getApplicationContext(),
Your_Second_Activity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
Second_Activity.Class
Bundle bundle = getIntent().getExtras();
String yourItem = bundle.getString("selectedItem");
Now! your selected item is inside in the yourItem Variable...
Use Bundle in onClickListner of the listview .
Bundle will pass data from one activity to Next.
There are two ways:
Pass it into Intent
intent.putExtra("jobNo", item.jobNo);
Use Application scope
((MyApplication) getApplication()).setJobNo(item.jobNo);

Categories

Resources