我的搜索功能是一个词正常工作,但有两个词的短语,需要一个间距,它显示没有任何ex(我的名字)



伙计们,有人帮助我我的搜索功能是正常工作的一个词,但有两个词的短语,需要一个空格,它显示没有任何ex(我的名字)。

这是我做我的上下文更改函数的部分。活动

                public class Builds extends ListActivity {

                private ProgressDialog pDialog;
                Button b4;
                EditText inputSearch;

                // URL to get contacts JSON
                private static String url = "http://computersolutions.comli.com/android_json.php";

                // JSON Node names
                private static final String TAG_CONTACTS = "contacts";
                private static final String TAG_ID = "id";
                private static final String TAG_NAME = "name";
                private static final String TAG_EMAIL = "email";
                private static final String TAG_SAMPLE = "sample";

                // contacts JSONArray
                JSONArray contacts = null;
                // Hash map for ListView
                ArrayList<HashMap<String, String>> contactList =
                new ArrayList<HashMap<String, String>>();
                @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.builds);
                b4 = (Button) findViewById(R.id.btnReturn);
                b4.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent i = new Intent(getApplicationContext(),
                                UserPage.class);
                        startActivity(i);
                        finish();
                    }
                });
                // Calling async task to get JSON
                new GetContacts().execute();
                }
                /*
                * Async task class to get JSON by making HTTP call
                */
                private class GetContacts extends AsyncTask<Void, Void, Void> {
                @Override
                protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                pDialog = new ProgressDialog(Builds.this);
                pDialog.setMessage("Connecting to web server...");
                pDialog.setCancelable(false);
                pDialog.show();
                } 

                @Override
                protected Void doInBackground(Void... arg0) {
                // Creating service handler class instance
                ServiceHandler sh = new ServiceHandler();
                // Making a request to URL and getting response
                String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
                Log.d("Response: ", "> " + jsonStr);
                if (jsonStr != null) {
                try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_CONTACTS);
                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String sample = c.getString(TAG_SAMPLE);

                // temporary HashMap for single contact
                HashMap<String, String> contact =
                new HashMap<String, String>();
                // adding each child node to HashMap key => value
                contact.put(TAG_ID, id);
                contact.put(TAG_NAME, name);
                contact.put(TAG_EMAIL, email);
                contact.put(TAG_SAMPLE, sample);
                // adding contact to contact list
                contactList.add(contact);
                }
                } catch (JSONException e) {
                e.printStackTrace();
                }
                } else {
                Log.e("ServiceHandler", "Couldn't get any data from the URL!");
                }
                return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                pDialog.dismiss();
                // Updating parsed JSON data into ListView
                final ListAdapter adapter = new SimpleAdapter(Builds.this, contactList,
                R.layout.list_item2,
                new String[] { TAG_NAME, TAG_EMAIL,TAG_ID,TAG_SAMPLE},
                new int[] {R.id.name, R.id.email,R.id.id ,R.id.sample}
                );
                inputSearch = (EditText) findViewById(R.id.inputSearch);
                setListAdapter(adapter);

文本监视器

                inputSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before,int count) {
                        // When user changed the Text
                        TextView txt;
                        txt=(TextView)findViewById(R.id.pprice);
                        String a = inputSearch.getText().toString();
                        txt.setText("Results for "+a+":");
                        String st = s.toString().trim();  
                        ((SimpleAdapter) adapter).getFilter().filter(st);
                    }
                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub                          
                    }
                });
                }
                }
                }

我认为你正在使用的trim()函数从你想要搜索的字符串中删除空格。因此,尝试删除trim()函数,看看它是否工作。

相关内容

最新更新