Should've been easy: Autocomplete text in a dialog box

Published May 30, 2012
Advertisement
A few weeks ago I threw up a quick example about dynamically building Expandable Lists, using a simple app called TopicNotes. While it is simple, I like to tinker with it here and there to test features I'm unfamiliar with. About a week ago I was playing with TopicNotes and decided pretty quickly that it needed an autocomplete feature for entering Topics to help avoid unintentional multiple versions of the same topic (I found I had a 'Study hall' topic and a 'Study Hall' topic). I could have done a variety of string checks to avoid this, but decided that adding an AutoCompleteTextView would be the most straightforward way to go. There are a number of good tutorials and examples of using AutoCompleteTextView readily available online. It is very easy to implement in any typical Activity.

However, when you use it in a dialog box, you have to explicitly tie it to the desired dialog in a way that isn't immediately evident (at least to me) in the available tutorials. My dialog box crashed time after time, and I had narrowed my problem down to the ArrayAdapter initially. As it turned out, it was a domino effect: the ArrayAdapter was crashing because the app was apparently confused about who owned the findViewById that was taking the dialog layout that was getting the AutoCompleteTextView.

Anyway, here's the code that works, in case anyone else finds themselves in this situation.

[source lang= "java"]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// topicnotes database
topic_notes_data = new TopicNotesData(this);

// The button to add new notes
final Button btnAddTopicNote = (Button) findViewById(R.id.Button_AddTopicNote);
btnAddTopicNote.setOnClickListener(new View.OnClickListener() {

// Here's where the custom dialog is created
@Override
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.addtopicnotedlg);
dialog.setTitle("Add Topic Note");

// this is where we need to have dialog explicitly call findViewById
final AutoCompleteTextView topicText = (AutoCompleteTextView) dialog.findViewById(R.id.AutoCompleteTopic);
ArrayAdapter adapter = new ArrayAdapter(context,R.layout.list_item,getAllTopics());
topicText.setAdapter(adapter);

Button saveButton = (Button)dialog.findViewById(R.id.Button_SaveTopicNote);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(topicText.getText() != null){
String strTopic = topicText.getText().toString();
EditText noteText = (EditText)dialog.findViewById(R.id.EditText_Note);

String strNote = noteText.getText().toString();

}
dialog.dismiss();
}
});
Button cancelButton = (Button)dialog.findViewById(R.id.Button_CancelTopicNote);
cancelButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

dialog.dismiss();
}
});

dialog.show();

}
});
}
[/source]
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement