0

我对 a 有问题ArrayList,所以我想检查ArrayList它并将其与 from 的值进行比较,TextView并显示 aToast的值TextView是否已经存在。

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

代码 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 扩展应用程序 {

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

关于为什么吐司没有出现的任何想法?

4

2 回答 2

1

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

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

for(MenuInputClass 麦克风:结果){
    if(mic.getNama_menu().equals(hehe)) {
        Toast.makeText(Appetizer_Activity.this, "已经存在"+呵呵, Toast.LENGTH_LONG).show();
        // 如果您希望 Toast 在每次出现字符串时显示一次通知,请取消注释下面的行
        // 休息;
    } else if (mic.getJumlha_menu().equals(hihi)) {
        Toast.makeText(Appetizer_Activity.this, "已经存在"+hihi, Toast.LENGTH_LONG).show();
        // 如果您希望 Toast 在每次出现字符串时显示一次通知,请取消注释下面的行
        // 休息;
    }
}
于 2012-11-09T04:38:09.380 回答
0

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

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

for (String myString : results) {
    if (myString.compare(hehe)) {
        Toast.makeText(Appetizer_Activity.this, "already exist"+hehe, Toast.LENGTH_LONG).show();          
        break;
    }
]
于 2012-11-04T03:06:24.193 回答