我正在创建一个应用程序,在我的文本文件中搜索给定的搜索词或首字母缩略词。这是我真正创建的第一个应用程序。文本文件的格式如下:acronym%term%definition。我有一个InputStream和一个BufferedReader逐行遍历文件。然后我将其分解为百分号,并使用我创建的Triple类将字符串放入三元组中,并将Triple添加到ArrayList中。我的下一个函数应该通过Triple的ArrayList搜索包含搜索首字母缩写词或术语的那个。如果找不到首字母缩略词,则转到InvalidAcronym活动。如果它存在,则进入ValidAcronym活动。出于某种原因,触发的唯一活动是InvalidAcronym活动。我认为这与将行添加到ArrayList有关。我最初使用HashMaps只使用了首字母缩略词和定义。有人可以帮我吗?这是我的代码:
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;
}}
答案 0 :(得分:0)
两个活动都可能已经开始。我没有运行你的代码。但似乎InvalidAcronym Activity在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);
}
答案 1 :(得分: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);