Advertisement
Guest User

my rooms

a guest
Apr 8th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.50 KB | None | 0 0
  1. MainActivity.java
  2. =======================
  3. package com.example.teacher.mysmarthome;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  15.     EditText txtUser,txtPass;
  16.     Button btnRegister;
  17.     Context context;
  18.     private final String TEST_USER="zeev";
  19.     private final String TEST_PASS="12345";
  20.     SPusers myUsers;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         setPointer();
  27.         myUsers = new SPusers(this);
  28.  
  29.     }
  30.  
  31.     private void setPointer() {
  32.         context=this;
  33.         txtUser=findViewById(R.id.txtUser);
  34.         txtPass=findViewById(R.id.txtPass);
  35.         btnRegister=findViewById(R.id.btnRegister);
  36.         btnRegister.setOnClickListener(new View.OnClickListener() {
  37.             @Override
  38.             public void onClick(View view) {
  39.                 //create a new intent
  40.                 Intent intent=new Intent(context,Registration.class);
  41.                 startActivity(intent);
  42.  
  43.                 //bad practice
  44.                 //startActivity(new Intent(context,Registration.class));
  45.             }
  46.         });
  47.     }
  48.  
  49.     public void btnLogin(View view)
  50.     {
  51.         //Toast.makeText(this, "Hello my brother from another mother", Toast.LENGTH_LONG).show();
  52.         String myUser=txtUser.getText().toString();
  53.         String myPass=txtPass.getText().toString();
  54.  
  55.         //check if user exists...
  56.  
  57.  
  58.         //LOGIN TO ROOMS SCREEN
  59.         Intent intent = new Intent(context,Rooms.class);
  60.         //not a good way...
  61.         //intent.putExtra("userName","Zeevik");
  62.  
  63.         //this is how we work, with bundle
  64.         Bundle bundle = new Bundle();
  65.         bundle.putString("userName","MBD");
  66.         bundle.putInt("userLevel",0);
  67.         bundle.putBoolean("canEdit",true);
  68.         bundle.putBoolean("canDelete",true);
  69.         intent.putExtras(bundle);
  70.  
  71.         //start the activity...
  72.         startActivity(intent);
  73.     }
  74.  
  75.     @Override
  76.     public void onClick(View view) {
  77.         switch (view.getId())
  78.         {
  79.             case R.id.btnAr:
  80.                 Toast.makeText(context, "Arabic", Toast.LENGTH_SHORT).show();
  81.                 break;
  82.  
  83.             case R.id.btnEn:
  84.                 Toast.makeText(context, "English", Toast.LENGTH_SHORT).show();
  85.                 break;
  86.  
  87.             case R.id.btnHe:
  88.                 Toast.makeText(context, "Hebrew", Toast.LENGTH_SHORT).show();
  89.                 break;
  90.  
  91.             case R.id.btnRu:
  92.                 Toast.makeText(context, "Russion", Toast.LENGTH_SHORT).show();
  93.                 break;
  94.  
  95.             default:
  96.                 Toast.makeText(context, "WTF?!?!?!?", Toast.LENGTH_SHORT).show();
  97.                 break;
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. RoomsAdapter.java
  104. ======================
  105. package com.example.teacher.mysmarthome;
  106.  
  107. import android.content.Context;
  108. import android.graphics.Color;
  109. import android.view.View;
  110. import android.view.ViewGroup;
  111. import android.widget.BaseAdapter;
  112. import android.widget.ImageView;
  113. import android.widget.TextView;
  114.  
  115. import java.util.List;
  116.  
  117. public class RoomsAdapter extends BaseAdapter {
  118.  
  119.     Context context;
  120.     List<ClsRooms> myRooms;
  121.     boolean flipFlop=true;
  122.  
  123.     public RoomsAdapter(Context context, List<ClsRooms> myRooms) {
  124.         this.context = context;
  125.         this.myRooms = myRooms;
  126.     }
  127.  
  128.     @Override
  129.     //how many times we will build the item.....
  130.     public int getCount() {
  131.         return myRooms.size();
  132.     }
  133.  
  134.     @Override
  135.     //get item data (Object)
  136.     public Object getItem(int i) {
  137.         return null;
  138.     }
  139.  
  140.     @Override
  141.     //get item id by it's id (good for outside item select)
  142.     public long getItemId(int i) {
  143.         return 0;
  144.     }
  145.  
  146.     @Override
  147.     //create view for each row (according to getCount)
  148.     public View getView(int i, View view, ViewGroup viewGroup) {
  149.  
  150.         //single view item
  151.         //TextView roomName=new TextView(context);
  152.         //roomName.setTextSize(32);
  153.         //roomName.setText(myRooms.get(i).roomName);
  154.  
  155.         //we hold the view (layout of room_item)
  156.         View myItemView = View.inflate(context,R.layout.room_item,null);
  157.         //we set pointers to each item
  158.         ImageView roomPic = myItemView.findViewById(R.id.picRoom);
  159.         TextView roomName = myItemView.findViewById(R.id.txtRoomName);
  160.         TextView roomApp = myItemView.findViewById(R.id.txtAppl);
  161.         flipFlop=!flipFlop;
  162.         if (flipFlop)
  163.         {
  164.             myItemView.setBackgroundColor(Color.GRAY);
  165.         }
  166.         //we change the info inside our view
  167.         roomName.setText(myRooms.get(i).roomName);
  168.         roomApp.setText(myRooms.get(i).items+"");
  169.  
  170.         return myItemView;
  171.     }
  172. }
  173.  
  174.  
  175. ClsRooms.java
  176. ==================
  177. package com.example.teacher.mysmarthome;
  178.  
  179. import android.graphics.Bitmap;
  180.  
  181. public class ClsRooms {
  182.     String roomName;
  183.     int items;
  184.     int roomPicture=0;
  185.  
  186.     public ClsRooms(String roomName, int items, int roomPicture) {
  187.         this.roomName = roomName;
  188.         this.items = items;
  189.         this.roomPicture = roomPicture;
  190.     }
  191. }
  192.  
  193.  
  194. Rooms.java
  195. =============
  196. package com.example.teacher.mysmarthome;
  197.  
  198. import android.content.Context;
  199. import android.content.Intent;
  200. import android.support.v7.app.AppCompatActivity;
  201. import android.os.Bundle;
  202. import android.widget.Button;
  203. import android.widget.ListView;
  204. import android.widget.TextView;
  205.  
  206. import java.util.ArrayList;
  207. import java.util.List;
  208.  
  209. public class Rooms extends AppCompatActivity {
  210.     TextView txtUserName;
  211.     Button btnAdd;
  212.     ListView lstRooms;
  213.     Context context;
  214.  
  215.     @Override
  216.     protected void onCreate(Bundle savedInstanceState) {
  217.         super.onCreate(savedInstanceState);
  218.         setContentView(R.layout.activity_rooms);
  219.         setPointer();
  220.         getData();
  221.         setRooms();
  222.     }
  223.  
  224.     private void setRooms() {
  225.         //create a demo list
  226.         List<ClsRooms> myRooms = new ArrayList<>();
  227.         myRooms.add(new ClsRooms("Sallon",5,0));
  228.         myRooms.add(new ClsRooms("BadRoom",3,0));
  229.         myRooms.add(new ClsRooms("Office",3,0));
  230.         myRooms.add(new ClsRooms("Nipo Room",3,0));
  231.         myRooms.add(new ClsRooms("Kitchen",7,0));
  232.         myRooms.add(new ClsRooms("Living Room",6,0));
  233.         myRooms.add(new ClsRooms("BathRoom",1,0));
  234.  
  235.         //create a new BaseAdapter for displaying the info inside the listView
  236.         RoomsAdapter myAdapter=new RoomsAdapter(context,myRooms);
  237.         //connect between the listView and our adapter
  238.         lstRooms.setAdapter(myAdapter);
  239.  
  240.     }
  241.  
  242.     private void getData() {
  243.         Intent intent=getIntent();
  244.         //old way
  245.         //String userName="Hello "+intent.getStringExtra("userName");
  246.  
  247.  
  248.         //best way
  249.         Bundle bundle = intent.getExtras();
  250.         String userName = "Hello "+bundle.getString("userName");
  251.  
  252.         txtUserName.setText(userName);
  253.     }
  254.  
  255.     private void setPointer() {
  256.         context=this;
  257.         txtUserName=findViewById(R.id.txtUserName);
  258.         lstRooms=findViewById(R.id.lstRooms);
  259.     }
  260. }
  261.  
  262.  
  263.  
  264. room_item.xml
  265. =================
  266. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  267.     android:layout_width="match_parent"
  268.     android:layout_height="100dp"
  269.     android:orientation="horizontal"
  270.     android:layout_marginTop="20dp"
  271.     android:gravity="center_vertical"
  272.     android:background="#fff">
  273.  
  274.     <ImageView
  275.         android:layout_width="50dp"
  276.         android:layout_height="100dp"
  277.         android:src="@drawable/logo_mysh"
  278.         android:layout_weight="1"
  279.         android:id="@+id/picRoom"/>
  280.     <TextView
  281.         android:layout_width="wrap_content"
  282.         android:layout_height="wrap_content"
  283.         android:textSize="28sp"
  284.         android:text="roomName"
  285.         android:layout_weight="2"
  286.         android:gravity="center"
  287.         android:id="@+id/txtRoomName"/>
  288.     <TextView
  289.         android:layout_width="wrap_content"
  290.         android:layout_height="wrap_content"
  291.         android:textColor="#00ff00"
  292.         android:text="0"
  293.         android:textSize="38sp"
  294.         android:layout_weight="1"
  295.         android:gravity="center"
  296.         android:id="@+id/txtAppl"/>
  297. </LinearLayout>
  298.  
  299.  
  300. activity_rooms.xml
  301. ===================
  302. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  303.     android:layout_width="match_parent"
  304.     android:layout_height="match_parent"
  305.     android:orientation="vertical" >
  306.  
  307.     <TextView
  308.         android:layout_width="match_parent"
  309.         android:layout_height="wrap_content"
  310.         android:text="Here we have the user name"
  311.         android:gravity="center"
  312.         android:layout_marginTop="20dp"
  313.         android:textSize="22sp"
  314.         android:id="@+id/txtUserName"/>
  315.     <Button
  316.         android:layout_width="match_parent"
  317.         android:layout_height="wrap_content"
  318.         android:background="#009fff"
  319.         android:textColor="#fff"
  320.         android:text="Add Room"
  321.         android:textSize="22sp"
  322.         android:layout_marginTop="20dp"
  323.         android:id="@+id/btnAdd"/>
  324.  
  325.     <ListView
  326.         android:layout_width="match_parent"
  327.         android:layout_height="match_parent"
  328.         android:id="@+id/lstRooms"></ListView>
  329. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement