我有2 TableView
s(tableProduct
,tableProduct2
)。第一个由数据库填充,第二个填充由第一个用户项(addMeal
方法)选择,该方法也将这些项转换为简单ArrayList
)。添加/删除少数对象后,用户可以将当前数据从第二个表保存到txt文件。它似乎在开始时工作得很好。但问题开始显示有点随机...我添加几个项目,保存,删除几个项目,保存,一切都很好。然后在这样的几个动作之后,即使TableView
为空,最后一个对象也保留在txt文件中。我无法做任何事情来删除它,我没有错误......
任何想法会发生什么?
public void addMeal() {
productData selection = tableProduct.getSelectionModel().getSelectedItem();
if (selection != null) {
tableProduct2.getItems().add(new productData(selection.getName() + "(" + Float.parseFloat(weightField.getText()) + "g)", String.valueOf(Float.parseFloat(selection.getKcal())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getProtein())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getCarb())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getFat())*(Float.parseFloat(weightField.getText())/100))));
productlist.add(new productSimpleData(selection.getName() + "(" + Float.parseFloat(weightField.getText()) + "g)", String.valueOf(Float.parseFloat(selection.getKcal())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getProtein())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getCarb())*(Float.parseFloat(weightField.getText())/100)), String.valueOf(Float.parseFloat(selection.getFat())*(Float.parseFloat(weightField.getText())/100))));
}
updateSummary();
}
public void deleteMeal() {
productData selection = tableProduct2.getSelectionModel().getSelectedItem();
if(selection != null){
tableProduct2.getItems().remove(selection);
Iterator<productSimpleData> iterator = productlist.iterator();
productSimpleData psd = iterator.next();
if(psd.getName().equals(String.valueOf(selection.getName()))) {
iterator.remove();
}
}
updateSummary();
}
public void save() throws IOException {
File file = new File("C:\\Users\\Maciek\\Desktop\\test1.txt");
if(file.exists()){
file.delete();
}
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
Iterator iterator;
iterator = productlist.iterator();
while (iterator.hasNext()) {
productSimpleData pd;
pd = (productSimpleData) iterator.next();
bw.write(pd.toString());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
bw.flush();
bw.close();
}
}
是的,我意识到addMethod
if
内部import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
/**
* Created by filipp on 5/23/2016.
*/
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message) {
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Elit")
.setContentText(message)
//.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
声明看起来很可怕,但不要介意,毕竟那部分是好的......
答案 0 :(得分:2)
您只检查productlist
列表中的第一项,以确定是否应删除该项目。由于您似乎没有在List
的{{1}}进行类似修改而在任何地方写items
,因此在这种情况下您也可以这样做。
tableProduct2
这样你也可以防止问题,如果列表中有2个相同的项目,这可能导致在选择第二个项目时删除第一个...
是的,我意识到
public void deleteMeal() { int selectedIndex = tableProduct2.getSelectionModel().getSelectedIndex(); if(selectedIndex >= 0) { tableProduct2.getItems().remove(selectedIndex); productlist.remove(selectedIndex); } updateSummary(); }
[...]看起来很可怕
是的,确实如此,所以是时候重写一下了:
将addMethod
和productData
中的属性更改为productSimpleData
,并且在您需要float
之前不要将数据转换为String
。
String
此外,这种循环可以重写为增强的for循环:
if (selection != null) {
float weight = Float.parseFloat(weightField.getText());
float weight100 = weight / 100;
float calories = Float.parseFloat(selection.getKcal())*weight100;
float protein = Float.parseFloat(selection.getProtein())*weight100;
float carb = Float.parseFloat(selection.getCarb())*weight100;
float fat = Float.parseFloat(selection.getFat())*weight100;
ProductData product = new productData(
selection.getName() + "(" + weight + "g)",
calories,
protein,
carb,
fat);
productlist.add(new productSimpleData(product.getName(), calories, protein, carb, fat));
tableProduct2.getItems().add(product);
}
假设您已将Iterator iterator;
iterator = productlist.iterator();
while (iterator.hasNext()) {
productSimpleData pd;
pd = (productSimpleData) iterator.next();
bw.write(pd.toString());
bw.newLine();
}
声明为productlist
或子类型,您可以这样做
List<productSimpleData>
此外,你可以依靠for (productSimpleData pd : productlist) {
bw.write(pd.toString());
bw.newLine();
}
为你关闭作家:
try-with-resources
此外,无需删除该文件,因为默认情况下java会覆盖该文件,只有在try (FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw)){
...
} catch (IOException e) {
e.printStackTrace();
}
的附加构造函数参数中指定此项时才附加数据。