本界面主要功能:
为菜品添加评论并进行评分
Activity:AddCommentPage
package com.example.fanpeng.smartcanteen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;
import org.litepal.LitePal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AddCommentPage extends AppCompatActivity {
private Button btn1,btn2;
private EditText addText;
private RatingBar gradeStar;
private User user=new User();
private Dishes dishes=new Dishes();
private static String TAG="MyCheck";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_comment_page);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
dishes=(Dishes)bundle.getSerializable("dishes1");
Log.d(TAG,dishes.getName()+"2");//---------------
user=(User)bundle.getSerializable("user1");
/
addText=findViewById(R.id.add_text);
gradeStar=findViewById(R.id.grade_star);
btn1=findViewById(R.id.add_btn);
btn2=findViewById(R.id.cancel_btn);
//
btn1.setOnClickListener(new addCommentListener(user,dishes));
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addText.setText("");
finish();
}
});
}
class addCommentListener implements View.OnClickListener{
User tempUser;
Dishes tempDishes;
List<Comment> comments;
public addCommentListener(User user,Dishes dishes){
this.tempUser=user;
this.tempDishes=dishes;
comments= LitePal.select("dishes_id=?",String.valueOf(dishes.getId())).find(Comment.class);
}
@Override
public void onClick(View v) {
double grade=Double.parseDouble(String.valueOf(gradeStar.getRating()));
String str=addText.getText().toString();
Log.d(TAG,tempDishes.getName()+"3");//-------------------
if(str.isEmpty()){
Toast.makeText(AddCommentPage.this,"未进行评论",Toast.LENGTH_SHORT).show();
}
else {
User user1=LitePal.find(User.class,tempUser.getId(),true);
Dishes dish1=LitePal.find(Dishes.class,tempDishes.getId(),true);
Log.d(TAG,str+String.valueOf(grade));
Comment comment=new Comment();
comment.setText(str);
comment.setCommentator(user1);
comment.setDish(dish1);
comment.setGrade(grade);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Log.d(TAG,sdf.format(new Date()));
Log.d(TAG,user1.getName());
Log.d(TAG,dish1.getName());
comment.setSetDate(sdf.format(new Date()));
comment.save();
user1.getComments().add(comment);
user1.save();
comment.save();
Log.d(TAG,String.valueOf(comment.getId()));
dish1.getComments().add(comment);
dish1.setCommentCount(dish1.getComments().size());
dish1.save();
Toast.makeText(AddCommentPage.this,"已添加",Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
layout:activity_add_comment_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fcfbf4"
tools:context=".AddCommentPage">
<TextView
android:text="请评分"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RatingBar
android:id="@+id/grade_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="5"
android:stepSize="0.5"
android:numStars="5"/>
<TextView
android:text="请评论"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/add_text"
android:layout_width="match_parent"
android:layout_height="300sp"
android:maxLines="6"
android:maxLength="140"
android:hint="请在此添加评论,最大140字"
android:gravity="top"
android:cursorVisible="true"/>
<Button
android:id="@+id/add_btn"
android:text="确 认 添 加"
android:layout_height="35dp"
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:background="#beb391"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="#e3e6c3"
android:textSize="18sp"
/>
<Button
android:id="@+id/cancel_btn"
android:text="取 消 添 加"
android:layout_height="35dp"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:background="#beb391"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="#e3e6c3"
android:textSize="18sp"
/>
</LinearLayout>