Spin ner OnItemSelected在Android Studio中不起作用[关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spin ner OnItemSelected在Android Studio中不起作用[关闭]相关的知识,希望对你有一定的参考价值。
我可以查看微调器项目,但onItemSelected
方法无效。其他文本数据不显示,该怎么办?
我有微调器,它显示用户的名称,我试图按微调器项目显示其他三个文本框数据,但Textview
上没有数据显示。
我不知道是什么。
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
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 android.widget.AdapterView.OnItemSelectedListener;
import android.view.View.OnClickListener;
public class Search extends AppCompatActivity implements AdapterView.OnItemSelectedListener
private Spinner spinner;
private ArrayList<String> students;
private JSONArray result;
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
students = new ArrayList<String>();
spinner = (Spinner) findViewById(R.id.myspinner);
// spinner.setOnItemSelectedListener(this);
textViewName = (TextView) findViewById(R.id.txtName);
textViewCourse = (TextView) findViewById(R.id.txtBussy);
textViewSession = (TextView) findViewById(R.id.txtAdd);
getData();
private void getData()
StringRequest stringRequest = new
StringRequest("https://maheshwaghela.com/RukhiVivah/RetrivesData/spinner1.php",
new Response.Listener<String>()
@Override
public void onResponse(String response)
JSONObject j = null;
try
j = new JSONObject(response);
result = j.getJSONArray(Config.JSON_ARRAY);
getStudents(result);
catch (JSONException e)
e.printStackTrace();
,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
private void getStudents(JSONArray j)
for(int i=0;i<j.length();i++)
try
JSONObject json = j.getJSONObject(i);
students.add(json.getString(Config.TAG_USERNAME));
catch (JSONException e)
e.printStackTrace();
spinner.setAdapter(new ArrayAdapter<String>(Search.this, android.R.layout.simple_spinner_dropdown_item, students));
//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_NAME);
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 bussy="";
try
JSONObject json = result.getJSONObject(position);
bussy = json.getString(Config.TAG_BUSSY);
catch (JSONException e)
e.printStackTrace();
return bussy;
//Doing the same with this method as we did with getName()
private String getSession(int position)
String address="";
try
JSONObject json = result.getJSONObject(position);
address = json.getString(Config.TAG_ADDRESS);
catch (JSONException e)
e.printStackTrace();
return address;
//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
textViewName.setText(getName(position));
textViewCourse.setText(getCourse(position));
textViewSession.setText(getSession(position));
//When no item is selected this method would execute
// @Override
public void onNothingSelected(AdapterView<?> parent)
textViewName.setText("");
textViewCourse.setText("");
textViewSession.setText("");
为什么onItemSelected
不起作用。请提供解决方案。
答案
尝试在之后添加setOnItemSelectedListener
spinner.setAdapter(new ArrayAdapter<String>(Search.this, android.R.layout.simple_spinner_dropdown_item, students));
//event listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
//Setting the values to textviews for a selected item
textViewName.setText(getName(position));
textViewCourse.setText(getCourse(position));
textViewSession.setText(getSession(position));
@Override
public void onNothingSelected(AdapterView<?> parent)
textViewName.setText("");
textViewCourse.setText("");
textViewSession.setText("");
);
以上是关于Spin ner OnItemSelected在Android Studio中不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Android Spinner onItemSelectedListener 有两个相同的参数
android Spinner:在 onItemSelected 视图中为空
如何防止 onItemSelected 在新实例化的 Spinner 上触发?
如果选择了非零位置,则在旋转后调用 Spinner 的 onItemSelected 回调两次
Android ListView 在 saveButton 之后没有更新,而是在 onItemSelected 之后更新