I have a listview , i would like to put some position preselected; in fact i have some values that i would like to put them selected. this is my code
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
selectedItems.add(adapter.getItem(position));
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
My selectedItems firstly is not empty; i would like to put its value as selected.
Pass your selectedItem list to your custom adapter and inside getView() method compare position. If it is available in seloectedItem list change the background of view.
Use a custom adapter and pass the list inside there and render it on getView() by inflating the item.xml layout and use a static class viewHolder to create a viewholder that transfers the data from your list into the appropriate widgets.
Make sure you check if the convertView has been created previously as you do not want to create the view every time android hits getView from your adapter as it will slow your listView UI.
Good luck
Related
I have a listview that is built from textviews with the built in Resource Layout simple_list_item_multiple_choice.
syntax like this :
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, internet);
If the user clicks an item list, the checkbox of selected item has to be checked.
I know to implement OnItemClickListner, But after it what should I do to check the selected item.
Yu can try this ,referred from https://stackoverflow.com/a/4590897/5193608.
SparseBooleanArray.get returns a boolean, but I believe you need to check it for each position in your list, e.g.
int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = cont_list.get(i);
/* do whatever you want with the checked item */
}
Following the guide found here http://developer.android.com/guide/topics/ui/menus.html#CAB I went up to a dead end on how to remove all the selected items from the listView's adapter.
In the guide it is shown as a method called deleteSelectedItems(); but since it is never implemented, I got stuck. How can I do this?
I asume you are using a List. Do the following:
private void deleteSelectedItems() {
SparseBooleanArray checked = mListView.getCheckedItemPositions();+
List<YourObject> list = mListOfObjects;
for (int i = 0; i < mListView.getCount(); i++)
if (checked.get(i))
YourObject item = list.get(i);
mListOfObjects.remove(item); //or whatever you want to do with it.
}
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.
I´d like to add an image in a row of a ListView, this is my code:
ListView listViewProducts = (ListView) findViewById(R.id.listViewProducts);
ImageView iconMeat = (ImageView) findViewById(R.id.iconMeat);
String[] products = getResources().getStringArray(R.array.products);
for (int itemPos = 0; itemPos < listViewProducts.getCount(); itemPos++)
{
if ( products[0].equals(listViewProducts.getItemAtPosition(itemPos)) )
{
//TODO
// In this case to add the image
}
}
Thanks
See these posts for using adapters in listview :-
https://stackoverflow.com/a/8267165/2035885
http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter
http://www.mkyong.com/android/android-listview-example/
Do following:-
Create a custom adapter for listview.
Create an array of drawables for populating the list.
In your getView method of adapter populate the list according to index.
I am trying to count listView items. Im using this code:
int count=0;
ListView listView = (ListView) findViewById(R.id.listView1);
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++;
}
}
Toast.makeText(getApplicationContext(), String.valueOf(count), Toast.LENGTH_SHORT).show();
Why the COUNT variable value is always 0, when listView display some records?
If you are looking for count of all ListView items, you can use this call (make sure adapter is set):
listView.getCount();
If what you want is count of visible items, try this (works only for visible ListView):
listView.getLastVisiblePosition()-listView.getFirstVisiblePosition();
Let me explain the reason..
You've just get the listview like this
ListView listView = (ListView) findViewById(R.id.listView1);
so listview has no elements and then you are tring to get the last visible postion by using listView.getLastVisiblePosition() it always returns zero because your listview hasn't yet bind with any adapter, i.e your listview is empty at the time your are getting the last visible position try to place this code after binding Adapter to the listview
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++;
}
}
Use this .It helps mine
String CountListRowNo= String.valueOf(+ListviewObj.getAdapter().getCount());