• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get (array)value from AutoCompleteTextView

GWE

Lurker
An AutoCompleteTextView has been loaden from a database. By typing a few characters into the
AutoCompleteTextView matching results are shown in a pop-up. However, when I choose one of
the shown items, the application stops and didnot continue with the Intent.
I think it has something to do with the "textView.getSelectedItem()", but I donot know sure.

What am I doing wrong ?
Here is a code snippet:

private void populatetv() {
List labels = new ArrayList();
int i;

for (i = 1; i < list.size(); i++) {
labels.add(list.get(i) + "\n");
}

ArrayAdapter dataAdapter = new ArrayAdapter
(MainActivity.this, android.R.layout.simple_spinner_item, labels);


final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textView);
textView.setAdapter(dataAdapter);
textView.requestFocus();

tv.setText(R.string.zg);

textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
boolean firstPick = true;


public void onItemSelected(AdapterView textView, View view, int index, long id){
if (firstPick) {
firstPick = false;
} else {


String item = textView.getSelectedItem().toString();



Intent i = new Intent(MainActivity.this, ZoekGemeente.class);
i.putExtra("gem", item);

startActivity(i);
}
}

public void onNothingSelected(AdapterView arg0) {
Toast.makeText(getApplicationContext(), "Maak aub een keuze", Toast.LENGTH_LONG).show(); }
});

}
 
Either textView is null or textView.getSelectedItem() returns null.

No way to know without seeing the stack trace from the Logcat view.
 
Back
Top Bottom