EditText getText devuelve vacío

hopefully I am just passing something silly.

PROBLEM: my EditText (2) getText() return empty String "" @ runtime IN THE LISTENER. Please see the lines inside the onClick(...)

I suspect it has to be with how I am inflating searchDialog and setContentView of dialog after create() on the Builder, but can't figure it out.

CÓDIGO:

private void initSearch() {
    results = new ArrayList<Photo>();
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
    AlertDialog dialog = searchDialog.create();
    LayoutInflater inflater = this.getLayoutInflater();
    dialog.setContentView(R.layout.search_dialog);
    searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
    final EditText tagField = (EditText) dialog.findViewById(R.id.tagField);
    final EditText valueField = (EditText) dialog.findViewById(R.id.valueField);
    searchDialog.setTitle("Search Photos");
    searchDialog.setMessage("Specify tag and value...");
    searchDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {

        @Override
  public void onClick(DialogInterface dialog, int which) {
      try { // PROBLEM: first two lines
          String tag = tagField.getText().toString(); // TAG WILL BE EMPTY
    String value = valueField.getText().toString(); // VALUE WILL BE EMPTY
    String criteria = tag+":\""+value+"\"";
    ArrayList<String> tags = new ArrayList<String>();
          tags.add(criteria);
    HomeScreen.results = c.getPhotosByTag(tags);

    if(!tag.equalsIgnoreCase("person") || !tag.equalsIgnoreCase("location")){
        throw new IllegalArgumentException("Tag types can only be location or person");
    }else if(results.size() == 0) {
        throw new IllegalArgumentException("No results");
    }else {
        Intent intent = new Intent(HomeScreen.this,SearchResults.class);
        startActivity(intent);
          }
      } catch(Exception e) {
          dialog.dismiss();
    Utilities.createErrorDialog(HomeScreen.this, e.getMessage());
      }

        }
     });
    searchDialog.show();
}

Y aquí está el XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tagText" 
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/tag" />
    <EditText 
        android:id="@+id/tagField"
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
    <TextView
        android:id="@+id/valueText" 
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/value" />
    <EditText 
        android:id="@+id/valueField"
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
</LinearLayout>

Cualquier ayuda será muy apreciada.

preguntado el 05 de mayo de 13 a las 18:05

How are you finding that tag is empty? Where and how are you outputting it? -

2 Respuestas

Try this code->

LayoutInflater inflater = this.getLayoutInflater();
    View view = inflater.inflate(R.layout.search_dialog, null);
    final EditText tagField = (EditText) view.findViewById(R.id.tagField);
    final EditText valueField = (EditText) view.findViewById(R.id.valueField);
    searchDialog.setView(view);

contestado el 05 de mayo de 13 a las 18:05

Intente algo similar a lo siguiente:

private void initSearch() {
    results = new ArrayList<Photo>();
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
    AlertDialog dialog = searchDialog.create();
    LayoutInflater inflater = this.getLayoutInflater();
    View v = inflater.inflate(R.layout.search_dialog, null);
    searchDialog.setView(v);
    final EditText tagField = (EditText) v.findViewById(R.id.tagField);
    final EditText valueField = (EditText) v.findViewById(R.id.valueField);
    searchDialog.setTitle("Search Photos");
    searchDialog.setMessage("Specify tag and value...");
    searchDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        try { // PROBLEM: first two lines
              String tag = tagField.getText().toString(); // TAG WILL BE EMPTY
              String value = valueField.getText().toString(); // VALUE WILL BE EMPTY
              String criteria = tag+":\""+value+"\"";
              ArrayList<String> tags = new ArrayList<String>();
              tags.add(criteria);
              HomeScreen.results = c.getPhotosByTag(tags);

              if(!tag.equalsIgnoreCase("person") || !tag.equalsIgnoreCase("location")){
                  throw new IllegalArgumentException("Tag types can only be location or person");
              }else if(results.size() == 0) {
                  throw new IllegalArgumentException("No results");
              }else {
                  Intent intent = new Intent(HomeScreen.this,SearchResults.class);
                  startActivity(intent);
              }
        } catch(Exception e) {
            dialog.dismiss();
            Utilities.createErrorDialog(HomeScreen.this, e.getMessage());
        }

    }
    });
    searchDialog.show();
}

Respondido 01 ago 13, 12:08

Hey Tronic, thanks for your answer. Please check the first two lines inside the onClick method. I am doing that yet bot returns are empty strings "". - Hacer

@Fer ha my bad, I only saw the title and first few lines. I will update my code in a moment. - TronicZomB

@Fer updated, also I'm not sure if the final modifier will have anything to do with the problem. But you since the dialog is not yet created, the view is not attached to it so findViewById devolverá nulo. - TronicZomB

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.