第一阶段冲刺六
完成任务:系统管理员使用界面
一、样图
二、代码
package com.example.hotel.Activity.Manager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.example.hotel.Database.MysqlUser;
import com.example.hotel.R;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ManagerActivity extends AppCompatActivity {
private String[] names = new String[]{
"房间1,优惠大床房 128 大床1.8米 22平米 无窗",
"房间2,商务大床房 138 大床1.8米 22-24平米 无窗",
"房间3,主题房 218 圆床 25平米 无窗",
"房间4,商务套房 388 大床1.8米 45平米 有窗",
"房间5,智控大床房 218 大床1.8米 30平米 有窗",
"房间6,标准双床房 108 双床 1.2米 20平米 有窗",
"房间7,商旅大床房 188 大床1.8米25平米 有窗",
"房间8,温馨家庭房 188 多张床 30-35平米 有窗",
"房间9,主题房 218 圆床 25平米 无窗",
"房间10,商务大床房 145 大床1.8米 23-25平米 有窗",
};
private int[] img = new int[]{R.drawable.room1,R.drawable.room2,R.drawable.room3,R.drawable.room4,R.drawable.room5,
R.drawable.room6,R.drawable.room7,R.drawable.room8,R.drawable.room9,R.drawable.room10};
private ListView list;
private SimpleAdapter simpleAdapter;
private int state,ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager);
List<Map<String,Object>> ListItems=new ArrayList<Map<String, Object>>();
{
//利用循环将键和值一一对应放入map集合内。
for (int i=0;i<names.length;i++){
Map<String,Object> listItem=new HashMap<String,Object>();
listItem.put("images",img[i]);
listItem.put("head",names[i]);
//加入list集合
ListItems.add(listItem);
}
}
SimpleAdapter adapter=new SimpleAdapter(this,ListItems,R.layout.list_2, new String[]{"head","images"}, new int[]{R.id.Head,R.id.Img});
ListView listView=(ListView)findViewById(R.id.managerlist);
//为ListView设置Adapter
listView.setAdapter(adapter);
//设置点击监听
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
ID=position+1;
MysqlUser mysqlUser=new MysqlUser();
try {
String sql="select roomstate from roominfo where roomid='"+ID+"'";
state=Integer.parseInt(mysqlUser.select(sql));
} catch (SQLException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (state==1)
Toast.makeText(ManagerActivity.this, "房间"+ID+"已被预定",Toast.LENGTH_SHORT).show();
else Toast.makeText(ManagerActivity.this, "房间"+ID+"未被预定",Toast.LENGTH_SHORT).show();
}
});
}
}