2016-07-29 7 views
0

Я создаю приложение. мое приложение падает, когда я ввожу любое слово после предоставления места в autocompletetextview, а затем снова, когда я ввожу любое слово, а затем его авария. пожалуйста помогите. используя следующий код .........сбой приложения при использовании пробела в autocompletetextview

 public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, 
    LocationListener, AdapterView.OnItemClickListener { 

private GoogleMap mMap; 
    GoogleApiClient mGoogleApiClient; 
    Location mLastLocation; 
    Marker mCurrLocationMarker,FindMarker; 
    LocationRequest mLocationRequest; 
    AutoCompleteTextView atvPlaces; 
    DownloadTask placesDownloadTask; 
    DownloadTask placeDetailsDownloadTask; 
    ParserTask placesParserTask; 
    ParserTask placeDetailsParserTask; 
    LatLng latLng; 
    final int PLACES = 0; 
    final int PLACES_DETAILS = 1; 
    ListView lv; 
    ImageButton remove; 
    private boolean exit = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    // setSupportActionBar(toolbar); 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
    checkLocationPermission(); 
    } 

    FloatingActionButton fab = (FloatingActionButton)   findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View view) { 
    onLocationChanged(mLastLocation); 
    } 
    }); 

    SupportMapFragment mapFragment = (SupportMapFragment)  getSupportFragmentManager() 
    .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 


    remove = (ImageButton)findViewById(R.id.place_autocomplete_clear_button); 

    // Getting a reference to the AutoCompleteTextView 
    atvPlaces = (AutoCompleteTextView) findViewById(R.id.id_search_EditText); 
    atvPlaces.setThreshold(1); 

    // Adding textchange listener 
    atvPlaces.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    // Creating a DownloadTask to download Google Places matching "s" 
    placesDownloadTask = new DownloadTask(PLACES); 

    // Getting url to the Google Places Autocomplete api 
     String url = getAutoCompleteUrl(s.toString().trim()); 

    // Start downloading Google Places 
    // This causes to execute doInBackground() of DownloadTask class 

     placesDownloadTask.execute(url); 


     if (!atvPlaces.getText().toString().equals("")){ 
      lv.setVisibility(View.VISIBLE); 
      remove.setVisibility(View.VISIBLE); 
     }else { 
      lv.setVisibility(View.GONE); 
      remove.setVisibility(View.GONE); 
     } 


    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
    int after) { 
    // TODO Auto-generated method stub 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    // TODO Auto-generated method stub 
    } 
    }); 

    // Setting an item click listener for the AutoCompleteTextView dropdown list 
    /* atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int index, 
    long id) { 

    ListView lv = (ListView) arg0; 
    SimpleAdapter adapter = (SimpleAdapter) arg0.getAdapter(); 

    HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(index); 

    // Creating a DownloadTask to download Places details of the selected place 
    placeDetailsDownloadTask = new DownloadTask(PLACES_DETAILS); 

    // Getting url to the Google Places details api 
    String url = getPlaceDetailsUrl(hm.get("reference")); 

    // Start downloading Google Place Details 
    // This causes to execute doInBackground() of DownloadTask class 
    placeDetailsDownloadTask.execute(url); 

     InputMethodManager inputManager = (InputMethodManager) 
       getSystemService(Context.INPUT_METHOD_SERVICE); 

     inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
       InputMethodManager.HIDE_NOT_ALWAYS); 


    } 
    });*/ 
    lv=(ListView)findViewById(R.id.list); 

    lv.setOnItemClickListener(this); 


    setListenerOnWidget(); 
} 

private void setListenerOnWidget() { 
    View.OnClickListener listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     atvPlaces.setText(""); 
     } 
    }; 
    remove.setOnClickListener(listener); 
} 

@Override 
public void onBackPressed() { 
    if(exit){ 
     finish(); 
    }else { 
     Toast.makeText(this, "Tap Back again to Exit.", 
       Toast.LENGTH_SHORT).show(); 
     exit = true; 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       exit = false; 
      } 
     }, 3 * 1000); 

    } 
    } 


