Reputation: 87
I'm trying to get input from a dialog that pops up after selection of a popup menu item. I don't quite understand how to get the user's text input so that I can set it to my TextView "ListName" (see the last method). The text that I get is this: android.widget.EditText@41f3abf8
MainActivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Method for activity events
public void onButtonClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG:
createLGPopup(v);
break;
case R.id.EditButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createEditButtonPopup(v);
break;
}
}
// findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
// TextView text = (TextView) findViewById(R.id.GetStarted);
// text.setText(choice);
// CreateLG Button's Popup Menu
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice.equals("Create List")) {
createListDialog();
}
else if (choice.equals("Create Group")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
// Create Edit Button's Popup Menu
public void createEditButtonPopup(View v) {
PopupMenu EditMenu = new PopupMenu(this, v);
EditMenu.getMenuInflater().inflate(R.menu.editlist_menu, EditMenu.getMenu());
EditMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice.equals("Edit List Name")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
else if (choice.equals("Clear List Items")) {
findViewById(R.id.GetStarted).setVisibility(View.VISIBLE);
//createListDialog();
}
else if (choice.equals("Delete List Name")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
return false;
}
});
EditMenu.show();
}
// Create List Dialog
public AlertDialog.Builder dialogBuilder;
private void createListDialog() {
dialogBuilder = new AlertDialog.Builder(this);
final EditText textInput = new EditText(this);
dialogBuilder.setTitle("Create list");
dialogBuilder.setMessage("Name your list: ");
dialogBuilder.setView(textInput);
dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView text = (TextView) findViewById(R.id.ListName);
text.setText(textInput.toString());
//TextView text = (TextView) findViewById(R.id.ListName);
//String input = textInput.getText().toString(); //(textInput.toString());
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
// add list to ListsButton
//findViewById(R.id.ListName). -> Change ListName text to created list
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
}
});
// Output
AlertDialog dialogue = dialogBuilder.create();
dialogue.show();
}
}
Upvotes: 1
Views: 593
Reputation: 8248
Building on what was discussed in the comments, you should be able to get it like this:
@Override
public void onClick(DialogInterface dialog, int which) {
TextView text = (TextView) findViewById(R.id.ListName);
text.setText(textInput.getText().toString());
// ...
ListName
), findViewById()
from within the dialog callback works since the dialog is defined as a nested class of the activity and thus findViewById()
is translated to MainActivity.this.findViewById()
so it should find ListName
fine. It also has to be cast to use methods specific to the TextView
class.textInput.toString()
, you'll be calling the toString()
method of a TextView
object. It looks like TextView
doesn't override toString()
, so this is the default Object#toString()
method which just returns the name of the type of the object plus its hash code (that's why you see android.widget.EditText@41f3abf8
). You need to get the CharacterSequence
from the view first (textInput.getText()
).Upvotes: 1
Reputation: 1973
You have to use the dialog's context when calling findViewById. Like this dialog.findViewById(R.id.ListName), instead of just findViewById(R.id.ListName). The dialog is passed into the onClick() method as a parameter just for this reason.
Upvotes: 0