第二次冲刺(二)

今天我完成了用户预定的房间信息查看。

关键代码为:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myreserve);
        Bundle bundle=getIntent().getExtras();
        Nowid=bundle.getString("nowid");
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                Connection conn=null;
                String sql=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver"); //加载驱动
                    String ip = "8.142.16.93";
                    conn =(Connection) DriverManager.getConnection(
                            "jdbc:mysql://" + ip + ":3306/" + "user",
                            "user", "123456");
                }catch (SQLException | ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                try {
                    sql="select roomid from roominfo where roomuserid='"+Nowid+"'";
                    Statement  stmt = conn.createStatement();
                    //使用Connection来创建一个Statment对象
                    ResultSet rs =stmt.executeQuery(sql);//用rs接收sql语句返回的查询结果
                    //执行查询语句并且保存结果
                    while (rs.next()){
                        int k=rs.getInt("roomid");
                        a[k-1]=1;
                    }
                    rs.close();//查询关闭
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0,j=0;i<10;i++){
            if(a[i]>0){
                b[j]=i;
                j++;
            }
        }
        List<Map<String,Object>> ListItems=new ArrayList<Map<String, Object>>();
        for (int i=0;i<names.length;i++){
            Map<String,Object> listItem=new HashMap<String,Object>();
            if(a[i]==1){
                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.myreservelist);
        //为ListView设置Adapter
        listView.setAdapter(adapter);
        //设置点击监听
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent= new Intent(MyreserveActivity.this, Myreserve2Activity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("roomid",position);
                bundle.putIntArray("b",b);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

 

posted @ 2021-06-02 20:43  张笑天  阅读(31)  评论(0编辑  收藏  举报