@Override 
public void onItemClick (AdapterView <?> adapterView, View view,int i, long l){ 
    //ListView lv = (ListView) adapterView; 
    SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter(); 

    HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(i); 

    // Creating a DownloadTask to download Places details of the selected place 
    placeDetailsDownloadTask = new DownloadTask(PLACES_DETAILS); 

    // Getting url to the Google Places details api 
    String url = getPlaceDetailsUrl(hm.get("reference")); 

    // Start downloading Google Place Details 
    // This causes to execute doInBackground() of DownloadTask class 
    placeDetailsDownloadTask.execute(url); 

    InputMethodManager inputManager = (InputMethodManager) 
      getSystemService(Context.INPUT_METHOD_SERVICE); 

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
      InputMethodManager.HIDE_NOT_ALWAYS); 

    String str = ((TextView) view.findViewById(R.id.place_name)).getText().toString(); 
    Toast.makeText(this,str, Toast.LENGTH_SHORT).show(); 
    atvPlaces.setText(str.replace(' ',',')); 

    lv.setVisibility(view.GONE); 


} 



private String getPlaceDetailsUrl(String ref) { 
    // Obtain browser key from https://code.google.com/apis/console 
    String key = "key=**************************************"; 

    // reference of place 
    String reference = "reference=" + ref; 

    // Sensor enabled 
    String sensor = "sensor=false"; 

    // Building the parameters to the web service 
    String parameters = reference + "&" + sensor + "&" + key; 

    // Output format 
    String output = "json"; 

    // Building the url to the web service 
    String url = "https://maps.googleapis.com/maps/api/place/details/" + output + "?" + parameters; 

    return url; 
    } 

private String getAutoCompleteUrl(String place) { 
    // Obtain browser key from https://code.google.com/apis/console 
    String key = "key=*********************************"; 

    // place to be be searched 
    String input = "input=" + place; 

    // place type to be searched 
    String types = "types=geocode"; 

    // Sensor enabled 
    String sensor = "sensor=false"; 

    // Building the parameters to the web service 
    String parameters = input + "&" + types + "&" + sensor + "&" + key; 

    // Output format 
    String output = "json"; 

    // Building the url to the web service 
    String url = "https://maps.googleapis.com/maps/api/place/autocomplete/" + output + "?" + parameters; 

    return url; 
    } 

    /** 
     * A method to download json data from url 
    */ 
private String downloadUrl(String strUrl) throws IOException { 
    String data = ""; 
    InputStream iStream = null; 
    HttpURLConnection urlConnection = null; 
    try { 
    URL url = new URL(strUrl); 

    // Creating an http connection to communicate with url 
    urlConnection = (HttpURLConnection) url.openConnection(); 

    // Connecting to url 
    urlConnection.connect(); 

    // Reading data from url 
    iStream = urlConnection.getInputStream(); 

    BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

    StringBuffer sb = new StringBuffer(); 

    String line = ""; 
    while ((line = br.readLine()) != null) { 
    sb.append(line); 
    } 

    data = sb.toString(); 

    br.close(); 

    } catch (Exception e) { 
    Log.d("Exception while downloading url", e.toString()); 
    } finally { 
    iStream.close(); 
    urlConnection.disconnect(); 
    } 
    return data; 
    } 

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public boolean checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) 
    != PackageManager.PERMISSION_GRANTED) { 

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
    Manifest.permission.ACCESS_FINE_LOCATION)) { 

    ActivityCompat.requestPermissions(this, 
    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
    MY_PERMISSIONS_REQUEST_LOCATION); 
    } else { 
    ActivityCompat.requestPermissions(this, 
    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
    MY_PERMISSIONS_REQUEST_LOCATION); 
    } 
    return false; 
    } else { 
    return true; 
    } 

    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
    } 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
    return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 

