2011-01-07 14 views
0

文字列内にデータが存在するかどうかを確認するために真または偽を返すブールメソッドがあります。ユーザーがすべてのデータを入力したり、ダイアログを実行しなかったりすると、すべて正常に機能します。.....しかし、ユーザーが「getItemsEditText」ダイアログポップアップにデータを入力しないで「OK」をクリックすると、このブール値は"pricePerItemText"には何も格納されていなくても、trueに解決されます。これはブールメソッドである:ここではEditTextを使用したJava/Androidダイアログ - 文字列のブールチェック

public Boolean doesAllDataExistCheckBool() 
{ 

    if (pricePerItemText != "" && itemsPerDayText != "" && sleepTimeText != "" && 
    wakeTimeText != "") 
    { 

     SharedPreferences.Editor editor = mySharedPreferences.edit 
     (); //opens shared preference editor 
     editor.putBoolean("storedDoesAllDataExist", true); 
     editor.commit(); //commit changes to mySharedPreferences 
     //End storing shared preferences 
     return true; 
    } 
    else 
    { 
     SharedPreferences.Editor editor = mySharedPreferences.edit 
     (); //opens shared preference editor 
     editor.putBoolean("storedDoesAllDataExist", false); 
     editor.commit(); //commit changes to mySharedPreferences 
     //End storing shared preferences 

     return false; 
    } 
} 

はブール値が真か偽かどうかを確認するためにテストされているところである。ここでは

if (position == 4) 
    { 
    allDataExists = doesAllDataExistCheckBool(); //checks if true or false 

    if (serviceStarted == true) 
    { 
     Context context = getApplicationContext(); 
     String text = "Schedule is already running"; 
     int duration = Toast.LENGTH_SHORT; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
    } 
    if (serviceStarted == false && doesAllDataExistCheckBool() == true) 
    { 
    startScheduleService(); 
    } 
    if (serviceStarted == false && doesAllDataExistCheckBool() == false) 
    { 
     Context context = getApplicationContext(); 
     String text = "Please enter all data before starting!"; 
     int duration = Toast.LENGTH_SHORT; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
    } 

} 

はのEditTextとOK /キャンセルボタンが付いたダイアログが書かれている方法です。

case ITEMS_PER_DAY : 

LayoutInflater li = LayoutInflater.from(this); 

final View itemsEntryView = li.inflate(R.layout.settings_dialog_input, (ViewGroup) 
findViewById(R.id.layout_root)); 

final EditText getItemsEditText = (EditText)itemsEntryView.findViewById 
(R.id.DialogEditText); 


return new AlertDialog.Builder(SettingsActivity.this) 

.setTitle("This is the title") 

.setView(itemsEntryView) 

.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int whichButton) 
    { 

    itemsPerDayText = getItemsEditText.getText().toString(); //gets input from 
    edittext and saves it to a string itemsPerDayText 


    //Initialize shared preferences 
    SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens editor 
    editor.putString("storedItemsPerDayText", itemsPerDayText); 
    editor.commit(); //commit changes to mySharedPreferences 
    //End storing shared preferences 

    } 
}) 
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int whichButton) 
    { 
    //user click cancel 
    } 
}).create(); 

これを行う別の方法はありますか?ユーザーが何も入力しなかった場合、なぜ「OK」をクリックすることができますか?何か案は?みんなありがとう!

答えて

1

あなたはあまりにも多くのコードを投稿しました。しかし、すぐに私はpricePerItemTextは、あなたがそれはあなたがJavaで文字列を比較する方法ではない、という含まれていませんでしたので、私たちは本当に分からない文字列、であると仮定すると、この

pricePerItemText != "" 

に気づきました。それは

!pricePerItemText.equals(""); 

編集する必要があります:

Javaでは、==オペレータはオブジェクトの参照ではなく、値を比較します。 mytext変数はほとんど間違いなくここで、「テキスト」のポイントには同じではありませんいくつかのメモリ位置、を指しているので、そう

String mytext = "text"; 
if (mytext == "text"){ print "True"} 

は真印刷することはありません。

"text == "text" 

が真であるという事実は、それは新しい文字列を再割り当てする必要がありませんので、文字列プールを維持するJavaのアーティファクトです。これは混乱の大きな原因です。

はここ

http://leepoint.net/notes-java/data/expressions/22compareobjects.html

+0

おかげFalmarriそれはおそらくより良い説明ランダムリンクです。すべてのコードの投稿については申し訳ありません。私はそれを論理的にすべて実行できるかどうかを誰が見ているかを確認したかっただけです。私はそれが何かばかげたことを知っていた。はい、あなたはすでにこれを知っているでしょう。私はJava NOOBです。 – dell116

+0

@ dell116:あまりにも多くのコードとあまりにも小さなコードを投稿することは、常にトレードオフです。理想的には、問題を示す最小限のコンパイル可能なコードを投稿する必要があります。しかし、それはアンドロイドで厄介です。 – Falmarri

+0

ちょっとわかりました....私のコードでpricePerItemTextが文字列であることを明示していませんでしたが、私の記述で文字列をテストしていると指定しました。 – dell116

関連する問題