我知道有很多問題與我的問題有關,我查看了這些解決方案,但無法從中獲得幫助。所以這就是我發佈這個問題的原因。如何在android spinner中添加提示?
我的app
正在從web-service
獲得json
數據。下面是我json
結果
{"users":[{"Id":"1","Name":"Faisal"},{"Id":"2","Name":"Salman"},{"Id":"3","Name":"Asim"},{"Id":"4","Name":"Asad"},{"Id":"5","Name":"Mateen"},{"Id":"6","Name":"Omar"},{"Id":"7","Name":"Usama"}]}
從我在一個Android微調僅Names
顯示上述數據。下面是我Config.java
代碼
public class Config {
//JSON URL
public static final String DATA_URL = "http://10.0.2.2:8000/MobileApp/index.php";
//Tags used in the JSON String
public static final String TAG_NAME = "Name";
public static final String TAG_ID = "Id";
//JSON array name
public static final String JSON_ARRAY = "users";
}
對於MainActivity.java
見下文
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
//Declaring an Spinner
private Spinner spinner;
//An ArrayList for Spinner Items
private ArrayList<String> users;
//JSON Array
private JSONArray result;
//TextViews to display details
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing the ArrayList
users = new ArrayList<String>();
//Initializing Spinner
spinner = (Spinner)findViewById(R.id.spinner);
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself
// we are passing this to setOnItemSelectedListener
spinner.setOnItemSelectedListener(this);
//Initializing TextView
textViewResult = (TextView)findViewById(R.id.textView);
//This method will fetch the data from the URL
getData();
}
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
getUsers(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 getUsers(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);
//Adding the name of the student to array list
users.add(json.getString(Config.TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item, users));
}
//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();
}
return name;
}
//Method to get student Id of a particular position
private String getId (int postion)
{
String Id="";
try{
JSONObject json = result.getJSONObject(postion);
Id = json.getString(Config.TAG_ID);
} catch (JSONException e) {
e.printStackTrace();
}
return Id;
}
//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Appending the values to textview for a selected item
// textViewResult.setText("");
textViewResult.setText("Hi " + getName(position) + " your ID is " + getId(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
textViewResult.setText("");
}
}
當我運行下面的結果顯示在應用
我要的是每當我跑app
的spinner
中的第一個text
將類似Select a name
在我的佈局文件,我沒有嘗試過android:hint="Select a name"
但沒有奏效,因爲它顯示我沒有在微調
更新1 作爲建議的Selin Marvan K
我已經做了以下
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
users.add("Select a name");
users.add(json.getString(Config.TAG_NAME));
當我運行app
以下結果顯示
論Selin Marvan K
的建議,我有以下
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Appending the values to textview for a selected item
// textViewResult.setText("");
if(!getName(position).equals("select a name")) {
textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1));
}
//textViewResult.setText("Hi " + getName(position) + " your ID is " + getId(position));
}
貝婁是其中我看到
它不應該顯示在第一次運行的文本結果也在dropdown
。
它顯示了我的提示,但結果仍顯示我的名字和ID。當我選擇第二個名稱第三個名稱和ID顯示
我不知道該怎麼做。任何幫助將高度讚賞
也許這可以幫助HTTP ://stackoverflow.com/questions/20451499/android-spinne r-set-default-text –
http://stackoverflow.com/questions/37810610/how-to-set-hint-text-for-spinner –
讓我知道那個人是否工作。 –