2013-05-05 4 views
0

надеюсь, что я просто передаю что-то глупое.EditText getText возвращает пусто

ПРОБЛЕМА: my EditText (2) getText() возвращает пустую строку "" @ runtime В ПРОСЛУШИВАНИИ. Пожалуйста, смотрите строки внутри onClick (...)

Я подозреваю, что это должно быть с тем, как я раздуваю поискDialog и setContentView диалога после create() на Builder, но не могу понять это.

КОД:

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(); 
} 

И вот 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> 

Любая помощь будет очень высоко ценится.

+0

Как Вы находите, что 'tag' пуст? Где и как вы его выводите? –

ответ

0

Попробуйте что-то вдоль линий следующее:

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(); 
} 
+0

Эй, спасибо за ваш ответ. Проверьте первые две строки внутри метода onClick. Я делаю это, но бот возвращает пустые строки "". – Fer

+0

@ Потрясающе, я видел только заголовок и первые несколько строк. Я обновлю свой код за мгновение. – TronicZomB

+0

@Fer обновлен, также я не уверен, что модификатор 'final' будет иметь какое-либо отношение к проблеме. Но с тех пор, как диалог еще не создан, представление не привязано к нему, поэтому 'findViewById' вернет значение null. – TronicZomB

1

Попробуйте код->

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); 
Смежные вопросы