How to fill AutoCompleteTextView in Android with an API call?

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:textColorHint="#000000">
    <AutoCompleteTextView
        android:id="@+id/t_place"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/baloo2_regular"
        android:hint="Enter 3 Digits atleast to show birth places"
        android:textColor="#000000"
        android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
import com.vedicastro.jyotishghar.R;
import ua.com.crosp.solutions.library.prettytoast.PrettyToast;
import static com.vedicastro.jyotishghar.Config.DATA_LOC;
/**
 * Created by Nanodalu on 11/6/2016.
 */
public class SignUp extends AppCompatActivity {
    ArrayAdapter<String> adapter;
 
    //JSON Array
    private JSONArray result;
    String  LATITUDE,LONGITUDE;
    //Initializing the ArrayList
    private final java.util.ArrayList<String>   exam_name = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);
   
        t_place = (AutoCompleteTextView) findViewById(R.id.t_place);
        t_place.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              //  getPlace((Editable) s);
                Log.d("P1-", "afterTextChanged: "+s);
            }
            @Override
            public void afterTextChanged(Editable s) {
              getPlace(s);
             //   adapter.notifyDataSetChanged();
                Log.d("P2-", "afterTextChanged: "+s);
            }
        });
        signin = (TextView) findViewById(R.id.submit);
     
       
    }
    private void getPlace(final Editable s) {
        final String locationselected=s.toString();
        if(locationselected.contains(" "))
        {
            locationselected.replace(" ", "%20");
        }
        pDialog1 = new SpotsDialog(SignUp.this, R.style.Custom);
      //  pDialog1.show();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_LOC,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("places:", "onResponse: " + response);
                        JSONObject j = null;
                //        hidePDialog1();
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);
                            result = j.getJSONArray("geonames");
                            //Calling method getexam_name to get the exam_name from the JSON Array
                            getexam_name(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                       // hidePDialog();
                        //Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                //PASSING STRING VALUE TO THE TAGGING PARAMETER WITH TENDS TO GO FOR TBALE NAME
                Map<String, String> params = new HashMap<String, String>();
                //     params.put(KEY_teacher_id,T_ID);
                params.put("place", locationselected);//
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    private void getexam_name(JSONArray j) {
        //Traversing through all the items in the json array-----------------------------------------------------------------------------------------
        exam_name.clear();
        //-----------------------------------------------------------------------------------------
        for (int i = 0; i < j.length(); i++) {
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);
                //Adding the name of the student to array list
                exam_name.add(json.getString("place_name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_item, exam_name);
        t_place.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
        adapter.notifyDataSetChanged();
        //Setting adapter to show the items in the spinner
        t_place.setThreshold(3);//will start working from first character
        t_place.setTextColor(Color.RED);
        t_place.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                String Selectionis = (String) parent.getItemAtPosition(position);
              //  Toast.makeText(getApplicationContext(), Selectionis, Toast.LENGTH_SHORT).show();
            }
        });
        // t_place.setAdapter(new ArrayAdapter<String>(SignUp.this, android.R.layout.simple_list_item_single_choice, exam_name));
      //  TastyToast.makeText(getApplicationContext(), "Latitude " + getLatitude(position), TastyToast.LENGTH_LONG, TastyToast.INFO);
      //  TastyToast.makeText(getApplicationContext(), "Longitude " +getlongitude(position), TastyToast.LENGTH_LONG, TastyToast.INFO);
    }
    //Method to get Latitude of a particular position
    public String getLatitude(int position){
        String Latitude="";
        try {
            //Getting object of given index
            JSONObject json = result.getJSONObject(position);
            //Fetching name from that object
            Latitude = json.getString("latitude");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Returning the name
        return Latitude;
    }
    //Method to get Latitude of a particular position
    public String getlongitude(int position){
        String longitude="";
        try {
            //Getting object of given index
            JSONObject json = result.getJSONObject(position);
            //Fetching name from that object
            longitude = json.getString("longitude");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Returning the name
        return longitude;
    }
  
 
    }
}
 
Post a Comment
0Comments