how does one iterate through a view's children and remove all ImageViews with a Tag that StartsWith a particular string?
All examples I can find this iterator;
for (int pos = 0; pos < mat.getChildCount(); pos++)
{
Object tag = mat.getChildAt(pos).getTag();
if (tag != null)
{
String s = tag.toString();
if (s.startsWith("Z"))
{
mat.removeView(mat.getChildAt(pos));
}
}
}
do a test, then remove the object.
The issue is both 'pos' and getChildCount change throughout the process. If I want to remove the first item and then the 2nd item (which after the first remove is actually the first item) it won't work, as pos is now 1 (ie 2nd item).
thanks
There are a few options.
for (int pos = 0; pos < mat.getChildCount();) {
if (remove) {
mat.removeViewAt(pos);
continue;
}
// only increment if the element wasn't removed
pos++;
}
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
mat.removeViewAt(pos);
// balance out the next increment
pos--;
}
}
// don't remove views until after iteration
List<View> removeViews = new ArrayList<>();
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
removeViews.add(mat.getChildAt(pos));
}
}
for (View view : removeViews) {
mat.removeView(view);
}
// count in reverse
for (int pos = mat.getChildCount() - 1; pos >= 0; pos--) {
if (remove) {
mat.removeViewAt(pos);
}
}
Related
What would be the equivalent foreach loop look like in android java I am porting the below code to android .This for loops is working for c#.
foreach (XmlNode candidate in parent.ChildNodes)
{
if (candidate is XmlElement && candidate.Name == element.Name)
{
if (candidate == element)
{
return index;
}
index++;
}
}
below is my function for android which gets error in the for loop:
private static int FindElementIndex(Element element)
{
Node parentNode = element.getParentNode();
if (parentNode.equals(Node.DOCUMENT_NODE))
{
return 1;
}
Element parent = (Element)parentNode;
int index = 1;
//how should be the foreach of the below to be changed?
for (Node candidate : parent.getChildNodes()) {
if (candidate.equals(Node.ELEMENT_NODE) && candidate.getNodeName() == element.getNodeName())
{
if (candidate == element)
{
return index;
}
index++;
}
}
Log.d("Log_d","Couldn't find element within parent");
//throw new ArgumentException("Couldn't find element within parent");
}
// parent.ChilNodes is some type of colleciton like arraylist
some update it show be for not foreach
for(XmlNode candidate : parent.ChildNodes)
{
if (candidate instanceOf XmlElement && candidate.Name == element.Name)
{
if (candidate == element)
{
return index;
}
index++;
}
}
I changed the function and for loop in the function like this
private static int FindElementIndex(Element element)
{
Node parentNode = element.getParentNode();
if (parentNode.equals(Node.DOCUMENT_NODE))
{
return 1;
}
Element parent = (Element)parentNode;
int index = 1;
NodeList nodes = parent.getChildNodes();
for (int k = 0; k < nodes.getLength(); k++) {
Node candidate = nodes.item(k);
if (candidate.equals(Node.ELEMENT_NODE) && candidate.getNodeName() == element.getNodeName()) {
return index;
}
index++;
}
Log.d("Log_d","Couldn't find element within parent");
//throw new ArgumentException("Couldn't find element within parent");
return 0;
}
I have a List<Restaurant> restaurants (11 items). I want to get only 6 items from 11 items to show in gridview. Now, I loop all size(). How to loop to get only 6 items.
if (restaurants != null && restaurants.size() > 0) {
for (int i = 1; i < restaurants.size(); i++) {
adapter = new ItemAdapter(MainActivity.this, restaurants);
grid_view.setAdapter(adapter);
}
}
Best way,
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
Try this code.
List<Restaurant> restaurantsTemp=new ArrayList();
for (int i = 0; i < 6; i++) {
restaurantsTemp.add(restaurants.get(i));
}
adapter = new ItemAdapter(MainActivity.this, restaurantsTemp);
grid_view.setAdapter(adapter);
no need loop if your list already have 11 records.Pass third parameter in adapter constructer and set it into count return as 6 in the adapter.
adapter = new ItemAdapter(MainActivity.this, restaurants,6);
grid_view.setAdapter(adapter);
Hi you need to use this code, Also you don't need to set adapter in loop.
if (restaurants != null && restaurants.size() > 0) { // restaurants.size() (11 items)
List<Restaurant> modifiedList=new ArrayList();
for (int i = 1; i < restaurants.size(); i++) { // 11 items
modifiedList.add(restaurants.get(i-1));
if(modifiedList.size()==6){
break;
}
}
adapter = new ItemAdapter(MainActivity.this, modifiedList);
grid_view.setAdapter(adapter);
}
ArrayList<String> sixItem = new ArrayList<>;
if (restaurants.size() >= 6) { // restaurants.size() (11 items)
for (int i = 1; i < 7; i++) { // 11 items
sixItem.add(i-1);
}
} else {
for (int i = 1; i < restaurants.size(); i++) {
sixItem.add(i-1);
}
}
adapter = new ItemAdapter(MainActivity.this, sixItem);
grid_view.setAdapter(adapter);
if restaurants.size() is less than 6 it won't show error.
try this in your ItemAdapter getCount() or getItemCount()
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
I can't seem to figure out how to write this code more efficiently. I'm iterating through views to check validity (text entered) but i find myself casting way too much. According to eclipse i need to cast in order to access the methods on the view. Here's the code:
// Verify Drivers/Vehicles Entered
private boolean checkDriversVehiclesValidity() {
int viewCount = mContainerView.getChildCount();
for (int i = 0; i < viewCount; i++) {
View v = mContainerView.getChildAt(i);
if (v.getId() == R.id.driverVehicleRow) {
for (int j = 0; j < ((LinearLayout) v).getChildCount(); j++) {
View v1 = ((ViewGroup) v).getChildAt(j);
if (v1 instanceof CustomAutoCompleteTextView) {
if (((CustomAutoCompleteTextView) v1).getError() != null) {
v1.requestFocus();
return false;
}
if (v1.getId() == R.id.drivers_field) {
String driverNumber = ((CustomAutoCompleteTextView) v1).getText().toString();
if ("".equals(driverNumber)) {
((CustomAutoCompleteTextView) v1).setError("Driver required");
v1.requestFocus();
return false;
}
} else if (v1.getId() == R.id.vehicles_field) {
String vehicleNumber = ((CustomAutoCompleteTextView) v1).getText().toString();
if ("".equals(vehicleNumber)) {
((CustomAutoCompleteTextView) v1).setError("Vehicle required");
v1.requestFocus();
return false;
}
}
}
}
}
}
return true;
}
For example, after checking for
if (v1 instanceof CustomAutoCompleteTextView)
you can be sure it IS an instance of CustomAutoCompleteTextView, so you can assign it to a properly typed variable like this:
CustomAutoCompleteTextView cv = (CustomAutoCompleteTextView)v1;
and use cv instead of ((CustomAutoCompleteTextView) v1) later.
I have a dynamic listview with one text and one checkbox per line.when i click a button.,i need to get all checked item names & Unchecked item names separately as arraylilst.How could i do that.Examples are much better..
I used..
SparseBooleanArray checked = mainlw.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i) == true) {
Planet tag = (Planet) mainlw.getItemAtPosition(checked.keyAt(i));
String selectedName=tag.getName();
Toast.makeText(getApplicationContext(), selectedName, Toast.LENGTH_SHORT).show();
}
}
Try this out and implement this logic according to your requirement.
int cntChoice = myList.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked += myList.getItemAtPosition(i).toString() + "\n";
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked+= myList.getItemAtPosition(i).toString() + "\n";
}
}
use CHOICE_MODE_MULTIPLE in your ListView and use getCheckedItemPositions() to get the checked ones.
So Onclick of button u can do this,From this you will get the items that are checked:-
#Override
public void onClick(View v)
{
System.out.println("check"+getListView().getCheckItemIds().length);
for (int i = 0; i < getListView().getCheckItemIds().length; i++)
{
System.out.println(getListView().getAdapter().getItem((int)getListView().getCheckItemIds()[i]).toString());
}
}
I have a multiselection ListView in my android app. What I need to know is how many items on that list I have selected.
Use getCheckedItemCount() of ListView. This gives you directly an int.
I found a solution.
getCheckedItemPositions() will create a SparceBooleanArray. So it is just to count the elements which are true in that array.
Example:
SparseBooleanArray positions = theListView.getCheckedItemPositions();
int counter = 0;
if (positions != null) {
int length = positions.size();
for (int i = 0; i < length; i++) {
if (positions.get(positions.keyAt(i))) {
counter++;
}
}
}
Instead of using keyAt followed by get, we can use valueAt directly.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}