本界面主要功能:
显示菜品的详细信息及对该菜品的评论
Acitivity:DetailsPage
package com.example.fanpeng.smartcanteen;
import android.content.Intent;
import android.graphics.BitmapFactory;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.litepal.LitePal;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class DetailsPage extends AppCompatActivity {
private TextView dishName,dishPrice,dishGrade;
private Button btn,btn_f5;
private ImageView dishImage;
private List<Comment> commentList;
private String TAG="MyCheck";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_page);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
Dishes dishes=(Dishes)bundle.getSerializable("dishes");
User user=(User)bundle.getSerializable("user");
Log.d(TAG,dishes.getName());
dishName=findViewById(R.id.show_dish_name);
dishPrice=findViewById(R.id.show_dish_price);
dishGrade=findViewById(R.id.show_dish_grade);
dishImage=findViewById(R.id.show_dish_image);
btn=findViewById(R.id.add_comment);
btn_f5=findViewById(R.id.f5_comment);
//dishImage
//dishImage.set//图片暂时未设置
dishName.setText(dishes.getName());
dishPrice.setText(String.valueOf(dishes.getPrice())+"元");
double AverGrade= LitePal.where("dishes_id = ?",String.valueOf(dishes.getId())).average(Comment.class,"grade");
BigDecimal b=new BigDecimal(AverGrade);
AverGrade=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
dishGrade.setText("评分:"+String.valueOf(AverGrade));
dishImage.setImageBitmap(BitmapFactory.decodeFile(dishes.getPicturePath()));
//评论列表///
commentList= LitePal.where("dishes_id=?",String.valueOf(dishes.getId())).find(Comment.class);//初始化
CommentAdapter adapter=new CommentAdapter(DetailsPage.this,R.layout.comment_menu,commentList);
ListView listView=findViewById(R.id.comment_list);
listView.setAdapter(adapter);
//添加评论/
btn.setOnClickListener(new addListener(user,dishes));
btn_f5.setOnClickListener(new f5Listener(dishes));
}
public class f5Listener implements View.OnClickListener{
Dishes dishes;
public f5Listener(Dishes dishes){
this.dishes=dishes;
}
@Override
public void onClick(View v) {
commentList= LitePal.where("dishes_id=?",String.valueOf(dishes.getId())).find(Comment.class);//初始化
Log.d(TAG,String.valueOf(dishes.getId()));
Log.d(TAG,String.valueOf(dishes.getCommentCount()));
CommentAdapter adapter=new CommentAdapter(DetailsPage.this,R.layout.comment_menu,commentList);
ListView listView=findViewById(R.id.comment_list);
listView.setAdapter(adapter);
Toast.makeText(DetailsPage.this,"已刷新",Toast.LENGTH_SHORT).show();
}
}
public class addListener implements View.OnClickListener{
User user;
Dishes dishes;
addListener(User user, Dishes dishes){
this.user=user;
this.dishes=dishes;
}
@Override
public void onClick(View v) {
//触发添加评论事件,记得将评论save,相关的user和dishes的save
Intent intent=new Intent(DetailsPage.this,AddCommentPage.class);
Bundle bundle=new Bundle();
bundle.putSerializable("dishes1",dishes);
bundle.putSerializable("user1",user);
Log.d(TAG,dishes.getName()+"1");
intent.putExtras(bundle);
startActivity(intent);
Toast.makeText(DetailsPage.this,"请为此菜品添加评论",Toast.LENGTH_SHORT).show();
}
}
}
layout:activity_details_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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=".DetailsPage">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#fcfbf4"
android:layout_weight="0"
android:layout_marginTop="10dp"
>
<ImageView
android:id="@+id/show_dish_image"
android:layout_width="200dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/show_dish_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/show_dish_image"
android:layout_alignTop="@id/show_dish_image"
android:text="菜名"
android:textSize="25sp"/>
<TextView
android:id="@+id/show_dish_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/show_dish_image"
android:layout_below="@id/show_dish_name"
android:layout_marginTop="20dp"
android:text="售价"
android:textSize="25sp"/>
<TextView
android:id="@+id/show_dish_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/show_dish_image"
android:layout_below="@id/show_dish_price"
android:layout_marginTop="20dp"
android:text="评分"
android:textSize="25sp"/>
</RelativeLayout>
<ListView
android:id="@+id/comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/add_comment"
android:text="添 加 评 论"
android:layout_height="35dp"
android:layout_weight="0"
android:layout_width="match_parent"
android:background="#beb391"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="#e3e6c3"
android:textSize="18sp" />
<Button
android:id="@+id/f5_comment"
android:text="刷 新 评 论"
android:layout_height="35dp"
android:layout_weight="0"
android:layout_width="match_parent"
android:background="#beb391"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="#e3e6c3"
android:layout_marginTop="5dp"
android:textSize="18sp" />
</LinearLayout>
注意:
点击添加评论按钮后跳转到评论界面,评论完毕返回本界面后还需要点击刷新评论按钮新添加的评论才会出现。