2

i want to compare arraylist with a textview, when the value of arraylist and textview is match/same, i want to show the index of the arraylist..

this is my code

TextView namamenu2=(TextView)findViewById(R.id.nama_menu2);         
String nama_menu_upd = namamenu2.getText().toString();

for(MenuInputClass mic2 : results) {    
    String comp=mic2.getNama_menu();
    if(nama_menu_upd.equals(comp))
    {                               
        int compIndex=results.indexOf(comp);
        Toast.makeText(Appetizer_Activity.this,compIndex, Toast.LENGTH_LONG).show();                                
    }       
} 

MenuInputClass

public class MenuInputClass {

    private String nama_menu;
    private String jumlah_menu;

    public void setNama_menu(String nama_menu) {
        this.nama_menu=nama_menu;
    }

    public String getNama_menu() {
        return (nama_menu);
    }
}

when i show compIndex on Toast, its always show -1.But when i try to show the size of result , its show correct size.how can i fix this?

1
  • 1
    Use For loop , Foreach will not let you know that index unless you take a int counter Commented Nov 30, 2012 at 5:34

1 Answer 1

2

Try normal For loop instead Foreach, Foreach will not let you know that index unless you take a int counter.

With Normal For:

for(int i=0; i< results.size(); i++) {    
        MenuInputClass mic2= results.get(i);
        String comp=mic2.getNama_menu();
        if(nama_menu_upd.equals(comp))
        {                               
            Toast.makeText(Appetizer_Activity.this, i, Toast.LENGTH_LONG).show();                                
        }       
    } 

With Foreach:

int counter=0;
for(MenuInputClass mic2 : results) {    
    String comp=mic2.getNama_menu();
    if(nama_menu_upd.equals(comp))
    {                               
        Toast.makeText(Appetizer_Activity.this, counter, Toast.LENGTH_LONG).show();                                
    }  
     counter++;     
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.