user3734013
user3734013

Reputation: 3

Get the arraylist stored id from the clickable item on the autocompletextview

So my problem is this I need to get the respective "id" field stored in my arrylist that correspond to the selected autocompletextview item and I can't do this. I parse data to a json format and store it in arraylist, and what I want is to click on the dropdown list of the autocompletetextview (already working) item, and get the respective id.

Json Data:

{"estab":[{"id":"00","design":"NULL"},
{"id":"01","design":"Continente de Viana do Castelo"},
{"id":"02","design":"Pingo Doce Viana do Castelo"},
{"id":"03","design":"Pingo Doce - Portuzelo (Viana Castelo)"},

Arraylist get and set:

public class Estbs {

 private String id;

private String design;



 public String getID() {
 return id;
 }

public void setID(String id) {
this.id = id;
}

 public String getDesign() {
 return design;
 }

public void setDesign(String design) {
 this.design = design;
 } 

}

main/json function

JSONArray jsonarray;
ProgressDialog mProgressDialog;
ArrayList<String> establist;
ArrayList<Estbs> estab;
 String x;



  private AutoCompleteTextView actv;



 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.add_linha);




 //Download JSON file AsyncTask
 new DownloadJSON().execute();

 }

//Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

 @Override
protected Void doInBackground(Void... params) {
// Locate the WorldPopulation Class 
estab = new ArrayList<Estbs>();
// Create an array to populate the spinner 
establist = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions
        .getJSONfromURL(url_donload_estbs);

try {
    // Locate the NodeList name
    jsonarray = jsonobject.getJSONArray("estab");
    for (int i = 0; i < jsonarray.length(); i++) {
        jsonobject = jsonarray.getJSONObject(i);

        Estbs estabs = new Estbs();
        estabs.setID(jsonobject.optString("id"));
        estabs.setDesign(jsonobject.optString("design"));
        estab.add(estabs);

        // Populate spinner with country names
        establist.add(jsonobject.optString("design"));

    }
} catch (Exception e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();
}
return null;
}

 @Override
 protected void onPostExecute(Void args) {
// Locate the spinner in activity_main.xml
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);


// Spinner adapter
actv
        .setAdapter(new ArrayAdapter<String>(Newlin_ProductActivity.this,
                android.R.layout.simple_list_item_1,
                establist));

// Spinner on item click listener
actv
        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0,
                    View arg1, int position, long arg3) {
                // TODO Auto-generated method stub
                // Locate the textviews in activity_main.xml
  TextView txtrank = (TextView) findViewById(R.id.design);

                // Set the text followed by the position 
                txtrank.setText("Rank : "
                        + estab.get(position).getID());

                x = estab.get(position).getID();

                Toast.makeText(getApplicationContext(), x,
                           Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
}
 }

Upvotes: 0

Views: 1232

Answers (2)

nilesh patel
nilesh patel

Reputation: 832

Please check bellow code :-

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) { 

      String selection = (String) parent.getItemAtPosition(position);

      int id;

      for (int i = 0; i < establist.size(); i++) {

        if(estab.get(i).getDesign().equals(selection))
             {
               id=estab.get(i).getId();
                break;
              }


}

}

Upvotes: 3

CodeWarrior
CodeWarrior

Reputation: 5176

In short onItemClicklistener() did the job but i would suggest you to have a look at the code below and try following such practise in daily coding.

public class MainActivity extends Activity implements OnItemClickListener {

    private ArrayList<String> establist;
    private ArrayList<Estbs> estab;
    private AutoCompleteTextView autoCompleteTextView;
    private TextView txtRank;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        autoCompleteTextView.setOnItemClickListener(this);
        txtRank = (TextView) findViewById(R.id.textView1);
        estab = new ArrayList<Estbs>();
        establist = new ArrayList<String>();
        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            for (int i = 0; i < 10; i++) {
                fillDataInEstabs();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            autoCompleteTextView.setAdapter(new ArrayAdapter<String>(
                    MainActivity.this, android.R.layout.simple_list_item_1,
                    establist));
        }
    }

    private void fillDataInEstabs() {
        Estbs e = new Estbs();
        e.setId(String.valueOf(count));
        e.setDesign("Hello " + String.valueOf(count));
        estab.add(e);
        establist.add(e.getDesign());
        count++;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
          String selection = (String) parent.getItemAtPosition(position);
          int actualPosition = estab.indexOf(selection);
          txtRank.setText("Rank : " + estab.get(actualPosition).getId());
          Toast.makeText(getApplicationContext(), estab.get(actualPosition).getId(),
                Toast.LENGTH_LONG).show();
    }
}

Upvotes: 0

Related Questions