csv 我如何修复吐司不显示,由于空?

1cosmwyk  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(101)

每当我加载我的模拟器与代码成功拉起来,并没有崩溃。我可以点击按钮,但他们只是不会加载吐司消息。吐司应该拉价格从我的上市文件后,按下四个按钮之一,但什么也没有发生。不知道错误在哪里。
我试过使用检查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;
    }
}

iqjalb3h

iqjalb3h1#

可能是你

if (property != null)

字符串
条件未满足。
在某些情况下,可能是您从单击Button获得的location字符串在"listings.csv"文件中找不到。

相关问题