Add new items on top of list view - android

Because android automatically moves new items to the bottom of list view I want that to be reversed. In any case my condition is met, I want to add new items on top of list view.
I have seen this post here but I don't know how to add that to my code, here it is:
if(condition){
listView = (ListView) findViewById(R.id.ListView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, R.layout.list_b_text, R.id.list_content, ArrayofName);
listView.setAdapter(adapter);
}

Just simply add every item at position 0 of your ArrayList so when you call listView.notifyDataSetChanged(); it will show latest items on top.
for (Object obj : objectList) {
ArrayofName.add (0, obj); // this adds new items at top of ArrayList
}
objectList is basically an ArrayList or List of Object or String (whatever is your case). If you want to add items one by one, remove for loop. This loop actually iterates every item of objectList and adds it in your ArrayList at top position.

Related

Open a row in listview automatically (android project)

For example; I have long listview and it has 30 lines(items). I want to show this listview but after open the screen, it will show 15. lines. That is, the middle of the listview will be shown automatically. Is it possible? (NOTE: I don't want to show 15. line as first row.)
UPDATE:
i don't want to delete rows. I have listview and it works well. I want to show the middle of list. Scrool will flow until 15. row and i will see all of them but it will show 12. or 15. row when listview opens.
Bind your ListView to ArrayAdapter
final ListView lv = (ListView) findViewById(R.id.lv);
String[] fruits = new String[] {
"Cereus peruvianus",
"Bacupari",
"Beach Plum",
"Black raspberry"};
// Create a List from String Array elements
final List fruits_list = new ArrayList(Arrays.asList(fruits));
// Create an ArrayAdapter from List
final ArrayAdapter arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, fruits_list);
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
Remove / Delete first item from List. You may remove multiple in a loop.
fruits_list.remove(0);
// Notify adapter
arrayAdapter.notifyDataSetChanged();
Edit:
// For scrolling to specific item in your list
lv.smoothScrollToPosition(15); // Here 15 is the position of the item
Answer is lv.setSelection(15);

How to remove item from adapter

I have a container, adapter, and couple items. I would like to save items into database one by one.
However, I have issue on getting next item from adapter and resize adapter.
So far I have the code below:
adapter.add(item[0]);
adapter.add(item[1]);
adapter.add(item[2]);
item = adaper.getItem(0);
item.setDismissListener(new Item.OnDimissedListener(){
#Override
public void save(){
1. save item to database, it works here
2. get next item, I do
item = adapter.removeFirst(); // this one returns null pointer exception.
}
}
container.setAdapter(adapter); // set adapter here
Do you guys have any idea?
I had a similar project where I stored strings in a SQLite database and users could add/delete/modify items. What I did was to temporarily store the list of items in an ArrayList<String> and then update the ListView adapter with adapter.notifyDataSetChanged(); when that ArrayList changes.
In your case, I'd try something like this:
// Create an ArrayList <String>
ArrayList<String> itemsArray = new ArrayList<String>();
itemsArray.add( YOUR_ITEMS_GO_HERE );
...
// add data in ArrayAdapter
adapter = new ArrayAdapter<String>(this, R.layout.list, itemsArray);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
// Perform your Save actions
// Delete the first item in the ArrayList
list.remove(0); // removes the first item
// Update the ListView
adapter.notifyDataSetChanged();
If you are not using Strings, replace <String> with your <Object Type>

Remove items from listview on its ItemClickListener

I am using a collection of ArrayList to fill my Listview. My ListView contains two separate rows types.
Header and Footer.
I am trying to achieve the ExpandableListView Functionality on my Listview from which I am trying to remove some items on click of header till next header.
I am using this function to loop through items and removing items
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
items.remove(i);
}
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
QUESTION IS : I am not able to remove all items from the list in one shot, some stays there !
I have tried looping through adapter.count(); but no luck
My List :
SECTION 1
ITEM 1
ITEM 2
Item N
Section 2
But when I click on Section 1 not all ITEMS get deleted in one shot WHY!
I am not able to use Expandable Listview at this stage because activity contains many more complex functionality on List. Please help me where I am going wrong!
Create a new ArrayList<Collection> , Then add your item in it and then use removeAll(collection).
TRY THIS:
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
ArrayList<Collection> deleteItems= new ArrayList<Collection>();
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
deleteItems.add(items.get(i));
}
items.removeAll(deleteItems);
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
EDIT
Every time you are deleting an item, you are changing the index of the elements inside .
e.g : let suppose you are deleting list1 , then list[2] becomes list1 and hence your code will skip list1 next time because now your counter would be moved to 2.
Here are other ways by which you can achieve this also,
Removing item while iterating it
So what exactly I did now. Instead of looping through items again I did like this :
I created another list and parallely populate it with the main array.
items.add(user);
// after populating items did this
newItems.addAll(items); // same collection ArrayList
and finally I can play with the main array by using removeAll and addAll methods.
items.removeAll(newItems); // remove items
items.addAll(afterPosition,newItems); // add items after position

Android : Listview stuff

I have a listview displaying list of items and a button at the bottom. On click of button, i'am displaying one alert dailog box to take input from user and adding that input to the listview. 1. How can i make sure the new input/entry should add always at top of listview? (Right now it is adding at bottom always)2. Is it possible to position the list view at the new entry? (i.e if the new entry is added at top, the list should be positioned at the top)Please guide me.
Inside onCreate()...
list = (ListView)findViewById(R.id.planetList);
adapter = new ArrayAdapter<String>(MyListViewActivity.this,
R.layout.my_list_row, R.id.planetNameTextView);
for (int i = 0; i < planetNamesArray.length; i++) {
adapter.add(planetNamesArray[i]);
}
list.setAdapter(adapter);
After taking input from user ....
adapter.add(newPlanetNameEnteredByUser);
adapter.notifyDataSetChanged();
you can use listview.setStackFromBottom(false) to fill your listview from top.
this link may help you.
http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom%28boolean%29
Yes you can do that, have two List.
List<String> old = new ArrayList<String>(); // contains your list content
List<String> new = new ArrayList<String>();
Now, when you want to add new content to ListView then add it first to the new ArrayList,
new.add("new content");
new.addAll(old); // add all the old contents
new.setSelection(position_you_want);
adapter.notifyDataSetChanged();
Add the object in array list
ArrayList<String> List= new ArrayList<String>();
ListView ListName= (ListView)findViewById(R.id.playerdata);
with
List.add(index, "value");
public void add (int index, E object)
Since: API Level 1
Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.
Parameters
index the index at which to insert the object.
object the object to add.
Throws
IndexOutOfBoundsException when location < 0 || > size()
and set list adapter
ListName.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1 , List));

Add new items to top of list view on Android?

Android has the transcript mode to allow to automatically scroll a list view to the bottom when new data is added to the adapter.
Can this be somehow reversed so that new items are automatically added at the top of the list ("inverse transcript mode")
Method stackFromBottom seems about right, but does not do the auto-scrolling on input change.
Does anyone have some example code where a list is constantly adding stuff that gets always inserted at the top? Am I on the right track here?
Update
Thanks for the answers, that made me think more. Actually. I want to have new entries to appear at the top, but the screen still show the item the user is looking at. The user should actively scroll to the top to view the new items. So I guess that transcript mode is not what I want.
Hmm, well, if I was going to try this, I'd do something like the following:
List items = new ArrayList();
//some fictitious objectList where we're populating data
for(Object obj : objectList) {
items.add(0, obj);
listAdapter.notifyDataSetChanged();
}
listView.post(new Runnable() {
#Override
public void run() {
listView.smoothScrollToPosition(0);
}
}
I don't know for certain that this will work, but it seems logical. Basically, just make sure to add the item at the beginning of the list (position 0), refresh the list adapter, and scroll to position (0, 0).
instead of this:
items.add(edittext.getText().toString());
adapter.notifyDataSetChanged();
you should try that (works for me):
listview.post(new Runnable() {
#Override
public void run() {
items.add(0, edittext.getText().toString());
adapter.notifyDataSetChanged();
listview.smoothScrollToPosition(0);
}
});
Shouldn't it be enough to just add a smoothScrollToPosition(0) whenever stuff gets added to the ListView? Don't think there's an automatic scroll option.
I spent several hours attempting to accomplish the same thing. Essentially, this acts like a chat app where the user scrolls up to view older messages at the top of the list.
The problem is that, you want to dynamically add another 50 or 100
records to the top but the scrolling should be continuous from where
the prepended items were added.
The moment you do a notifyDataSetChanged, the ListView will automatically position itself at the first item in your data set and NOT at the position that preceded the position where the new items got inserted.
This makes it look like your list just jumped 50 or 100 records. I believe TranscriptMode set to normal is not the solution. The listview needs to function as a normal listview and you need to programmatically scroll to the bottom of the list to simulate the TranscriptMode as it functions under "normal".
Try to use
LinkedList items = new LinkedList<Object>();
//some fictitious objectList where we're populating data
for(Object obj : objectList) {
items.addFirst(obj);
}
listAdapter.notifyDataSetChanged();
This resolves the problem:
...
ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> aList;
...
lv = (ListView) findViewById(R.id.mylist);
aList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, aList);
lv.setAdapter(aAdapter);
...
adapter.insert ("Some Text 1", 0);
adapter.insert ("Some Text 2", 0);
adapter.insert ("Some Text 3", 0);
...
you should try that (list position and refresh adapter)
list.add(0,editTextSName.getText().toString());
adapter.notifyDataSetChanged();

Categories

Resources