1

I'm using this code to add values from MySQL to my AutoCompleteTextView. This works, but I want that on select an item to open a new activity passing the id_poblacion value. I have tried many things, but I am able only to get the poblacion value, no the id_poblacion.

Poblacion.java

public class JsonParse {
    double current_latitude,current_longitude;
    public JsonParse(){}
    public JsonParse(double current_latitude,double current_longitude) {
        this.current_latitude=current_latitude;
        this.current_longitude=current_longitude;
    }
    public List<SuggestGetSet> getParseJsonWCF(String sName) {
        List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
        try {
            String temp=sName.replace(" ", "%20");
            URL js = new URL("my url"+temp);
            URLConnection jc = js.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
            String line = reader.readLine();
            JSONObject jsonResponse = new JSONObject(line);
            JSONArray jsonArray = jsonResponse.getJSONArray("results");
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject r = jsonArray.getJSONObject(i);
                ListData.add(new SuggestGetSet(r.getString("id_poblacion"),r.getString("poblacion")));
            }
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return ListData;
    }
}

public class SuggestGetSet {

    String id_poblacion, poblacion;
    public SuggestGetSet(String id_poblacion, String poblacion){
        this.setId(id_poblacion);
        this.setName(poblacion);
    }
    public String getId() {
        return id_poblacion;
    }
    public void setId(String id_poblacion) {
        this.id_poblacion = id_poblacion;
    }

    public String getName() {
        return poblacion;
    }
    public void setName(String poblacion) {
        this.poblacion = poblacion;
    }
}

protected static final String TAG = "SuggestionAdapter";
private List<String> suggestions;
public Poblaciones(Activity context, String nameFilter) {
    super(context, android.R.layout.simple_dropdown_item_1line);
    suggestions = new ArrayList<String>();
}

@Override
public int getCount() {
    return suggestions.size();
}

@Override
public String getItem(int index) {
    return suggestions.get(index);
}

@Override
public Filter getFilter() {
    Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            JsonParse jp = new JsonParse();
            if (constraint != null) {
                // A class that queries a web API, parses the data and returns an ArrayList<GoEuroGetSet>
                List<SuggestGetSet> new_suggestions = jp.getParseJsonWCF(constraint.toString());
                suggestions.clear();
                for (int i=0;i<new_suggestions.size();i++) {
                    suggestions.add(new_suggestions.get(i).getName());
                }

                // Now assign the values and count to the FilterResults object
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence contraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return myFilter;
}

Index.java

    super.onCreate(savedInstanceState);
    setContentView(R.layout.index);
    AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    acTextView.setAdapter(new Poblaciones(this,acTextView.getText().toString()));
    acTextView.setOnItemClickListener(new OnItemClickListener() { 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getApplicationContext(), NewActivity.class);
            intent.putExtra("id_poblacion", id_poblacion);
            startActivity(intent);
        }
    });

Thank you

David
  • 315
  • 1
  • 4
  • 8
  • use your adapter: parent.getAdapter().getItem(position) – pskink Jun 05 '14 at 06:53
  • I have tried it, but I get the name of the value (poblacion). And I want to get the id (id_poblacion) – David Jun 05 '14 at 07:51
  • add some logging in getParseJsonWCF to make sure you get the right data from the network – pskink Jun 05 '14 at 08:16
  • also you over complicated your code, see my answer here http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara how i did it in ~40 lines of code – pskink Jun 05 '14 at 08:20
  • pskink could you please explain me how to add logging? I'm new with android and I have no idea. Thank you. – David Jun 05 '14 at 23:00
  • use Log.d method for that – pskink Jun 06 '14 at 05:40

1 Answers1

0

Id_poblacion ---> suggestions.add(new_suggestions.get(i).getId());

Nestor
  • 1
  • 1
    Would you be able to explain what your line does? In this way, more SO user can receive benefit. :) – jazzurro Dec 05 '14 at 04:39