如果ArrayList中存在该值,则显示Toast

时间:2012-11-04 02:43:04

标签: android arraylist

我对ArrayList有疑问,因此我想检查ArrayList并将其与TextView中的值进行比较,如果来自{Toast,则显示TextView {1}}已经存在。

以下是我已经尝试过的一些代码:

代码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();
}

代码2:

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

我的PemesananMenuClass:     公共类PemesananMenuClass扩展了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

我的ListPemesananActivity类:

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());
                     } 
            }
    }; 

为什么烤面包没有出现?

2 个答案:

答案 0 :(得分:1)

您的arraylist包含MenuInputClass对象,您必须遍历列表以查明是否有任何对象包含您需要的字符串。根据您是希望Toast通知出现一次还是多次,您可能希望从循环中断。

您可以尝试做这样的事情。

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;
    }
}

答案 1 :(得分:0)

在第一个示例(if(results.contains(hehe)))中,您询问hehe对象是否在数组列表中,而不是数组列表中的任何对象与{{1}具有相同的文本}。您可以有两个hehe包含完全相同的字符集,但它们是不同的对象。

如果您的目标是确定数组列表中的任何条目是否包含相同的文本,则需要比较每个对象包含的文本。例如,

String