@Override 
    public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    //mLocationRequest.setInterval(1000); 
    //mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

    if (ContextCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

@Override 
public void onLocationChanged(Location location) { 
mLastLocation = location; 
if(mCurrLocationMarker != null){ 
    mCurrLocationMarker.remove(); 
} 

LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude()); 
MarkerOptions markerOption = new MarkerOptions(); 
markerOption.position(latLng); 
markerOption.title("Current Position"); 
markerOption.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
mCurrLocationMarker = mMap.addMarker(markerOption); 
Toast.makeText(this,"Location changed",Toast.LENGTH_SHORT).show(); 
CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(13).build(); 
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

if(mGoogleApiClient != null){ 
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this); 
} 

    loadNearByPlaces(location.getLatitude(), location.getLongitude()); 
    } 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 

    mMap = googleMap; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
    if (ContextCompat.checkSelfPermission(this, 
    android.Manifest.permission.ACCESS_FINE_LOCATION) 
    == PackageManager.PERMISSION_GRANTED) { 
    buildGoogleApiClient(); 
    mMap.setMyLocationEnabled(true); 

    } 
    } else { 
    buildGoogleApiClient(); 
    mMap.setMyLocationEnabled(true); 
    } 

    } 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
    .addConnectionCallbacks(this) 
    .addOnConnectionFailedListener(this) 
    .addApi(LocationServices.API) 
    .build(); 
    mGoogleApiClient.connect(); 
    } 

@Override 
public void onRequestPermissionsResult(int requestCode, 
    String permissions[], int[] grantResult) { 
    switch (requestCode) { 
    case MY_PERMISSIONS_REQUEST_LOCATION: { 

    if (grantResult.length > 0 
    && grantResult[0] == PackageManager.PERMISSION_GRANTED) { 
    if (ContextCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) 
    == PackageManager.PERMISSION_GRANTED) { 
    if (mGoogleApiClient == null) { 
    buildGoogleApiClient(); 
    } 
    mMap.setMyLocationEnabled(true); 
    } 

    } else { 
    Toast.makeText(this, "permisison denied", Toast.LENGTH_LONG).show(); 
    } 
    return; 
    } 

    } 
    } 

