2

我想直接从服务器上的数据中实现搜索。例如从 google.com 搜索。一个edittext,就像在谷歌的搜索栏中,edittext中的一个单词然后它会点击服务器并从该单词开始获取记录。

public class SearchActivity extends Activity {
TextView imeino;
Spinner select;
EditText searchby;
StringBuffer URL;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    imeino = (TextView)findViewById(R.id.getimeino);
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();
    imeino.setText(device_id);
    searchby = (AutoCompleteTextView)findViewById(R.id.editText1);
     //this is a spinner if i choose mobile from this then edittext only takes numeric value.and if a select name then it takes alphabets.
    select = (Spinner)findViewById(R.id.spinner1);
    select.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            if(select.getSelectedItem().toString().equals("Mobile"))
            {
                searchby.setInputType(InputType.     TYPE_CLASS_PHONE|InputType.TYPE_TEXT_VARIATION_PHONETIC);

            }
            if(select.getSelectedItem().toString().equals("name"))
            {
                searchby.setInputType(InputType.   TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

            }
            if(select.getSelectedItem().toString().equals("Address"))
            {
                searchby.setInputType(InputType. TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }
    });
this is my edittext in which i have to search for data

    searchby.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            searchby = (AutoCompleteTextView)findViewById(R.id.editText1);
            String getitemforsearch = searchby.getText().toString();
            SearchFromCustomerData(getitemforsearch);               
            }

        public void SearchFromCustomerData(String getitemforsearch) {
            String searchType= new String();
            if(select.getSelectedItem().toString().equals("Mobile"))
                {
                    searchType= "CustomerMobile";                       
                }
                if(select.getSelectedItem().toString().equals("Address"))
                {           
                    searchType= "CustomerAddress";                      
                }
                if(select.getSelectedItem().toString().equals("name"))
                {
                    searchType= "CustomerName";
                }

这是我访问网络服务的网址,它将显示选项

URL = new     StringBuffer("?"+searchType+"=" +     getitemforsearch+"&searchType="+searchType);
                    try {
                        URL sever = new URL(URL.toString());
                        System.out.println(URL.toString());
                        URLConnection yc = sever.openConnection();
                        InputStream in = yc.getInputStream(); // Get the data in the entity
                        InputStreamReader is = new InputStreamReader(in);
                        StringBuilder sb = new StringBuilder();
                        BufferedReader br = new BufferedReader(is);
                        String read = br.readLine();
                        while (read != null) {
                                sb.append(read);
                                read = br.readLine();
                        }
                        JSONObject jsonObject = new JSONObject(sb.toString());
                        JSONArray resultArray = jsonObject.getJSONArray("Customer");
                        System.out.println(" Length :" + resultArray.length());
                        String[] CustomerCode = new String[resultArray.length()];
                        String[] CustomerName = new String[resultArray.length()];
                        String[] CustomerEmail = new String[resultArray.length()];
                        String[] CustomerMobile = new String[resultArray.length()];
                        String[] Customeraddress = new String[resultArray.length()];
                        for (int i = 0; i < resultArray.length(); i++) {
                            JSONObject object = (JSONObject) resultArray.get(i);
                            System.out.println("true");
                       CustomerCode[i] = object.getString("CustomerCode") == null ? "" :    object.getString("CustomerCode");
                       CustomerName[i] = object.getString("CustomerName") == null ? "" : object.getString("CustomerName");
                       CustomerEmail[i] = object.getString("CustomerEmailId") == null ? "" : object.getString("CustomerEmailId");
                       CustomerMobile[i] = object.getString("CustomerMobile") == null ? "" : object.getString("CustomerMobile");
                       Customeraddress[i] = object.getString("CustomerAddress") == null ? "" : object.getString("CustomerAddress");                                            
                        }                      
                                }
             catch (Exception e) {
            }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
    }

这是我的布局文件..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SearchActivity" >

<TextView
    android:id="@+id/getimeino"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:hint="ImEiNo..." />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/getimeino"
    android:layout_marginTop="30dp"
    android:entries="@array/process_arrays"
    android:prompt="@string/process_prompt" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/getimeino"
    android:layout_centerHorizontal="true"
    android:ems="10" >

    <requestFocus />
 </EditText>

</RelativeLayout>
4

0 回答 0