Guest User

Untitled

a guest
Nov 17th, 2019
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.palmitessa;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorManager;
  7. import android.os.Bundle;
  8.  
  9. import androidx.annotation.NonNull;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import androidx.appcompat.widget.Toolbar;
  13.  
  14. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  15. import com.google.android.material.snackbar.Snackbar;
  16.  
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.TextView;
  21.  
  22. import com.example.palmitessa.dummy.DummyContent;
  23.  
  24. import java.util.List;
  25.  
  26. /**
  27. * An activity representing a list of Sensors. This activity
  28. * has different presentations for handset and tablet-size devices. On
  29. * handsets, the activity presents a list of items, which when touched,
  30. * lead to a {@link SensorDetailActivity} representing
  31. * item details. On tablets, the activity presents the list of items and
  32. * item details side-by-side using two vertical panes.
  33. */
  34. public class SensorListActivity extends AppCompatActivity {
  35.  
  36. /**
  37. * Whether or not the activity is in two-pane mode, i.e. running on a tablet
  38. * device.
  39. */
  40. private boolean mTwoPane;
  41. private SensorManager sensorM;
  42. private static List<Sensor> sensorL;
  43.  
  44. public List<Sensor> getSensor(){
  45. return sensorL;
  46. }
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_sensor_list);
  52.  
  53. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  54. setSupportActionBar(toolbar);
  55. toolbar.setTitle(getTitle());
  56.  
  57. sensorM = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  58. sensorL = sensorM.getSensorList(Sensor.TYPE_ALL);
  59.  
  60. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  61. fab.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View view) {
  64. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  65. .setAction("Action", null).show();
  66. }
  67. });
  68.  
  69. if (findViewById(R.id.sensor_detail_container) != null) {
  70. // The detail container view will be present only in the
  71. // large-screen layouts (res/values-w900dp).
  72. // If this view is present, then the
  73. // activity should be in two-pane mode.
  74. mTwoPane = true;
  75. }
  76.  
  77. View recyclerView = findViewById(R.id.sensor_list);
  78. assert recyclerView != null;
  79. setupRecyclerView((RecyclerView) recyclerView);
  80. }
  81.  
  82. private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
  83. recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(this, DummyContent.ITEMS, mTwoPane));
  84. }
  85.  
  86. public static class SimpleItemRecyclerViewAdapter
  87. extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
  88.  
  89. private final SensorListActivity mParentActivity;
  90. private final List<DummyContent.DummyItem> mValues;
  91. private final boolean mTwoPane;
  92. private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
  93. @Override
  94. public void onClick(View view) {
  95. DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
  96. if (mTwoPane) {
  97. Bundle arguments = new Bundle();
  98. arguments.putString(SensorDetailFragment.ARG_ITEM_ID, item.id);
  99. SensorDetailFragment fragment = new SensorDetailFragment();
  100. fragment.setArguments(arguments);
  101. mParentActivity.getSupportFragmentManager().beginTransaction()
  102. .replace(R.id.sensor_detail_container, fragment)
  103. .commit();
  104. } else {
  105. Context context = view.getContext();
  106. Intent intent = new Intent(context, SensorDetailActivity.class);
  107. intent.putExtra(SensorDetailFragment.ARG_ITEM_ID, item.id);
  108.  
  109. context.startActivity(intent);
  110. }
  111. }
  112. };
  113.  
  114. SimpleItemRecyclerViewAdapter(SensorListActivity parent,
  115. List<DummyContent.DummyItem> items,
  116. boolean twoPane) {
  117. mValues = items;
  118. mParentActivity = parent;
  119. mTwoPane = twoPane;
  120. }
  121.  
  122. @Override
  123. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  124. View view = LayoutInflater.from(parent.getContext())
  125. .inflate(R.layout.sensor_list_content, parent, false);
  126. return new ViewHolder(view);
  127. }
  128.  
  129. @Override
  130. public void onBindViewHolder(final ViewHolder holder, int position) {
  131. holder.mIdView.setText(mValues.get(position).id);
  132. holder.mContentView.setText(mValues.get(position).content);
  133.  
  134. holder.itemView.setTag(mValues.get(position));
  135. holder.itemView.setOnClickListener(mOnClickListener);
  136. }
  137.  
  138. @Override
  139. public int getItemCount() {
  140. return mValues.size();
  141. }
  142.  
  143. class ViewHolder extends RecyclerView.ViewHolder {
  144. final TextView mIdView;
  145. final TextView mContentView;
  146.  
  147. ViewHolder(View view) {
  148. super(view);
  149. mIdView = (TextView) view.findViewById(R.id.id_text);
  150. mContentView = (TextView) view.findViewById(R.id.content);
  151. }
  152. }
  153. }
  154. }
RAW Paste Data