private void loadNearByPlaces(double latitude, double longitude) { 
//YOU Can change this type at your own will, e.g hospital, cafe,  restaurant.... and see how it all works 
    String type = "atm"; 
    StringBuilder googlePlacesUrl = 
    new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
    googlePlacesUrl.append("location=").append(latitude).append(",").append(longitude); 
    googlePlacesUrl.append("&radius=").append(PROXIMITY_RADIUS); 
    googlePlacesUrl.append("&types=").append(type); 
    googlePlacesUrl.append("&sensor=true"); 
    googlePlacesUrl.append("&key=" + GOOGLE_BROWSER_API_KEY); 

    JsonObjectRequest request = new JsonObjectRequest(googlePlacesUrl.toString(), 
    new Response.Listener<JSONObject>() { 
@Override 
public void onResponse(JSONObject result) { 

    Log.i(TAG, "onResponse: Result= " + result.toString()); 
    parseLocationResult(result); 
    } 
    }, 
    new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
    Log.e(TAG, "onErrorResponse: Error= " + error); 
    Log.e(TAG, "onErrorResponse: Error= " + error.getMessage()); 
    } 
    }); 

    AppController.getInstance().addToRequestQueue(request); 
    } 

    private void parseLocationResult(JSONObject result) { 

    String id, place_id, placeName = null, reference, icon, vicinity = null; 
    double latitude, longitude; 

    try { 
    JSONArray jsonArray = result.getJSONArray("results"); 

    if (result.getString(STATUS).equalsIgnoreCase(OK)) { 

    //mMap.clear(); 

    for (int i = 0; i < jsonArray.length(); i++) { 
    JSONObject place = jsonArray.getJSONObject(i); 

    id = place.getString(ATM_ID); 
    place_id = place.getString(PLACE_ID); 
    if (!place.isNull(NAME)) { 
    placeName = place.getString(NAME); 
    } 
    if (!place.isNull(VICINITY)) { 
    vicinity = place.getString(VICINITY); 
    } 
    latitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION) 
    .getDouble(LATITUDE); 
    longitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION) 
    .getDouble(LONGITUDE); 
    reference = place.getString(REFERENCE); 
    icon = place.getString(ICON); 

    MarkerOptions markerOptions = new MarkerOptions(); 
    LatLng latLng = new LatLng(latitude, longitude); 
    markerOptions.position(latLng); 
    markerOptions.title(placeName); 
      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory .HUE_BLUE)); 
     markerOptions.snippet(vicinity); 
    mMap.addMarker(markerOptions); 

     mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

      @Override 
      public View getInfoWindow(Marker arg0) { 

       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       View myContentsView = getLayoutInflater().inflate(R.layout.marker, null); 
       TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title)); 
       tvTitle.setText(marker.getTitle()); 
       TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet)); 
       tvSnippet.setText(marker.getSnippet()); 
       return myContentsView; 
      } 
     }); 

    } 

    Toast.makeText(getBaseContext(), jsonArray.length() + " ATM_FOUND!", 
    Toast.LENGTH_SHORT).show(); 
    } else if (result.getString(STATUS).equalsIgnoreCase(ZERO_RESULTS)) { 
    Toast.makeText(getBaseContext(), "No ATM found in 5KM radius!!!", 
    Toast.LENGTH_LONG).show(); 
    } 

    } catch (JSONException e) { 

    e.printStackTrace(); 
    Log.e(TAG, "parseLocationResult: Error=" + e.getMessage()); 
    } 
    } 

private class DownloadTask extends AsyncTask<String, Void, String> { 
private int downloadType = 0; 

// Constructor 
public DownloadTask(int type) { 
    this.downloadType = type; 
} 

@Override 
protected String doInBackground(String... url) { 
    // For storing data from web service 
    String data = ""; 

    try { 
     // Fetching the data from web service 
     data = downloadUrl(url[0]); 
    } catch (Exception e) { 
     Log.d("Background Task", e.toString()); 
    } 
    return data; 
} 

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    switch (downloadType) { 
     case PLACES: 
      // Creating ParserTask for parsing Google Places 
      placesParserTask = new ParserTask(PLACES); 

      // Start parsing google places json data 
      // This causes to execute doInBackground() of ParserTask class 
      placesParserTask.execute(result); 

      break; 

     case PLACES_DETAILS: 
      // Creating ParserTask for parsing Google Places 
      placeDetailsParserTask = new ParserTask(PLACES_DETAILS); 

      // Starting Parsing the JSON string 
      // This causes to execute doInBackground() of ParserTask class 
      placeDetailsParserTask.execute(result); 
    } 
} 
} 

    /** 
    * A class to parse the Google Places in JSON format 
    */ 
private class ParserTask extends AsyncTask<String, Integer,  List<HashMap<String, String>>> { 

int parserType = 0; 

public ParserTask(int type) { 
    this.parserType = type; 
} 

@Override 
protected List<HashMap<String, String>> doInBackground(String... jsonData) { 

    JSONObject jObject; 
    List<HashMap<String, String>> list = null; 

    try { 
     jObject = new JSONObject(jsonData[0]); 

     switch (parserType) { 
      case PLACES: 
       PlaceJSONParser placeJsonParser = new PlaceJSONParser(); 
       // Getting the parsed data as a List construct 
       list = placeJsonParser.parse(jObject); 
       break; 
      case PLACES_DETAILS: 
       PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser(); 
       // Getting the parsed data as a List construct 
       list = placeDetailsJsonParser.parse(jObject); 
     } 

    } catch (Exception e) { 
     Log.d("Exception", e.toString()); 
    } 
    return list; 
} 

