如何在android中为StringTokenizer使用两个分隔符

时间:2014-07-17 07:18:12

标签: android regex

  

大家好,我坚持如何使用两个分隔符   android中的StringTokenizer,用于将空值存储到db中   像这样的字符串,string1 = IMPS 2223 9481851276 7654321,,33

     

但是在使用stringtokeniser时缺少空字段

     

我想在这样的db中存储结果,                   IMPS                   2223                   9481851276                   7654321                   空值                   空值                   33

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.getactivity_layout);

            LinearLayout layout = (LinearLayout) findViewById(R.id.details);
            sms = getIntent().getStringExtra("value");
            merchantName = getIntent().getStringExtra("merchantName");

            tv = (TextView) findViewById (R.id.textview_getactivity_key);
            tv.setText("Enter the Detail for-"+merchantName);

            keyText = (EditText) findViewById(R.id.edittext_getactivity_value); 

            //finalKey = "Pay"+" "+merchantName+"-";

            keyText.setText(finalKey);

            View submitButton = findViewById(R.id.finish);

            inputMap = new LinkedHashMap<String, EditText>();

            Pattern p = Pattern.compile("\\|.*?\\|");
            Matcher m = p.matcher(sms);

            while (m.find())
            {
                //String to be replaced
                String s = m.group(0);
                Log.d("TestTag", "Value of string in getdetails"+s);
                if (!" ".equals(s.replaceAll("\\|", "")))
                {
                TextView t = new TextView(this);
                t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                t.setText(s.replaceAll("\\|", ""));
                t.setPadding(0, 0, 0, 5);
                t.setTextColor(getResources().getColor(android.R.color.black));

                EditText et = new EditText(this);
                et.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                //et.setInputType(1234);

                layout.addView(t);
                layout.addView(et);

                inputMap.put(s, et);
                }

            }

            submitButton.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {

                    keyValue = keyText.getText().toString().trim();

                    ArrayList<String> tempArray = new ArrayList<String>();
                        String string=null;
                        if(sms != null && inputMap != null){

                        for(String s : inputMap.keySet()){

                            String inputValue = inputMap.get(s).getText().toString().trim();
                            if(inputValue.equalsIgnoreCase("") || inputValue.equals(null)){
                                //showDialog(s.replaceAll("\\|", "")+ " is mandatory.", inputMap.get(s));
                                return;
                                //sms = sms.replace(s, inputValue);
                                //tempArray.add(sms.replace(s, inputValue));
                            }
                        else{
                                sms = sms.replace(s, inputValue);
                                tempArray.add(sms.replace(s, inputValue));
                            }


                            string = sms.replaceAll("\\|\\s\\|",",");

                        }   

                        StringTokenizer tokens = new StringTokenizer(string, " ");


                        String first = tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String second =tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String third = tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String fourth =tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String fifth = tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String sixth = tokens.hasMoreTokens() ? tokens.nextToken() : null;
                        String seventh =tokens.hasMoreTokens() ? tokens.nextToken() : null;

                        Log.d("Test123", "Split String is : "+first+" "+second+" "+third+" "+fourth+" "+fifth+" "+sixth+" "+seventh);



                        in.setKeyValue(keyValue);
                        in.setSmsKeyword(first);
                        in.setCustomerAccountNo(second);
                        in.setMobileNo(third);
                        in.setMmid(fourth);
                        in.setAmount(fifth);
                        in.setCustomerPin(sixth);
                        in.setRemarks(seventh);
                        in.setTempName(merchantName);

                        accessDB.open();
                        accessDB.insertMerchant(in);
                        accessDB.close();

                        Intent detailsIntent = new Intent(GetDetailsActivity.this,MainScreenTab.class);
                        detailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(detailsIntent);
                    }

0 个答案:

没有答案