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]