I am new in Android development. I have MultiselectionListview in my app.I want to delete all the items selected, But for it i have to call a function from Sqlite Db.I have to pass selected item positions in the form of array. I am getting position as follows :
SparseBooleanArray checked = lv_del.getCheckedItemPositions();
for (int i= 0; i<=checked.size();i++)
{
int[] posArr = new int[checked.size()];
// Item position in adapter
int position = checked.keyAt(i);
if(checked.valueAt(i))
{
posArr [i] = (int) adapter.getItemId(position);
}
}
}
but its not working, how can i pass selected items positions via array ??
any help will be appreciated.
You can use an additional button outside the listview as an ok button.And write the code for creating the list of items using position array and call the delete function with that list in the listener of ok button.
Related
I am working on a XMPP based chat in android.. and I am struck at a point where I need to update the position of an item in the listview to the top in case a new.message arrives.
The use case is.. I am on Contacts screen of the app and a new message comes.. so this contact should move to top of the list and get bold. This is what is similar to whatsapp as well
How can this be done. My class imolemebts activity and i have implemented custom list adapter.
So howcan I find if an item exists in the listview and secondly how to dynamically change position
First, keep in mind that a ListView is just a representation of a list of Objects. So if you want to know if an item is in the ListView, you just have to check if the corresponding Object is in your list of Objects.
Is the same when you want to change the position of one item, you have to change the position of the Object in the list.
Start by defining these objects:
private ArrayList<MyObject> lists = new ArrayList<MyObject>();
private MyCustomAdapter myAdapter;
The first time you create your ListView, just do as usually:
//fill your list with your objects
lists.add(myObject1);
lists.add(myObject2);
lists.add(myObject3);
//create and set the adapter
myAdapter = new MyCustomAdapter(..., ..., lists);
myListView.setAdapter(myAdapter);
Now you can know if your lists contains a specific object (which is the same that checking if an item is in your ListView) by simply testing that:
lists.contains(anObject);
Then, if you want to change the position of a specific item in the ListView, you have to create a new list and put the elements in the correct order. You can use something like that (not tested but it should work):
private ArrayList<MyObject> moveItemToTop(ArrayList<MyObject> lists, int positionOfItem) {
if (lists == null || positionOfItem < 0 || positionOfItem >= lists.size()) {
return lists;
}
ArrayList<MyObject> sortedList = new ArrayList<MyObject>();
//add the item to the top
sortedList.add(lists.get(positionOfItem));
for (int i=0; i<lists.size(); i++) {
if (i != positionOfItem) {
sortedList.add(lists.get(i));
}
}
return sortedList;
}
Or even this (which is way easier...).
Finally, call these two methods to update your ListView:
myAdapter = new MyCustomAdapter(..., ..., moveItemToTop(lists, itemPosition));
myListView.setAdapter(myAdapter);
This is how I resolved it
private void moveMessageToTop(MessageObject message) {
int index = 0;
for (Friends friend : mFriends) {
if (friend.getName().equalsIgnoreCase(message.getFrom().split("#")[0])) {
index = mFriends.indexOf(friend);
break;
}
}
if (index != 0) {
mFriends.add(0,new Friends(message.getFrom().split("#")[0], message
.getMessage()));
} else {
Friends frnd = mFriends.get(index);
frnd.setStatus(message.getMessage());
mFriends.add(0, frnd);
mFriends.remove(index);
}
((ListAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
}
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
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));
How to get the new value to the top poistion of the listview when I entered the digit in a edittext.
Am getting the new value, which is given in the edittext,adding to the last position of the listview.
But I want the new value must be added to the top position of the listview.
Please can anyone help me in doing so?
If you are using ArrayList<String> arrayList = new ArrayList<String>(); then you can add the data to ArrayList at the 0 index and call notifyDataSetChanged(); to re-populate your ListView.
arrayList.add(0, "First");
just Reverse Your For Loop from size of ArrayList to 0.
Like This following Code ...
int j = ArrayList.size();
for (int i =j-1; i >= 0; i--)
You might want to add a button to the View containing the EditText and then when the user clicks that button get the value of the EditText and add it to the ListAdapter.
button.setOnClickListener(new OnClickListener(){
//Get the data from the edittext
String s = editText.getText().toString();
//Add it to your adapter
adapter.add(s);
//Refresh the adapter
adapter.notifyDataSetChanged()
});
Here are some links you might want to check out:
ArrayAdapter
EditText
is it possible to programatically access specific rows in a list of CheckedTextViews to change the state of their textboxes?
my program has a listview which has several CheckedTextViews which the user can press to toggle state.
I want to save the state of the checkboxes when the user leaves the activity, so I have in my onPause method:
public void onPause(){
super.onPause();
SparseBooleanArray positions;
positions = listView.getCheckedItemPositions();
ListAdapter items = listView.getAdapter();
int j = items.getCount();
ArrayList<Long> ids = new ArrayList<Long>();
for (int k =0; k < j;k++){
if(positions.get(k)==true){
ids.add(items.getItemId(k));
}
}
this.application.getServicesHelper().open();
this.application.getServicesHelper().storeServices(ids,visit_id);
this.application.getServicesHelper().close();
}
which very simply iterates the list view, adds the checked items to an ArrayList and then saves that list of ids to the database.
My problem lise in trying to reset the list once a user goes back to that activity.
so far in my onStart method, I recall the checked items from the database, but I do not know how to march the ids returned to the listview elements. can I do something like:
listView.getElementById(id_from_database).setChecked?
I know I cant use getElementById but I have it here to show what I mean
Thanks in advance
Kevin
You can call
listView.setItemChecked(int position, boolean value)
This is what Ive ended up doing.. but it seems like a complete hack.
Basically I have to set up a double for loop.. one to iterate through my list elements, and one to iterate through the cursor that I have retreived my check list state (a simply array of ids of elements that were checked when state was last saved)
my outer for iterates through the list elements checking each id against a loop through the list of ids to be set as checked. if they equal each other then set that item as checked.
// mAdapter is contains the list of elements I want to display in my list.
ServiceList.this.setListAdapter(mAdapter);
// Getting a list of element Ids that had been previously checked by the user. getState is a function I have defined in my ServicesAdapter file.
Cursor state = ServiceList.this.application.getServicesHelper().getState(visit_id);
int checks = state.getCount();
int check_service;
int c = mAdapter.getCount();
if(checks>0){
state.moveToFirst();
for (int i=0; i<checks; i++) {
// set check_service = the next id to be checked
check_service = state.getInt(0);
for(int p=0;p<c;p++){
if(mAdapter.getItemId(p)==check_service){
// we have found an id that needs to be checked. 'p' corresponds to its position in my listView
listView.setItemChecked(p,true);
break;
}
}
state.moveToNext();
}
}
ServiceList.this.application.getServicesHelper().close();
Please tell me there is a more efficient way of achieving this!!
Thanks
Kevin