Show a Toast if the value exists in ArrayList


Ask by : Christian Wibowo November 09, 2012 09:12

I have a problem with an ArrayList, so i want to check ArrayList and compare it with a value from TextView and show a Toast if the value from TextView is already exist.

Here is some code that I've already tried:

Code 1

TextView namamenu=(TextView)findViewById(R.id.nama_menu2);
EditText jumlahmenu=(EditText)findViewById(R.id.txtJumlahMenu);
MenuInputClass sr = new MenuInputClass();
String hehe = namamenu.getText().toString();
String hihi = jumlahmenu.getText().toString();
sr.setNama_menu(hehe);
sr.setJumlah_menu(hihi);

//results is the arraylist
if(results.contains(hehe)) {
    Toast.makeText(Appetizer_Activity.this, "already exist"+hehe, Toast.LENGTH_LONG).show();
}

Code 2:

if(results.contains(sr)) {
    Toast.makeText(Appetizer_Activity.this, "already exist"+hehe, Toast.LENGTH_LONG).show();
}

my PemesananMenuClass : public class PemesananMenuClass extends Application {

    private static String nama_menu;
    private String harga_menu;
    private String keterangan_menu;
    private String gambar;

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

    public String getNama_menu() {
        return (nama_menu);
    }
    actually there are some other artibute

my ListPemesananActivity class :

public class ListPemesanan_Activity extends Activity {


        Button submit_list_pemesanan,submit_reservasi_nomeja;
        TextView cobacoba;                 
        ArrayList<String> list = new ArrayList<String>();
        ArrayAdapter<String> adapter;
        //public static ArrayList<MenuInputClass> searchResults = GetSearchResults();
        public static ListView list_pemesanan;
        protected static ListView lv;      

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_pemesanan_menu);

    //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
        // lv = (ListView) findViewById(R.id.list);
        submit_list_pemesanan=(Button)findViewById(R.id.btn_submit_list_pemesanan);
        submit_reservasi_nomeja=(Button)findViewById(R.id.btn_reservasi_meja);
        list_pemesanan=(ListView)findViewById(R.id.list);
        //final TextView tnamamenu = (TextView)findViewById(R.id.txtNamaMasuk);
        //EditText tnomeja = (EditText)findViewById(R.id.txtNoMeja);
        //final TextView tjumlahmenu = (TextView)findViewById(R.id.txtJumlahMasuk);

        OnClickListener listener = new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            for (int i = 0; i < list_pemesanan.getAdapter().getCount(); i++) {
                                View viewListPemesanan = list_pemesanan.getChildAt(i);
                                TextView txtNamaMenu = (TextView) viewListPemesanan.findViewById(R.id.txtNamaMasuk);
                                TextView txtJumlahMenu = (TextView) viewListPemesanan.findViewById(R.id.txtJumlahMasuk);
                                EditText txtNoMeja = (EditText)findViewById(R.id.txtNoMeja);
                                String masuk_namamenu = txtNamaMenu.getText().toString();
                                String masuk_jumlahmenu = txtJumlahMenu.getText().toString();
                                String masuk_nomeja = txtNoMeja.getText().toString();
                               // Toast.makeText(ListPemesanan_Activity.this, ""+cacad, Toast.LENGTH_LONG).show();            
                                initializeData(masuk_namamenu,masuk_jumlahmenu,masuk_nomeja);
                            }                              
                       }

                        private void initializeData(String masuk_namamenu,String masuk_jumlahmenu,String masuk_nomeja) {
                            // TODO Auto-generated method stub
                             ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                                 nameValuePairs.add(new BasicNameValuePair("nama_menu",masuk_namamenu));
                                 nameValuePairs.add(new BasicNameValuePair("jumlah",masuk_jumlahmenu));
                                 nameValuePairs.add(new BasicNameValuePair("no_meja",masuk_nomeja));
                                sendData(nameValuePairs);   
                        }

                        private void sendData(ArrayList<NameValuePair> data) {
                                 // TODO Auto-generated method stub
                                  // 1) Connect via HTTP. 2) Encode data. 3) Send data.
                                 try
                                 {
                                     HttpClient httpclient = new DefaultHttpClient();
                                     HttpPost httppost = new HttpPost("http://nervousme.vacau.com/android/insert_pesanan_new.php");
                                     httppost.setEntity(new UrlEncodedFormEntity(data));
                                     HttpResponse response = httpclient.execute(httppost);
                                 }
                                 catch(Exception e)
                                 {
                                     Log.e("log_tag", "Error:  "+e.toString());
                                 } 
                        }
                }; 
        submit_list_pemesanan.setOnClickListener(listener);    

        OnClickListener listener1 = new OnClickListener() {
            @Override
            public void onClick(View v) {

                    EditText txtNoMeja = (EditText)findViewById(R.id.txtNoMeja);

                    String masuk_nomeja = txtNoMeja.getText().toString();
                   // Toast.makeText(ListPemesanan_Activity.this, ""+cacad, Toast.LENGTH_LONG).show();            
                    initializeData(masuk_nomeja);

           }

            private void initializeData(String masuk_nomeja) {
                // TODO Auto-generated method stub
                 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);                 
                     nameValuePairs.add(new BasicNameValuePair("no_meja",masuk_nomeja));
                    sendData(nameValuePairs);   
            }

            private void sendData(ArrayList<NameValuePair>   data) {
                     // TODO Auto-generated method stub
                      // 1) Connect via HTTP. 2) Encode data. 3) Send data.
                     try
                     {
                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost("http://nervousme.vacau.com/android/reservasi_nomeja.php");
                         httppost.setEntity(new UrlEncodedFormEntity(data));
                         HttpResponse response = httpclient.execute(httppost);
                     }
                     catch(Exception e)
                     {
                         Log.e("log_tag", "Error:  "+e.toString());
                     } 
            }
    }; 

Any idea on why the toast doesn't appear?

View original question

Answer by : NeoNovember 09, 2012 04:38

Your arraylist contains MenuInputClass objects, and you have to traverse the list to find out if any object contains the strings that you need. Depending on if you want your Toast notification to appear once or multiple times, you might want to break from the loop.

You can try doing something like this.

for(MenuInputClass mic : results) {
    if(mic.getNama_menu().equals(hehe)) {
        Toast.makeText(Appetizer_Activity.this, "already exist"+hehe, Toast.LENGTH_LONG).show();
        // Uncomment the line below if you want Toast to show notifications once FOR EVERY appearance of the string
        // break;
    } else if (mic.getJumlha_menu().equals(hihi)) {
        Toast.makeText(Appetizer_Activity.this, "already exist"+hihi, Toast.LENGTH_LONG).show();
        // Uncomment the line below if you want Toast to show notifications once FOR EVERY appearance of the string
        // break;
    }
}
View original answer