    @Override 
protected void onPostExecute(List<HashMap<String, String>> result) { 

    switch (parserType) { 
     case PLACES: 
      String[] from = new String[]{"description"}; 
      int[] to = new int[]{R.id.place_name}; 

      // Creating a SimpleAdapter for the AutoCompleteTextView 
      //SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to); 

      // Setting the adapter 
      //atvPlaces.setAdapter(adapter); 

      ListAdapter adapter = new SimpleAdapter(MainActivity.this, result,R.layout.row,from,to); 
      // Adding data into listview 
      lv.setAdapter(adapter); 
      break; 
     case PLACES_DETAILS: 
      String location = atvPlaces.getText().toString(); 
      if (location != null && !location.equals("")) { 
       new GeocoderTask().execute(location); 


      } 

      break; 
    } 
} 
} 

    private class GeocoderTask extends AsyncTask<String, Void, List<Address>> { 

@Override 
protected List<Address> doInBackground(String... locationName) { 
    // TODO Auto-generated method stub 

    Geocoder geocoder = new Geocoder(getBaseContext()); 
    List<Address> addresses = null; 

    try { 
     // Getting a maximum of 3 Address that matches the input text 
     addresses = geocoder.getFromLocationName(locationName[0], 3); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return addresses; 
} 

protected void onPostExecute(List<Address> addresses) { 
    if(addresses==null || addresses.size()==0){ 
     Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show(); 
    } 
    for(int i=0;i<addresses.size();i++){ 
     Address address = (Address)addresses.get(i); 
     latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
     String addressText = String.format("%s, %s", 
       address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", 
       address.getCountryName()); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Find Location"); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
     FindMarker = mMap.addMarker(markerOptions); 

     CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(13).build(); 
     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     loadNearByPlaces(address.getLatitude(), address.getLongitude()); 


    } 

} 

    } 

    } 

мое приложение аварии, когда я ввести любое слово после того, как обеспечить пространство в AutoCompleteTextView, а затем еще раз, когда я ввести любое слово, то его крах ....

+0

Вы пытались отладить код и перейти к строке ошибки. –

+0

Отправьте сообщение об ошибке, пожалуйста. – Vucko

+0

java.lang.NullPointerException: попытка вызвать метод интерфейса 'int java.util.List.size()' в ссылке нулевого объекта в файле android.widget.SimpleAdapter.getCount (SimpleAdapter.java:100) в файле android.widget.ListView .setAdapter (ListView.java:491) на «im get this error – Arpit

ответ

0

Вашего list инициализируется нуль в

List<HashMap<String, String>> list = null; 

И если разбор пожар любого исключения, то список будет еще пустым, так как он не будет инициализирован & установки нулевого списка, чтобы адаптер будет производить NullPointerException по адресу getCount(). Поэтому просто убедитесь, что он инициализирован.

List<HashMap<String, String>> list = new ArrayList<>(); 

ВАЖНО

При использовании пространства & попытаться вызвать этот URL, он будет стрелять в FileNotFoundException, если не закодированы должным образом. В вашем случае URL url = new URL(strUrl); запускает это исключение при использовании символа пробела.

Самый простой способ - заменить пространство %20. В поле getAutoCompleteUrl() перед возвратом введите следующий код, чтобы заменить пробел %20.

private String getAutoCompleteUrl(String place) { 

    -------existing code---- 

    url = url.replaceAll(" ", "%20"); 
    return url; 
} 
+0

его рабочий, но после пробела не появляется никаких предложений при вводе какого-либо слова после пробела – Arpit

+0

пожалуйста, разрешите это – Arpit

+0

@Arpit См. Обновленный ответ – sJy

0

На самом деле я столкнулся с такой же ситуацией. Вы добавляете данные для автоматического заполнения текстового представления динамически, в основном так же, как Google для каждого слова, которое вы вводите, и получают данные с сервера, если я ошибаюсь, пожалуйста, исправьте меня.

