In android, is it possible to get all items inside the list view. Lets say the list view has multiple rows and only 2 rows are visible on the screen while the rest are accessible using the scroll bar. Each row has a radio button and a text view. Is there a way to get all textview of the rows whose radio button is selected and not just the ones visible on the screen.
Your answer may be:
for(int item = 0; item < m_listitem.count(); item ++){
if(m_listitem[item].isSelected){
View view = ListView.getChildAt(i);
TextView textview = view.findViewById(your textView id);
// do some thing
}
}
You can use custom list view to show your list items with checkbox & textview.
I happened to have a similar requirement where I had multiple EditText inside a ListView and only few of them were visible on the screen. I needed to get the values of all EditText and not just the ones visible on the screen.
Well if you are using a default Adapter, then the way it will work is it will recycle the old views to create new ones. So there is no way to preserve values of those Views which are not visible.
So the only workaround is to create your own Adapter, maybe something like the following, which will not recycle any views, but every time inflate new ones.
public class ListViewAdapter extends ArrayAdapter {
public ListViewAdapter(Context context, ArrayList<Object> items) {
super(context, 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return LayoutInflater.from(getContext()).inflate(R.layout.your_layout_for_list_view_item, parent, false);
}
}
After that, as in above answer Lãng Tử Bị Điên has mentioned, you can check in your java code, if your radio buttons are checked or not, and according to that, selected desired TextViews
for(int item = 0; item < m_listitem.count(); item ++){
if(m_listitem[item].isSelected){
View view = ListView.getChildAt(i);
TextView textview = view.findViewById(your textView id);
// do some thing
}
}
Hopefully this should do it.. It sure worked in my case!
Related
The Problem
My way to add the view makes every fifth item to add the view when i only want one position to have this "Mängd" row.
Why Can i only edit listitems when they are visible on the screen.
The child will be static at 5 items even though i got like 20 item....
Is there any way to only say that item 1 will have this and not
position - firstvisibleposition
i think this is the problem with the listview
My code is not understandable at the time because of other things so i hope you get my problem anyways.
This is my main question
It seems like the thing i add to position 0 also happens to 6 and 12 Why is ListView this wierd ?
It's on swedish, but this is what i got with list view.
Every listview item has a empty Linearlayout that i add a view to when i press the down arrow button. the problem is that every fifth item gets this or i can only click on the first 5.
I dont get why they make ListView so complicated. i want to be able to get a child that is in the list without seening it!
CODE getView
public View getView (int position, View convertView, ViewGroup parent){
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.view_meal_item_editable, null);
}
convertView.setOnTouchListener(new ItemSwipeListener(position,
getResources().getDisplayMetrics().density));
convertView.setClickable(true);
// Lookup view for data population
TextView food_item_name = (TextView) convertView.findViewById(R.id.food_item_name);
food_item_name.setHint("hello");
}
Where i add the view
View view = searchResultList.getAdapter().getView(position, searchResultList.getChildAt(position - searchResultList.getFirstVisiblePosition()), searchResultList);
LinearLayout extendedView = (LinearLayout)view.findViewById(R.id.extended_food_information);
View convertExtendedView = LayoutInflater.from(this).inflate(R.layout.change_amount_on_food_view, null);
extendedView.addView(convertExtendedView);
It's recommended to use a header view if you do this stuff only for the first element.
Otherwise it will be better if you add your extra view in getView() method, something like:
if(position==0){
// add extra view
} else {
// remove extra view if exist
}
Or you can remove the IF condition: if (convertView == null), so you will inflate a new layout each time, it will solve your problem but this is not good for list performance
I have a listview and I want to change the dividerHeight for specific Item.
For example I want it look like that:
Item 1
Item 2
Item 3
// Bigger space here
Item 4
Item 5
I know how to change space between each Items but how can I specify a space depending on the item position in the list.
The other possibility is to use a different drawer for some items.
Thanks a lot
PS: Sorry for my bad english, I'm french
EDIT: Here is the code where my Items are displayed on my Listview:
drawerItemsList = getResources().getStringArray(R.array.items);
myDrawer = (ListView) findViewById(R.id.my_drawer);
myDrawer.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_item, drawerItemsList));
You can customize your adapter by extending ArrayAdapter class and then do what you want in its getView method, A simple solution for your problem might be the following snippet code:
#Override
protected View getView(int position, View convertView, AdapterView parent) {
View view = convertView;
// Check 'view' value and inflate it if needed
if(position == 3) { // Gap index in the list
// Here you can add some additional bottom margin or padding to the
// 'view'.
// Or you can left this view empty like a free space
}
return view;
}
i want to change color of listview (simple_list_item_2 adapter) children at specific positions programmatically. (here for example all items with a = lv.getCount();)
ListView lv = getListView();
int a = lv.getCount();
for (int i = 0; i < a; i++) {
((TextView) lv.getChildAt(i).findViewById(android.R.id.text1)).setTextColor(Color
.parseColor("#EEC900"));
}
getChildAt(); doesnt always work for me. in case of the list-item being out of sceen, getChild doesnt return a view or something..
isnt there a better solution instead if getChildAt?
You will have to do this in your Adapter class. Android caches and re-cycles the views in a listview to conserve memory. So there are are no views that you can't see to change the color of.
So for example if you had an arrayAdapter, you would override the getView function and run your check there:
#Override
public View getView(int position, View view, ViewGroup parent) {
// use "position" to determine which item you have.
// Then set the properties of "view" which is your list row.
}
I facing below issue with item back ground on scrolling.
In my application I have a listview which require multi-selection. Also this is a custom list where selection needs to be represented by change in list item color instead of check-box based approach.
For this: In the OnClick I'm checking if the position is selected or not and then set the background for the item. However this has issue when I scroll the list. Taking an example:
suppose the list has 50 items. And 10 are visible at a time. I select say 5th item [thus changing the background]. And then I scroll the list. After scroll the visible part of the list corresponding to earlier 5th item ,say 15th item in item of the list but 5th index in visible portion,still has background corresponding to selected state. Whereas it should not have been set since I have not selected 15th item yet.
I tried:
a-In the getView method of adapter, if the item is not one of selected items I'm setting one background else different.Tried - setBackgroundColor as well setBackgrounddrawable.
b- In the xml have set the cacheColorHint to transparent
c- Have selector attached to items and the items responding to state [pressed,selected] in onlcick.
However still I'm not able to get rid of unwanted background color for item on scrolling.
Any help. I tried various suggestion mentioned in various post in SO but not succcessful yet.
I tried
thanks
pradeep
this is a normal behavior of ListView adapter in android, its getView() called on every scroll and for every new list item it call getView, if listview item currently not visible on UI then its convertView is equals to null: At a time listview take load of only visible list items, if it showing at a time 10 element out of 50, then listView.getChildCount() will return only 10 not 50.
In your case when you select 5, it reflected selection for 5+10(visible items count) = 15, 25, 35, 45 too.
To solve this problem you should have a flag associate with your each listItem data, for example if you have string array itemData[50] as array, then take an array of boolean isSelected[50] with initial value false for each.
Take a look for getView(), in adapter class:
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
string text = itemData[position]
if (convertView == null) {
rowLayout = (RelativeLayout) LayoutInflater.from(context)
.inflate(R.layout.list_view_item, parent, false);
holder = new ViewHolder();
holder.txtString= (TextView) rowLayout
.findViewById(R.id.txtTitle);
rowLayout.setTag(holder);
} else {
rowLayout = (RelativeLayout) convertView;
holder = (ViewHolder) rowLayout.getTag();
}
if(isSelected[position] == true){
holder.txtString.setText("Selected")
rowLayout.setBackGround(selected)
}else{
holder.txtString.setText("Not Selected")
rowLayout.setBackGround(notSelected)
}
public class ViewHolder {
public TextView txtString;
}
and in your Activity class on listView.setOnItemClickListener():
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
isSelected[position] = true // on selection
RelativeLayout rowLayout = (RelativeLayout) view;
rowLayout.setBackGround(Selected);
// also set here background selected for view by getting layout refference
}
});
I am trying to highlight an item in a ListView (touchable interface). The aim is that the user will touch an image and it will select the item in the ListView moving it to be visible (it does not all show on the screen at the same time) and then highlight the item in the ListView related to the position on the image by changing the background color of the item view. I have a custom ListAdapter with a view for each item.
From the image I can get the postion of the item in the ListView. From this I can call listView.setSelection(selectedPos). This positions the item at the top of the list. However I now want to get the view for this item; I have tried getFirstVisiblePosition() however it seems that this is not available until the list has been layed out (I am responding to a click on the image) so I cannot use this.
Nothing else I try works - all I want to do is to do something like listView.getViewAt(index) but I simply cannot find a way of doing it. I cannot use 'selected' as I am in touchable mode.
For what seems a very simple thing - I've wasted a day; any help would be greatly appreciated!
Cheers,
Neil
Shouldn't listView.getChildAt(position) do what you want?
Try creating your own custom adapter for the list view. For example:
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource, List<RowData> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//convertView holds the view for each item.
TextView txt = (TextView)convertView.findViewById(R.id.button);
//do some other stuff with the views
return convertView;
}
}
I got round this by doing the following:
I marked the item I wished to select in the adapter first by adding in my own method
Then when the view is requested I can check to see if the view is for a position at which I requested
I get then modify the presented view that is returned by GetView
This seems the simplest option.