2017-06-22 8 views
0

私は、指定された検索語または頭字語のテキストファイルを検索するアプリケーションを作成中です。これは私が実際に作成した最初のアプリです。テキストファイルの形式は次のとおりです:acronym%term%definition。私は、InputStreamとBufferedReaderを1行ずつファイルに通しています。私はそれをパーセント記号で分割し、私が作成したトリプルクラスを使用してトリプルに文字列を入れ、そのトリプルをArrayListに追加します。私の次の関数は、検索頭文字または用語を含むTripleのArrayListを検索することになっています。頭字語が見つからない場合は、InvalidAcronymアクティビティに移動します。存在する場合は、ValidAcronymアクティビティーに移動します。何らかの理由で、トリガされる唯一のアクティビティはInvalidAcronymアクティビティです。私はそれがArrayListに行を追加することと関係があると思います。私はもともとHashMapsを使用して頭字語と定義だけで作業していました。誰かが私を助けることができますか?ここに私のコードです:リストを検索して答えをAndroidアプリに表示

package com.redacted.laurenanderson.acronymlookup; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.util.Pair; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageButton; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 


public class Search extends AppCompatActivity{ 
    public EditText mEdit; 
    public static String currAc; 
    // public static HashMap<String,String> acros = new HashMap<String,String>(){{ 
    //add all currently known thingies. 

//}}; 
public static ArrayList<Triple> acros2 = new ArrayList<Triple>(); 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    InputStream is = this.getResources().openRawResource(R.raw.acronyms2); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

    String[] str; 
    //String FILENAME = "acronyms.txt"; 
    String FILENAME = "acronyms2.txt"; 



    try { 
     String s = reader.readLine(); 
     while (s != null){ 
      //str = s.split(" ",2); 
      //acros.put(str[0],str[1]); 
      //THIS IS GOING TO BE THE REPLACEMENT 
      str = s.split("%",3); 
      System.out.println(str.toString()); 
      Triple<String,String,String> acro = new Triple<String,String,String>(str[0],str[1],str[2]); 
      acros2.add(acro); 
      s = reader.readLine(); 
     } 

    } 
    catch(IOException e){ 
     System.out.println("OH NO"); 
     return; 
    } 


    mEdit = (EditText)findViewById(R.id.acroterm); 
    //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.home); 
    //fab.setImageResource(R.drawable.ic_search); 
    ImageButton fab = (ImageButton) findViewById(R.id.search); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


     /* if(acros.containsKey(mEdit.getText().toString())){ 
       currAc = mEdit.getText().toString(); 
       Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
       view.getContext().startActivity(intent1); 
      } 
      else{ 
       Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
       view.getContext().startActivity(intent2); 
      }*/ 

      for(Triple triple: acros2){ 
       currAc = mEdit.getText().toString(); 
       if(triple.contains(currAc)){ 
        Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
        view.getContext().startActivity(intent1); 
       } 

      } 
      Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
      view.getContext().startActivity(intent2); 


     } 
    }); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 
/*public HashMap<String,String> getMap(){ 
    return acros; 
}*/ 
public ArrayList<Triple> getList(){return acros2;} 

/*public static Pair<String,String> returnAcro(String s){ 
    Pair<String,String> p = new Pair<String,String>(s,acros.get(s)); 
    //Triple<String,String,String> p = new Triple<String,String,String>(s, triple.second(),triple.third()); 
    return p; 
}*/ 
public static Triple<String,String,String> returnAcro2(String s){ 
    for(Triple triple : acros2){ 
     if(triple.contains(s)){ 
      return triple; 
     } 
    } 
    return null; 
}} 
+1

だけと思ったが、私はそのif文でリターンを置きます、あなたの有効な活動を開始するもの –

+0

何とかそれが問題を完全に解決しました。私はあなたに信用を与えることができるように答えとしてそれを置く必要があります! – CSRenA

答えて

0

両方の活動が開始されている可能性があります。私はあなたのコードを実行していません。しかし、InvalidAcronymアクティビティはValidAcronymの後に開始され、したがって可視であるようです。

 for(Triple triple: acros2){ 
      currAc = mEdit.getText().toString(); 
      if(triple.contains(currAc)){ 
       Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
       view.getContext().startActivity(intent1); 
      } 

    } 
     Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
     view.getContext().startActivity(intent2); 

アクティビティを開始すると、実行が停止しません。したがって、文字列が一致した後でもInvalidAcronymが開始されます。あなたはフラグを使用して、以下に示すように壊すことができます。

 boolean flag = true; 
     for(Triple triple: acros2){ 
      currAc = mEdit.getText().toString(); 
      if(triple.contains(currAc)){ 
       Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
       view.getContext().startActivity(intent1); 
       flag = false; 
       break; 
      } 
     } 
     Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
     if(flag){ 
      view.getContext().startActivity(intent2); 
     } 
+0

ありがとうございました!私はリターンを使用し、それは動作しますが、これは本当に私がなぜそれが起こっていたのか理解してくれました。 – CSRenA

+0

ありがとうございます。 BTWリターンでは、そのforループの後にさらにコードを実行することはできません。後でonCreate()で何かを追加する必要がある場合は、breakやflagなどを使用する必要があります。 –

0

のでonClickListener内部元のコードに:

/*It goes through this loop */ 
for(Triple triple: acros2){ 
    currAc = mEdit.getText().toString(); 
    /*Matches at the contains*/ 
    if(triple.contains(currAc)){ 
      Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
      view.getContext().startActivity(intent1); 
      /*Starts The activity*/ 
    } 
    /*Keeps Going*/ 
} 
/*Still going*/ 
Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
view.getContext().startActivity(intent2); 
/*Starts the InvalidAcronym activity, and shows it on top of everything will happen whether or not the acronym is valid*/ 

ので、それを修正する:

for(Triple triple: acros2){ 
    currAc = mEdit.getText().toString(); 
    if(triple.contains(currAc)){ 
     Intent intent1 = new Intent(view.getContext(), ValidAcronym.class); 
     view.getContext().startActivity(intent1); 
     /*end the onClick function early because it is valid and we don't need to keep going*/ 
     //This should show the correct activity now. 
     return; 
    } 

} 
//This won't be hit when it is valid 
Intent intent2 = new Intent(view.getContext(), InvalidAcronym.class); 
view.getContext().startActivity(intent2); 
関連する問題