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());
Related
I am using a custom array adapter to display grades in a ListView, my question is how can I let a user set the number of rows displayed? I will take their selection in an editbox and then when they hit a button called "select" I will limit the display. I have searched for this elsewhere but couldn't find exactly how to perform this method. Thanks.
Just implement this code after onClick()
for (int i = 0; i < userInput; i++) {
yourList.add(data);
}
MyCustomAdapter dataAdapter = new MyCustomAdapter(yourCkass.this.getActivity(),
R.yourLayoutListItem, yourList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
I have a ListView which am setting that to my adapter. The ListView item contains two view elements which are chekbox and TextView.
I have given the setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) for the ListView.
When i want to select one or more element, I want to get the count (based on selecting and deselecting).
I can't do this stuff in onItemClickListener of ListView.
So I have written the logic in BaseAdapter.
holder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count += 1;
}
});
But If i deselect the item then the value have to decrease. If iam having only one item in ListView then I can write
SparseBooleanArray checked = listView.getCheckedItemPositions();
and get the value in Fragment. Just I get confused. Could someone help me?
Use this.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}
// use count as you wish
Make sure count is in a local (or block of the OnClickListener) scope so that every time you click a button or something, count gets reset and it recounts how many items are checked/unchecked.
Let me know if you have more question :)
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 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
This question already has answers here:
Selecting All Items in a Listview on checkbox select
(4 answers)
Closed 8 years ago.
I want to disable/enable all checkboxes in listview. infact want to get select all behaviour by clicking on top checkbox.
thanks
This is what finally worked for me, where I'm using a cursor adapter, not just an ArrayListAdapter for my list items:
final ListView list = getListView();
for ( int i=0; i< getListAdapter().getCount(); i++ ) {
list.setItemChecked(i, true);
}
list.getChildCount doesn't work because it only seems to count what's been drawn immediately (not everything that's off the screen) so childCount might only be 6 or 8 items when the entire list is 100 or more items. Also as I had to use list.setItemChecked to get the items to 'stay checked' -- at least in my case where my list items were instances of CheckedTextView.
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
}
You need to edit this code to handle both enabling and disabling, but I hope you get the idea !
Also, this only checks each checkbox, make sure you are returning an id or object from your list in order to send/save the data elsewhere.
I think you should run this long-running task off the UI thread. When you click button in OnClickListener:
new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < list.getAdapter().getCount(); i++) {
final int position = i;
mHandler.post(new Runnable() {
#Override
public void run() {
list.setItemChecked(pos, true);
}
});
}
}
}).start();
and in onCreate() :
this.mHandler = new Handler();
Each item in list view should be Checkable like CheckableRelativeLayout that implements Checkable interface.
just add one more parameter in adapter as
Boolean Ex.MyAdapter adapter = new Save_Weight_Adapter(this, R.layout.layoutname, array, false);
listview.setAdapter(adapter);
on select all button click listener add this code
MyAdapter adapter = new Save_Weight_Adapter(this, R.layout.layoutname, array, true);
listview.setAdapter(adapter);
and check in adpater
if(chk) {
blue_check.setVisibility(View.VISIBLE);
} else {
blue_check.setVisibility(View.GONE);
}