Guest User

Untitled

a guest
Sep 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. public class Activity_Main extends AppCompatActivity implements NetworkMainAdapter.ContactsAdapterListener {
  2. private static final String TAG = MainActivity.class.getSimpleName();
  3. private RecyclerView recyclerView;
  4. private List<NetworkMAIN> contactList;
  5. private NetworkMainAdapter mAdapter;
  6. private SearchView searchView;
  7.  
  8. // url to fetch contacts json
  9. private static final String URL = "http://site.ru/json.php";
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. Toolbar toolbar = findViewById(R.id.toolbar);
  16. setSupportActionBar(toolbar);
  17.  
  18. // toolbar fancy stuff
  19. //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  20. getSupportActionBar().setTitle(R.string.toolbar_title);
  21.  
  22. recyclerView = findViewById(R.id.recycler_view);
  23. contactList = new ArrayList<>();
  24. mAdapter = new NetworkMainAdapter(this, contactList, this);
  25.  
  26. // white background notification bar
  27. whiteNotificationBar(recyclerView);
  28.  
  29. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
  30. recyclerView.setLayoutManager(mLayoutManager);
  31. recyclerView.setItemAnimator(new DefaultItemAnimator());
  32. recyclerView.addItemDecoration(new MyDividerItemDecoration(this, DividerItemDecoration.VERTICAL, 36));
  33. recyclerView.setAdapter(mAdapter);
  34.  
  35. fetchContacts();
  36. }
  37.  
  38. /**
  39. * fetches json by making http calls
  40. */
  41. private void fetchContacts() {
  42. JsonArrayRequest request = new JsonArrayRequest(URL,
  43. new Response.Listener<JSONArray>() {
  44. @Override
  45. public void onResponse(JSONArray response) {
  46. if (response == null) {
  47. Toast.makeText(getApplicationContext(), "Couldn't fetch the contacts! Pleas try again.", Toast.LENGTH_LONG).show();
  48. return;
  49. }
  50.  
  51. List<NetworkMAIN> items = new Gson().fromJson(response.toString(), new TypeToken<List<NetworkMAIN>>() {
  52. }.getType());
  53.  
  54. // adding contacts to contacts list
  55. contactList.clear();
  56. contactList.addAll(items);
  57.  
  58. // refreshing recycler view
  59. mAdapter.notifyDataSetChanged();
  60. }
  61. }, new Response.ErrorListener() {
  62. @Override
  63. public void onErrorResponse(VolleyError error) {
  64. // error in getting json
  65. Log.e(TAG, "Error: " + error.getMessage());
  66. Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
  67. }
  68. });
  69.  
  70. MyApplication.getInstance().addToRequestQueue(request);
  71. }
  72.  
  73. @Override
  74. public boolean onCreateOptionsMenu(Menu menu) {
  75. getMenuInflater().inflate(R.menu.search, menu);
  76.  
  77. // Associate searchable configuration with the SearchView
  78. SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  79. searchView = (SearchView) menu.findItem(R.id.action_search)
  80. .getActionView();
  81. searchView.setSearchableInfo(searchManager
  82. .getSearchableInfo(getComponentName()));
  83. searchView.setMaxWidth(Integer.MAX_VALUE);
  84.  
  85. // listening to search query text change
  86. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  87. @Override
  88. public boolean onQueryTextSubmit(String query) {
  89. // filter recycler view when query submitted
  90. mAdapter.getFilter().filter(query);
  91. return false;
  92. }
  93.  
  94. @Override
  95. public boolean onQueryTextChange(String query) {
  96. // filter recycler view when text is changed
  97. mAdapter.getFilter().filter(query);
  98. return false;
  99. }
  100. });
  101. return true;
  102. }
  103.  
  104. @Override
  105. public boolean onOptionsItemSelected(MenuItem item) {
  106. // Handle action bar item clicks here. The action bar will
  107. // automatically handle clicks on the Home/Up button, so long
  108. // as you specify a parent activity in AndroidManifest.xml.
  109. int id = item.getItemId();
  110.  
  111. //noinspection SimplifiableIfStatement
  112. if (id == R.id.action_search) {
  113. return true;
  114. }
  115.  
  116. return super.onOptionsItemSelected(item);
  117. }
  118.  
  119.  
  120.  
  121. private void whiteNotificationBar(View view) {
  122. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  123. int flags = view.getSystemUiVisibility();
  124. flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  125. view.setSystemUiVisibility(flags);
  126. getWindow().setStatusBarColor(Color.WHITE);
  127. }
  128. }
  129.  
  130. @Override
  131. public void onContactSelected(NetworkMAIN contact) {
  132. //Toast.makeText(getApplicationContext(), "Selected: " + contact.getLocation() + ", " + contact.getMain(), Toast.LENGTH_LONG).show();
  133. }
  134. }
  135.  
  136. <?php
  137. session_start();
  138. echo isset($_SESSION['login']);
  139. if(isset($_SESSION['login'])) {
  140. header('LOCATION:index.php'); die();
  141. }
  142. ?>
  143. <!DOCTYPE html>
  144. <html>
  145. <head>
  146. <meta http-equiv='content-type' content='text/html;charset=utf-8' />
  147. <title>Login</title>
  148. <meta charset="utf-8">
  149. <meta name="viewport" content="width=device-width, initial-scale=1">
  150. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  151. </head>
  152. <body>
  153. <div class="container">
  154. <h3 class="text-center">Login</h3>
  155. <?php
  156. if(isset($_POST['submit'])){
  157. $username = $_POST['username']; $password = $_POST['password'];
  158. if($username === 'admin' && $password === 'admin'){
  159. $_SESSION['login'] = true; header('LOCATION:data.php'); die();
  160. } {
  161. echo "<div class='alert alert-danger'>Username and Password do not match.</div>";
  162. }
  163.  
  164. }
  165. ?>
  166. <form action="" method="post">
  167. <div class="form-group">
  168. <label for="username">Username:</label>
  169. <input type="text" class="form-control" id="username" name="username" required>
  170. </div>
  171. <div class="form-group">
  172. <label for="pwd">Password:</label>
  173. <input type="password" class="form-control" id="pwd" name="password" required>
  174. </div>
  175. <button type="submit" name="submit" class="btn btn-default">Login</button>
  176. </form>
  177. </div>
  178. </body>
  179. </html>
Add Comment
Please, Sign In to add comment