Используйте этот код для заполнения данных в автозаполнениях TextView динамически с сервера в зависимости от категории

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

import android.app.Activity; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.MatrixCursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.SyncStateContract.Columns; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.util.Log; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.Filter; 
import android.widget.FilterQueryProvider; 
import android.widget.ListView; 

public class TestActivity extends Activity { 
    public Context mContext; 
    // views declaration 
    public AutoCompleteTextView txtAutoComplete; 
    public ListView lvItems; 
    // arrayList for Adaptor 
    ArrayList<String> listItems; 
    // getting input from AutocompleteTxt 
    String strItemName; 
    // making Adaptor for autocompleteTextView 
    ArrayAdapter<String> adaptorAutoComplete; 
    private static final int ADDRESS_TRESHOLD = 2; 
    private Filter filter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // for showing full screen 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_listitem); 
     mContext = this; 
     listItems = new ArrayList<String>(); 
     // Declaring and getting all views objects 
     Button btnShare = (Button) findViewById(R.id.ListItem_btnShare); 
     Button btnSort = (Button) findViewById(R.id.ListItem_btnSort); 
     lvItems = (ListView) findViewById(R.id.ListItem_lvItem); 
     txtAutoComplete = (AutoCompleteTextView) findViewById(R.id.ListItem_autoComplete); 

     // adding listeners to button 
     btnShare.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 
     btnSort.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     String[] from = { "name" }; 
     int[] to = { android.R.id.text1 }; 
     SimpleCursorAdapter a = new SimpleCursorAdapter(this, 
       android.R.layout.simple_dropdown_item_1line, null, from, to, 0); 
     a.setStringConversionColumn(1); 
     FilterQueryProvider provider = new FilterQueryProvider() { 
      @Override 
      public Cursor runQuery(CharSequence constraint) { 
       // run in the background thread 
       if (constraint == null) { 
        return null; 
       } 
       String[] columnNames = { Columns._ID, "name" }; 
       MatrixCursor c = new MatrixCursor(columnNames); 
       try { 

        // total code for implementing my way of auto complte 

        String SOAP_ACTION = "your action"; 
        String NAMESPACE = "your name space"; 
        String METHOD_NAME = "your method name"; 
        String URL = "your Url"; 
        SoapObject objSoap = null; 
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        // Use this to add parameters 
        request.addProperty("KEY", yourkey); 
        request.addProperty("Key", constraint); 
        request.addProperty("Key", Id); 
        // Declare the version of the SOAP request 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
          SoapEnvelope.VER11); 
        envelope.setOutputSoapObject(request); 
        envelope.dotNet = true; 

        HttpTransportSE androidHttpTransport = new HttpTransportSE(
          URL); 

        // this is the actual part that will call the webservice 

        androidHttpTransport.call(SOAP_ACTION, envelope); 
        // Get the SoapResult from the envelope body. 
        objSoap = (SoapObject) envelope.getResponse(); 
        if (objSoap != null) { 
         String strData = objSoap.toString(); 

        } 

        if (objSoap != null) { 
         System.out.println("getPropertyCountinevents//////////" 
           + objSoap.getPropertyCount()); 
         for (int i = 0; i < objSoap.getPropertyCount(); i++) { 
          Object obj = objSoap.getProperty(i); 
          if (obj instanceof SoapObject) { 
           SoapObject objNew = (SoapObject) obj; 

           c.newRow() 
             .add(i) 
             .add(objNew.getProperty("Itemname") 
               .toString()); 
          } 
         } 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return c; 
      } 
     }; 
     a.setFilterQueryProvider(provider); 
     txtAutoComplete.setAdapter(a); 

    } // on create ends 

} // final class ends 

заменить шахтный код получения данных от сервера к вашему т.е. призывающей WebServices через HTTP звонки, и он будет работать как шарм ,

+0

эта ссылка не открыта – Arpit

+0

Я отредактировал свой ответ –

Смежные вопросы