How to add data in list below previous one? - android

I want to add data in list. When I click the button, it takes data from different classes and adds it as a row in the list and comes back.
And again, when I insert data in form and press the button, it adds it below the previous one on another row. So an array is created of rows, and now when I click on any position of list item row, I get its content. So how to implement it?

First thing you need to do is whenever you add new data in the list you should call notifyDataSetChanged(). For getting its content you need to write onItemClickListener() and in listener you can get the content through its position id.

Your question is not very clear. But from what I understand, you what to use a ListView or a ListActivity.
In either case, you will need an adapter which manages the data displayed in the list. I would suggest that your "different class" which provides the data should subclass BaseAdapter.
You can then bind the class providing the data to the view using setAdapter(ListAdapter)
When your data changes, all you need to do is call notifyDataSetChanged() on your data class which provides the data and the view will be updated.
To get the clicked item, you will need to create and set an OnItemClickListener on your ListView
See this list activity example for guidance.

deep copy the generated list and append it to the previous array

Related

Android ExpandableListView save input in each item's edit text on button click

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.

Dynamically adding place holder list items to ListView AND updating source data set with user input

I have an empty ListView where the user should be able to add each list item manually at the press of a button. When the user clicks on the button, I have a place holder list item appear which contains an EditText. When the user edits the text and it loses focus, I want the source data set to be updated with the users input.
Dynamically add elements to a listView Android
Up to now, I found this answer which uses an ArrayAdapter that connects an ArrayList and my ListView but from my understanding, this only updates the ListView when the ArrayList is updated. To clarify, I want to do the opposite, i.e when the list item is changed within the ListView, I want the appropriate ArrayList (or whatever data source which would make this easier) item to be updated.
I'd appreciate any help with this.
Thanks in advance!
I would suggest storing the position you focus on, then when it loses focus, write the new data to the item in your arraylist by calling getItem(position); followed up by notifyDatasetChanged();

Android: How to dynamically replace a list view with another list view like Instagram with onClick?

I'm building an android app and I'm trying to replace a list view with another list view if you click a button, like the notifications page on Instagram. On that page, if you click on the top "following" button it will show you a listview of what your followers have liked. If you click on the "you" button it will show you a listview of what people have liked your photos.
Any help would be greatly appreciated!
You can do it by following ways,
1. add Two listviews and can change the visibility as per your requirement.
2. On button click you can load the other data into same list view and can update your adapter in the same list view.
in 1 you have to load two list view at first which will consume more time if data is larger sure you can write a login in asynctask to load list views in background thread.
in 2 you have to update your adapter at the button so you will have to provide some progress bar of dialog for user while you list view is getting update.
You can use either of this whichever suites you best.
Simply , You don't need to switch Listview , you only need to switch adapters .
eg, you can switch to mFollowingAdapter when clicked on Following button and switch to mYouAdapter when you select "You" tab. that's it.
You should write a list, that has a custom adapter. This adapter will be able to display BOTH views you want to display.
If the data to be displayed is the same format (ie. both have an imageview next to a textview), you are in good shape.
When you want to switch to a different list, get the information you would like to display, replace the data in your the collection backing your list, then notify the list that the data has changed, and it should redraw.
So, this might look like:
create ArrayList() with data A
setup List, with this data and display
replace the ArrayList() with data B
call listView.notifyDataSetChanged
You can still do this if the Data A, and Data B have different views, in this case, you would need to handle this logic in your custom adapter.

Android ListView last clicked item as first

I'm wondering right now how to implement following functionality in best and most reusable way.
Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.
I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?
You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.
To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.
The most simplest thing I could suggest for it is
Remove the item from the list and the add the item to the list on calling on activity result.
Set the data to the adapter (may be using a public method in adapter class)
Call notify data set changed.
Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().

Access to data that is shown by a list view in android

I have a list view in an android program. Each row of this list view has a button. When user clicks one of these buttons, an action must be performed on data that is shown by that row.
How can I get access to that data and manipulate it?
Call getItem() on your Adapter to retrieve whatever the data is that is at that position within the Adapter. So, for example, if your Adapter is an ArrayAdapter<Restaurant>, getItem() will return the Restaurant for a given position.

Categories

Resources