Created
March 18, 2020 20:23
-
-
Save efturtle/ad29aec1309052eb757e624a3f1a7967 to your computer and use it in GitHub Desktop.
Aplicacion 1 del segundo parcial, Listado de sensores en un ListView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <TextView | |
| android:id="@+id/textView" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="@string/listado_de_sensores" | |
| android:textSize="20sp" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintHorizontal_bias="0.069" | |
| app:layout_constraintLeft_toLeftOf="parent" | |
| app:layout_constraintRight_toRightOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" | |
| app:layout_constraintVertical_bias="0.022" /> | |
| <ListView | |
| android:id="@+id/listado" | |
| android:layout_width="368dp" | |
| android:layout_height="440dp" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintHorizontal_bias="0.078" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> | |
| </androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.listadesensores; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import android.content.Context; | |
| import android.hardware.Sensor; | |
| import android.hardware.SensorManager; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.AdapterView; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.ListView; | |
| import android.widget.Toast; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class MainActivity extends AppCompatActivity { | |
| private List<Sensor> listadoSensor; | |
| private SensorManager sensorManager; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| //retrieve the list from layout | |
| ListView listado = (ListView) findViewById(R.id.listado); | |
| //instance of sensor service | |
| sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
| //puts the sensorList in our List (listadoSensor) | |
| listadoSensor = sensorManager.getSensorList(Sensor.TYPE_ALL); | |
| List<String> nombresDeSensores = new ArrayList<>(); | |
| //this will add all the elements to the listView | |
| for (Sensor sensor : listadoSensor) { | |
| nombresDeSensores.add(sensor.getName()); | |
| } | |
| ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, nombresDeSensores); | |
| //our list has now an adapter, this adapter is for the long hold press on a list item. | |
| listado.setAdapter(adapter); | |
| //on long hold it will display name, type and version of item on list pressed. | |
| listado.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
| @Override | |
| public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | |
| int sensorID = (int) id; | |
| Sensor sensor = listadoSensor.get(sensorID); | |
| if (sensor != null) { | |
| Toast.makeText(MainActivity.this, sensor.getName() + " " + sensor.getType() + " " + sensor.getVersion(), Toast.LENGTH_SHORT).show(); | |
| } | |
| return false; | |
| } | |
| }); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manny es puto xd