2016-05-20 8 views
0

これはAndroidスタジオのAndroid Appパーソナリティクイズに書いたコードです私のGUIウィンドウが正しく動作し、正しく電話機にリンクしていて、よく見えます。ただし、投稿ボタン(強制的にアップロード)をクリックすると、正しい返信を入力すると、別の画像が表示されません。 ImageSetterをスイッチケースから取り出して、それがAppに表示されるので、私の写真では問題ではないことは分かっています。だから私が来たという結論は、何らかの形で、GUIのウィンドウのEditTextフィールドボックスからテキストを読み込むためのコードが正しくないか、間違っているかをチェックする文字リストを作成しているということです。 (はい、私はそれを文字列にするためにただarraylistに置くことを知っていますが、私はそれが私が考えていた最初のやり方でした。最高のは、私はそれを感謝Androidスタジオアプリ開発文字列入力またはセッティングイメージリソースを受信

`

import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    EditText nameTxt, answer2, answer3, answer4, answer5, answer6, answer7, answer8; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ImageView imageview = (ImageView)findViewById(R.id.showCharacter); 


     //have to cast it into type (EditText) since by default is is a GUI container and an error 
     //will be generated if you do not again specify that the container is type EditText 
     nameTxt = (EditText) findViewById(R.id.inputA1);//links nameTxt to GUI editText field 
     answer2 = (EditText) findViewById(R.id.inputA2);//links nameTxt to GUI editText field 
     answer3 = (EditText) findViewById(R.id.inputA3);//links nameTxt to GUI editText field 
     answer4 = (EditText) findViewById(R.id.inputA4);//links nameTxt to GUI editText field 
     answer5 = (EditText) findViewById(R.id.inputA5);//links nameTxt to GUI editText field 
     answer6 = (EditText) findViewById(R.id.inputA6);//links nameTxt to GUI editText field 
     answer7 = (EditText) findViewById(R.id.inputA7);//links nameTxt to GUI editText field 
     answer8 = (EditText) findViewById(R.id.inputA8);//links nameTxt to GUI editText field 


     ArrayList<String> responses = new ArrayList<String>();//string array to hold temp responses 

     String respStr = "";//used for comparison to the answer key since it is easier than using an 
     //arraylist for comparison. 

     String character = "";// honestly this doesnt output anything or do anything except help me 
     //keep track of which key corresponds to which picture so I dont get confused 

     //creates a new button called addBtn and likes it to the GUI button btnAdd 
     final Button addBtn = (Button) findViewById(R.id.btnAdd); 
     if (addBtn != null) {//designed to not allow the submission button to be clickable unless 
      //the first field is filled out 
      addBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
       public void onClick(View view) { 
       Toast.makeText(getApplicationContext(),"Using the Force", Toast.LENGTH_SHORT).show(); 
       } 
      }) ; 
     } 

     nameTxt.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int start, int  before, int count) { 
       addBtn.setEnabled(String.valueOf(nameTxt.getText()).trim().length() > 0); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
     //takes the input of the first question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant wway to ensure that input from the 
     //user can be either upper or lower case. 
     String q1S = ""; 
     q1S = nameTxt.getText().toString(); 
     q1S = q1S.trim().toLowerCase(); 
     if (q1S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q1S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q1S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q1S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q1S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 
     for(int i =0; 0>responses.size(); i++){ 

      respStr += responses.get(i); 
     } 


     //takes the input of the second question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant wway to ensure that input from the 
     //user can be either upper or lower case. 
     String q2S = ""; 
     q2S = nameTxt.getText().toString(); 
     q2S = q2S.trim().toLowerCase(); 
     if (q2S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q2S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q2S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q2S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q2S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 

     //takes the input of the third question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q3S = ""; 
     q3S = nameTxt.getText().toString(); 
     q3S = q3S.trim().toLowerCase(); 
     if (q3S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q3S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q3S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q3S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q3S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 


     //takes the input of the fourth question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q4S = ""; 
     q4S = nameTxt.getText().toString(); 
     q4S = q4S.trim().toLowerCase(); 
     if (q4S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q4S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q4S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q4S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q4S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 

     //takes the input of the fifth question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q5S = ""; 
     q5S = nameTxt.getText().toString(); 
     q5S = q5S.trim().toLowerCase(); 
     if (q5S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q5S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q5S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q5S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q5S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 


     //takes the input of the sixth question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q6S = ""; 
     q6S = nameTxt.getText().toString(); 
     q6S = q6S.trim().toLowerCase(); 
     if (q6S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q6S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q6S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q6S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q6S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 

     //takes the input of the seventh question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q7S = ""; 
     q7S = nameTxt.getText().toString(); 
     q7S = q7S.trim().toLowerCase(); 
     if (q7S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q7S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q7S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q7S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q7S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 

     //takes the input of the eigth question using getText() then it must convert getText which 
     //of type EditText to type string using toString. Then the variable is set to itself with 
     //no leading or trailing spaces using the .trim(0 and then made lowercase by using the 
     //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the 
     //user can be either upper or lower case. 
     String q8S = ""; 
     q8S = nameTxt.getText().toString(); 
     q8S = q8S.trim().toLowerCase(); 
     if (q8S.equalsIgnoreCase("a")){ 
      responses.add("a"); 
     } 
     if (q8S.equalsIgnoreCase("b")){ 
      responses.add("b"); 
     } 
     if (q8S.equalsIgnoreCase("c")){ 
      responses.add("c"); 
     } 
     if (q8S.equalsIgnoreCase("d")){ 
      responses.add("d"); 
     } 
     if (q8S.equalsIgnoreCase("e")){ 
      responses.add("e"); 
     } 
     //imageview.setImageResource(R.drawable.sidious); 




     switch(respStr){ 
      case "aabaacaa": 
       character ="Sideous"; 
       imageview.setImageResource(R.drawable.sidious); 
       break; 
      case "abdbadaa": 
       character ="Maul"; 
       imageview.setImageResource(R.drawable.maul); 
       break; 
      case "aadccdab": 
       character ="Vader"; 
       imageview.setImageResource(R.drawable.vader); 
       break; 
      case "bbbdcaaa": 
       character ="Boba Fett"; 
       imageview.setImageResource(R.drawable.boba); 
       break; 
      case "abeeadba": 
       character ="Grevous"; 
       imageview.setImageResource(R.drawable.grievous); 
       break; 
      case "aaacbabb": 
       character ="Windu"; 
       imageview.setImageResource(R.drawable.windu); 
       break; 
      case "abacbbbb": 
       character ="Obi-Won-Kenobi"; 
       imageview.setImageResource(R.drawable.kenobi); 
       break; 
      case "bacbcaab": 
       character ="Solo"; 
       imageview.setImageResource(R.drawable.solo); 
       break; 
      case "bbaccaba": 
       character ="Clone"; 
       imageview.setImageResource(R.drawable.trooper); 
       break; 
      case "aacbbabb": 
       character ="Anakin"; 
       imageview.setImageResource(R.drawable.anakin); 
       break; 
      default: 
       character = "R2D2"; 
     } 


     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

`

答えて

0

あなたのforループが間違った条件を持っている

あなたのコード:。。

for(int i =0; 0>responses.size(); i++){ 
    respStr += responses.get(i); 
} 

次のようになります。

for(int i=0; i<responses.size(); i++){ 
    respStr += responses.get(i); 
} 
+0

あなたが最高ですありがとう!どのようなルーキーミス。 – BuddyTheElf

関連する問題