每当我用代码加载模拟器时,它都会成功拉起并且不会崩溃。我可以单击按钮,但它们只是不会加载 Toast 消息。 Toast 应该在按下四个按钮之一后从我的列表文件中提取价格,但没有任何反应。不知道错误在哪里。
我尝试过使用检查id方法
(id == R.id.button3)
,但它与条件property != null
不匹配。我也尝试过其他方法,但没有效果。
注意:通知已在模拟器中启用。
主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
private Listing listing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listing = new Listing();
listing.loadProperties("listings.csv");
// connecting buttons with the
// layout using findViewById()
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
Button button5 = findViewById(R.id.button5);
Button button6 = findViewById(R.id.button6);
// apply setOnClickListener over buttons
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
}
// common onClick() for all buttons
@Override
public void onClick(View view) {
Button clickedButton = (Button) view;
String location = clickedButton.getText().toString();
Property property = listing.getProperty(location);
if (property != null) {
// Display property price in a Toast message
String priceText = "Price: $" + property.getPrice();
Toast.makeText(this, priceText, Toast.LENGTH_SHORT).show();
}
}
}
财产:
public abstract class Property {
private String ID;
private String location;
private double price;
public Property(String ID, String location, double price) {
this.ID = ID;
this.location = location;
this.price = price;
}
// Getters and Setters
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "ID: " + ID + "\nLocation: " + location + "\nPrice: " + price;
}
}
C属性:
public class CommercialProperty extends Property {
private String zone;
private int numUnits;
private int numParkingSpots;
public CommercialProperty(String ID, String location, double price, String zone, int numUnits, int numParkingSpots) {
super(ID, location, price);
this.zone = zone;
this.numUnits = numUnits;
this.numParkingSpots = numParkingSpots;
}
// Getters and Setters for CommercialProperty-specific properties
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public int getNumUnits() {
return numUnits;
}
public void setNumUnits(int numUnits) {
this.numUnits = numUnits;
}
public int getNumParkingSpots() {
return numParkingSpots;
}
public void setNumParkingSpots(int numParkingSpots) {
this.numParkingSpots = numParkingSpots;
}
@Override
public String toString() {
return super.toString() + "\nZone: " + zone + "\nUnits: " + numUnits + "\nParking Spots: " + numParkingSpots;
}
}
R属性:
public class ResidentialProperty extends Property {
private double annualHoaFees;
private int numBedrooms;
private double numBathrooms;
public ResidentialProperty(String ID, String location, double price, double annualHoaFees, int numBedrooms, double numBathrooms) {
super(ID, location, price);
this.annualHoaFees = annualHoaFees;
this.numBedrooms = numBedrooms;
this.numBathrooms = numBathrooms;
}
// Getters and Setters for ResidentialProperty-specific properties
public double getAnnualHoaFees() {
return annualHoaFees;
}
public void setAnnualHoaFees(double annualHoaFees) {
this.annualHoaFees = annualHoaFees;
}
public int getNumBedrooms() {
return numBedrooms;
}
public void setNumBedrooms(int numBedrooms) {
this.numBedrooms = numBedrooms;
}
public double getNumBathrooms() {
return numBathrooms;
}
public void setNumBathrooms(double numBathrooms) {
this.numBathrooms = numBathrooms;
}
@Override
public String toString() {
return super.toString() + "\nAnnual HOA Fees: " + annualHoaFees + "\nBedrooms: " + numBedrooms + "\nBathrooms: " + numBathrooms;
}
}
列表:
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Listing {
private ArrayList<Property> properties;
public Listing() {
properties = new ArrayList<>();
}
public void loadProperties(String fileName) {
try (BufferedReader br = new BufferedReader(new FileReader("listings.csv"))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(" ");
if (data.length >= 6) {
String ID = data[0];
String location = data[1] + " " + data[2] + " " + data[3];
double price = Double.parseDouble(data[4]);
if (ID.startsWith("rp")) {
double annualHoaFees = Double.parseDouble(data[5]);
int numBedrooms = Integer.parseInt(data[6]);
double numBathrooms = Double.parseDouble(data[7]);
ResidentialProperty rp = new ResidentialProperty(ID, location, price, annualHoaFees, numBedrooms, numBathrooms);
properties.add(rp);
} else if (ID.startsWith("cp")) {
String zone = data[5];
int numUnits = Integer.parseInt(data[6]);
int numParkingSpots = Integer.parseInt(data[7]);
CommercialProperty cp = new CommercialProperty(ID, location, price, zone, numUnits, numParkingSpots);
properties.add(cp);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Property getProperty(String address) {
for (Property prop : properties) {
if (prop.getLocation().equals(address)) {
return prop;
}
}
return null;
}
}
可能是你的
if (property != null)
条件不满足。
在某些情况下可能会出现这种情况 可能是您从单击
location
获得的 Button
字符串在您的 "listings.csv"
文件中找不到。