How to iterate through a view's elements
I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "layout"):
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
boolean success = formIsValid(layout);
public boolean formIsValid(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof EditText) {
//validate your EditText here
} else if (v instanceof RadioButton) {
//validate RadioButton
} //etc. If it fails anywhere, just return false.
}
return true;
}
How to iterate through a view's elements
I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "layout"):
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
boolean success = formIsValid(layout);
public boolean formIsValid(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof EditText) {
//validate your EditText here
} else if (v instanceof RadioButton) {
//validate RadioButton
} //etc. If it fails anywhere, just return false.
}
return true;
}
How to iterate through all activity components(views) in Android
this may help you...
public ArrayList<Button> getButtons() {
ArrayList<Button> buttons = new ArrayList<Button>();
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
findButtons(viewGroup, buttons);
return buttons;
}
private static void findButtons(ViewGroup viewGroup,ArrayList<Button> buttons) {
for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
findButtons((ViewGroup) child, buttons);
} else if (child instanceof Button) {
buttons.add((Button) child);
}
}
}
How do iterate through all the views/items in a RecyclerView?
RecyclerView views are created/destroyed as needed when you scroll. You cannot rely on them being available.
I assume you are trying to retrieve the selection state. It is better to store this within your object in the adapter and update its value upon clicking a radio button.
Then you can implement a getItem method in your adapter that returns the object and it's current selection state.
Loop through all subviews of an Android view?
@jqpubliq Is right but if you really want to go through all Views you can simply use the getChildCount()
and getChildAt()
methods from ViewGroup
. A simple recursive method will do the rest.
Android : How iterate through views of a expandableList
Add new Class CustomChilds
package stack.buy.com.stackdemos;
/**
* Created by Desktop - Ganesh on 10/14/2016.
*/
public class CustomChilds {
String Quntity;
String Unit;
String Note;
String GroupName;
public String getQuntity() {
return Quntity;
}
public void setQuntity(String quntity) {
Quntity = quntity;
}
public String getUnit() {
return Unit;
}
public void setUnit(String unit) {
Unit = unit;
}
public String getNote() {
return Note;
}
public void setNote(String note) {
Note = note;
}
public String getGroupName() {
return GroupName;
}
public void setGroupName(String groupName) {
GroupName = groupName;
}
public CustomChilds(String quntity, String unit, String note, String groupName) {
Quntity = quntity;
Unit = unit;
Note = note;
GroupName = groupName;
}
public CustomChilds() {
}
}
Adapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<CustomChilds>> _listDataChild;
// private final HashMap<String, String, String> mCheckedItems;
String cat, item, quty, units, notes;
ArrayList<String> ss = new ArrayList<>();
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<CustomChilds>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public CustomChilds getChild( int groupPosition, int childPosititon){
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId ( int groupPosition, int childPosition){
return childPosition;
}
@Override
public View getChildView ( final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent){
final CustomChilds childText =getChild(groupPosition, childPosition);
final ViewHolder holder;
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.mm = (LinearLayout) row.findViewById(R.id.entries);
holder.qty = (EditText) row.findViewById(R.id.qty1);
holder.note = (EditText) row.findViewById(R.id.note);
holder.unit = (Spinner) row.findViewById(R.id.unit);
holder.cb = (CheckBox) row.findViewById(R.id.cb);
holder.txtListChild = (TextView) row
.findViewById(R.id.lblListItem);
row.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) row.getTag();
}
List<CustomChilds> temp = _listDataChild.get(this._listDataHeader.get(groupPosition));
temp.set(childPosition,new CustomChilds(holder.qty.getText().toString(),holder.unit.getSelectedItem().toString(),holder.note.getText().toString(),childText.GroupName));
final int grp = groupPosition;
holder.cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
holder.mm.setVisibility(View.VISIBLE);
} else {
holder.mm.setVisibility(View.GONE);
}
System.out.println(ss);
}
});
holder.txtListChild.setText(childText.getGroupName());
return row;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
// somewhere else in your class definition
static class ViewHolder {
EditText qty;
EditText note;
Spinner unit;
LinearLayout mm;
CheckBox cb;
TextView txtListChild;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<CustomChilds>> listDataChild;
Button save;
Button cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save = (Button) findViewById(R.id.save);
listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild);
expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
System.out.println("Save clicked");
for(int i = 0; i < listAdapter.getGroupCount();i++) {
for (int k = 0; k < listAdapter.getChildrenCount(i); k++) {
CustomChilds child = listAdapter.getChild(i, k);
String unit=child.getUnit();
String note=child.getNote();
String qut=child.getQuntity();
System.out.println("For i="+i+" and k="+k+" string value is="+child.getNote());
}
}
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<CustomChilds>>();
listDataHeader.add("Nokia");
listDataHeader.add("Apple");
listDataHeader.add("Samsung");
List<CustomChilds> nokia = new ArrayList<CustomChilds>();
nokia.add(new CustomChilds("","","","Nokia1"));
nokia.add(new CustomChilds("","","","Nokia2"));
List<CustomChilds> apple = new ArrayList<CustomChilds>();
apple.add(new CustomChilds("","","","Apple1"));
apple.add(new CustomChilds("","","","Apple2"));
List<CustomChilds> samsung = new ArrayList<CustomChilds>();
samsung.add(new CustomChilds("","","","Samsung1"));
samsung.add(new CustomChilds("","","","Samsung2"));
listDataChild.put(listDataHeader.get(0), nokia); // Header, Child data
listDataChild.put(listDataHeader.get(1), apple);
listDataChild.put(listDataHeader.get(2), samsung);
}
}
Get Unit, Quantity and Note onClick
save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
System.out.println("Save clicked");
for(int i = 0; i < listAdapter.getGroupCount();i++) {
for (int k = 0; k < listAdapter.getChildrenCount(i); k++) {
CustomChilds child = listAdapter.getChild(i, k);
String unit=child.getUnit();
String note=child.getNote();
String qut=child.getQuntity();
System.out.println("For i="+i+" and k="+k+" string value is="+child.getNote());
}
}
}
});
How do I iterate through a list in my View?
I just posted this example in another question. Try something like this,
<table class="results" width="100%" border="0" cellpadding="5">
<thead class="tablehead">
<tr>
<td width="55px"><b>Country</b></td>
<td width="55px"><b>State</b></td>
<td width="55px"><b>City</b></td>
<td width="55px"><b>Town</b></td>
<td width="55px"><b>Postal</b></td>
</tr>
</thead>
<tbody>
<%
int count = 0;
foreach (var item in (IEnumerable<MY_RESULTS>)ViewData["My_Results"])
{
if (count % 2 == 0) { Response.Write("<tr class='even'>"); } else { Response.Write("<tr class='odd'>"); }
%>
<td><%= Html.Encode(item.Country)%></td>
<td><%= Html.Encode(item.State)%></td>
<td><%= Html.Encode(item.City)%></td>
<td><%= Html.Encode(item.Town)%></td>
<td><%= Html.Encode(item.Postal)%></td>
</tr>
<% count += 1; } %>
</tbody>
</table>
Iterate through a boost::multi_array view
Here is one way to do this:
#include <iostream>
#include <boost/multi_array.hpp>
// Functor to iterate over a Boost MultiArray concept instance.
template<typename T, typename F, size_t Dimensions = T::dimensionality>
struct IterateHelper {
void operator()(T& array, const F& f) const {
for (auto element : array)
IterateHelper<decltype(element), F>()(element, f);
}
};
// Functor specialization for the final dimension.
template<typename T, typename F>
struct IterateHelper<T, F, 1> {
void operator()(T& array, const F& f) const {
for (auto& element : array)
f(element);
}
};
// Utility function to apply a function to each element of a Boost
// MultiArray concept instance (which includes views).
template<typename T, typename F>
static void iterate(T& array, const F& f) {
IterateHelper<T, F>()(array, f);
}
int main() {
boost::multi_array<char, 2> a{boost::extents[2][6]};
a[0][0] = 'B';
a[0][1] = 'o';
a[0][2] = 'o';
a[0][3] = 's';
a[0][4] = 't';
a[0][5] = '\0';
a[1][0] = 'L';
a[1][1] = 'i';
a[1][2] = 'o';
a[1][3] = 'n';
a[1][4] = 's';
a[1][5] = '\0';
typedef boost::multi_array<char, 2>::array_view<2>::type array_view;
typedef boost::multi_array_types::index_range range;
array_view b = a[boost::indices[range{0,2}][range{0,4}] ];
// Use the utility to apply a function to each element.
iterate(b, [](char& c) {
std::cout << c << std::endl;
});
return 0;
};
The code above defines a utility function iterate()
, to which you pass an object satisfying the Boost MultiArray concept (which includes views) and a function to apply to each element. The utility function works by using a Functor that iterates over each dimension recursively.
CoLiRu
Related Topics
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity
Change Viewpager Animation Duration When Sliding Programmatically
(Deprecated) Fragment Onoptionsitemselected Not Being Called
How to Use Scale Gesture Detector for Pinch Zoom in Android
Bitmap Is Returning Null from Bitmapfactory.Decodefile(Filename)
Disable Home Button in Android Toddler App
Check If the Sdcard Is Present, Boolean Is Always True
Using Enableautomanage() in Fragment
Save Sensitive Data in React Native
Genymotion Unable to Start the Genymotion Virtual Device
Prevent Ussd Dialog and Read Ussd Response
How to Check How Much Free Space an Sd Card Mounted on an Android Device Has
Android: Picasso Load Image Failed . How to Show Error Message