我在Mysql
数据库中有一个带有一些插件的表,我将所有插件从数据库中添加到了微调器。我想根据微调器的选择检索数据。我面临一个问题,即有两个记录在plug1
的数据库中,当我选择plug1
正确检索时,当我在微调器中选择plug2
时检索plug1
的另一条记录。我使用了选定项目的位置,这就是为什么它采用位置明智的顺序。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".DashboardActivity"
android:orientation="vertical"
android:background=" #dfe3e7"
tools:showIn="@layout/app_bar_dashboard">
<Spinner
android:id="@+id/spinnerDevices"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/spinner"
android:layout_marginTop="20dp"
android:overlapAnchor="false"></Spinner>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:text="ON/OFF"
android:textSize="20sp"
android:paddingLeft="5dp"/>
<Switch
android:id="@+id/on"
android:layout_width="100dp"
android:layout_height="45dp"
android:checkable="true"
android:padding="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="15dp"
>
<android.support.v7.widget.CardView
android:layout_width="130dp"
android:layout_height="180dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background=" #1c2833"
android:padding="8dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/image1"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/device1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView1"
android:layout_marginTop="10dp"
android:layout_marginLeft="1dp"
android:fontFamily="serif"
android:text="TEMPERATURE"
android:textColor="#fff"
android:textSize="15sp" />
<TextView
android:id="@+id/meter1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/device1"
android:fontFamily="serif"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:textColor="#ff6861"
android:textSize="30sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background=" #1c2833"
android:padding="7dp">
<ImageView
android:id="@+id/imageView2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/image2"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/device2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView2"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:fontFamily="serif"
android:textColor="#fff"
android:text="CURRENT"
android:textSize="15sp" />
<TextView
android:id="@+id/meter2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/device2"
android:fontFamily="serif"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:textColor="#ff6861"
android:textSize="30sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background=" #1c2833"
android:padding="7dp">
<ImageView
android:id="@+id/imageView3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/image3"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/device3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView3"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:fontFamily="serif"
android:text="VOLTAGE"
android:textColor="#fff"
android:textSize="15sp" />
<TextView
android:id="@+id/meter3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/device3"
android:fontFamily="serif"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:textSize="30sp"
android:textColor="#ff6861"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>>
</LinearLayout
JAVA
package com.example.sp;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DashboardActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener , NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "DashboardActivity";
private String URLstring = "http://192.168.0.108/Smart/getdata.php";
public static final String JSON_ARRAY = "data";
public static final String EmployeeNamearray = "plugno";
private TextView textView;
String line=null;
private ArrayList<String> names = new ArrayList<String>();
private JSONArray data;
private JSONArray result;
private ArrayList<String> students;
private final int jsoncode = 1;
private Spinner spinner;
String status="";
private TextView textViewName, textViewCourse, textViewSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView) findViewById(R.id.greeting_text_view);
spinner = (Spinner) findViewById(R.id.spinnerDevices);
names = new ArrayList<String>();
students = new ArrayList<String>();
getdata();
getData();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
textViewName = (TextView) findViewById(R.id.meter1);
textViewCourse = (TextView) findViewById(R.id.meter2);
textViewSession = (TextView) findViewById(R.id.meter3);
spinner.setOnItemSelectedListener(this);
Switch s = (Switch) findViewById(R.id.on);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(DashboardActivity.this, "switch is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(DashboardActivity.this, "switch is off", Toast.LENGTH_SHORT).show();
}
}
});
}
private void getData() {
//Creating a string request
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getStudents(JSONArray j) {
//Traversing through all the items in the json array
for (int i = 0; i < j.length(); i++) {
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
if(json.getString("plugno").equals("plugno")){
students.add(json.getString(Config.TAG_PLUGNO));
}
//Adding the name of the student to array list
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(DashboardActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
spinner.setAdapter(new ArrayAdapter<String>(DashboardActivity.this, android.R.layout.simple_spinner_dropdown_item, names));
}
//Method to get student name of a particular position
private String getName(int position) {
String name = "";
try {
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching name from that object
name = json.getString(Config.TAG_TEMPERATURE);
} catch (JSONException e) {
e.printStackTrace();
}
//Returning the name
return name;
}
//Doing the same with this method as we did with getName()
private String getCourse(int position) {
String course = "";
try {
JSONObject json = result.getJSONObject(position);
course = json.getString(Config.TAG_CURRENT);
} catch (JSONException e) {
e.printStackTrace();
}
return course;
}
//Doing the same with this method as we did with getName()
private String getSession(int position) {
String session = "";
try {
JSONObject json = result.getJSONObject(position);
session = json.getString(Config.TAG_VOLTAGE);
} catch (JSONException e) {
e.printStackTrace();
}
return session;
}
//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
}
//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
textViewName.setText("");
textViewCourse.setText("");
textViewSession.setText("");
}
private void getdata() {
StringRequest stringRequest = new StringRequest("http://192.168.0.108/Smart/getdata.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
data = j.getJSONArray(JSON_ARRAY);
empdetails(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void empdetails(JSONArray j) {
for (int i = 0; i < j.length(); i++) {
try {
JSONObject json = j.getJSONObject(i);
names.add(json.getString(EmployeeNamearray));
} catch (JSONException e) {
e.printStackTrace();
}
}
// arrayList.add(0,"Select Employee");
spinner.setAdapter(new ArrayAdapter<String>(DashboardActivity.this, android.R.layout.simple_spinner_dropdown_item,names));
spinner.setAdapter(new ArrayAdapter<String>(DashboardActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
}
//Method to get student name of a particular position+
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
Intent i = new Intent(getApplicationContext(), AddPlugActivity.class);
startActivity(i);
// Handle the camera action
} else if (id == R.id.nav_manage) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:2)
在此方法中,像这样添加
//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
yourlist.get(position);
}
让我知道它是否对您有用。