我创建了一个单例类,它是一个Boat列表。我正在为MainActivity的Action Bar创建一个选项,它将根据用户输入船的注册情况显示有关船的信息。如果它在列表中找到注册,它应该在TextView上附加有关该特定船只注册的所有信息。但是,我不断收到我的错误消息。我认为原因在于我的for循环语句或它的正文,但我不太确定。这是我的代码看起来像(我还将提供列表,BoatList又名我的单例类和我的主代码下面的常规类Boat):
public void showBoat(View view)
{
//Declare references and variables
EditText et;
TextView tv;
ImageView im;
BoatList boat_list;
String boatRegist;
//Set references
et = (EditText) findViewById(R.id.edit_boat_regist);
tv = (TextView) findViewById(R.id.text_main);
im = (ImageView) findViewById(R.id.image_area);
boat_list = BoatList.getInstance();
//Get user input
boatRegist = et.getText().toString();
//Search through the list and try to find the boat by registration
int i;
for(i = 0; i < boat_list.size(); i++)
{
if(boatRegist == boat_list.get(i).getRegi())
{
tv.append(boat_list.get(i).getMake() + "\n" + boat_list.get(i).getRegi() +
"\n" + boat_list.get(i).getYear() + "\n" + boat_list.get(i).getLength()
+ "\n" + boat_list.get(i).getBeam() + "\n" + boat_list.get(i).getFuel() +
"\n" + boat_list.get(i).getPrice() + "\n" + boat_list.get(i).getAge()
+ "\n");
}
else
Toast.makeText(ShowBoatActivity.this, "Error: The registration is invalid!",
Toast.LENGTH_SHORT).show();
}
//Open the picture from the URL address
/*
ImageDownloader idl = new ImageDownloader();
idl.download(boat_list.get(i).getPicURL(), im);*/
}
以下是我的Singleton类:
import java.util.LinkedList;
public final class BoatList extends LinkedList<Boat>
{
private static BoatList instance = null;
private BoatList()
{
// Exists only to defeat additional instantiations.
}
//Define method
public static BoatList getInstance()
{
if(instance == null)
instance = new BoatList();
return instance;
}
} //end BoatList
最后,这是我的常规班,船:
public class Boat {
//Declare data members
private String make;
private String registeration;
private int year;
private int length;
private int beam;
private String fuel;
private double price;
private String picURL;
private int age;
private double priceLT;
//Define the methods
public String getMake()
{
return make;
}
public void setMake(String m)
{
make = m;
}
public String getRegi()
{
return registeration;
}
public void setRegi(String r)
{
registeration = r;
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year = y;
}
public int getLength()
{
return length;
}
public void setLength(int L)
{
length = L;
}
public int getBeam()
{
return beam;
}
public void setBeam(int b)
{
beam = b;
}
public String getFuel()
{
return fuel;
}
public void setFuel(String f)
{
fuel = f;
}
public double getPrice()
{
return price;
}
public void setPrice(double p)
{
price = p;
}
public String getPicURL()
{
return picURL;
}
public void setPicURL(String url)
{
picURL = url;
}
public int getAge()
{
int currentYear = 2015;
//Calculate the age of the boat
age = currentYear - year;
return age;
}
public double getLuxuryTax()
{
//Declare variables
double newPrice;
double luxTax = .15;
//Calculate the price with luxury tax if the first condition
//is found to be True.
if (length >= 50 && age <= 10)
{
newPrice = price * luxTax;
priceLT = price + newPrice;
if (priceLT == 0)
{
priceLT = (Double) null;
return priceLT;
}
}
return priceLT;
}
}
答案 0 :(得分:1)
您正在使用==
来比较注册值,您应该使用.equalsIgnoreCase(str)
reference来确保用户的输入在他/她使用小写字母而非资本时进行积极比较。