从autocompletetextview上的可点击项中获取arraylist存储的id

时间:2014-06-26 20:01:46

标签: android arraylist autocompletetextview

所以我的问题是这个我需要获得存储在我的arrylist中的相应“id”字段,该字段对应于所选的autocompletextview项目,我不能这样做。我将数据解析为json格式并将其存储在arraylist中,我想要的是单击autocompletetextview(已经工作)项的下拉列表,并获取相应的id。

Json数据:

{"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函数

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
            }
        });
}
 }

2 个答案:

答案 0 :(得分:3)

请检查以下代码: -

@覆盖

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;
              }


}

}

答案 1 :(得分:0)

简而言之onItemClicklistener()完成了这项工作,但我建议您查看下面的代码,并尝试在日常编码中遵循这样的做法。

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();
    }
}