2017-02-14 38 views
0

我知道有很多問題與我的問題有關,我查看了這些解決方案,但無法從中獲得幫助。所以這就是我發佈這個問題的原因。如何在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(""); 

} 
} 

當我運行下面的結果顯示在應用

enter image description here

我要的是每當我跑appspinner中的第一個text將類似Select a name

在我的佈局文件,我沒有嘗試過android:hint="Select a name"但沒有奏效,因爲它顯示我沒有在微調

enter image description here

更新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以下結果顯示

enter image description here

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)); 
} 

貝婁是其中我看到

enter image description here

它不應該顯示在第一次運行的文本結果也在dropdown

它顯示了我的提示,但結果仍顯示我的名字和ID。當我選擇第二個名稱第三個名稱和ID顯示

我不知道該怎麼做。任何幫助將高度讚賞

+0

也許這可以幫助HTTP ://stackoverflow.com/questions/20451499/android-spinne r-set-default-text –

+0

http://stackoverflow.com/questions/37810610/how-to-set-hint-text-for-spinner –

+0

讓我知道那個人是否工作。 –

回答

0

我認爲微調沒有一絲

但我有小的解決方案

你的代碼是

users.add(json.getString(Config.TAG_NAME));//this will add name to user array list 

之前添加用戶名到ArrayList中,輸入以下代碼

ie.add your hint您陣列

users.add("your hint"); 
eg:users.add("select name"); 

我認爲這將幫助你

+0

好吧,我會盡力讓你知道 – faisal1208

+0

它並沒有幫助我,所有它讓我看到空飛旋 – faisal1208

+0

對不起我的壞,我的'xampp'服務器未運行,請儘快更新 – faisal1208

0

使用,如果條件

if(!getName(posission).equals("select a name")) 
{ 
    textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1)); 
}   
+0

在什麼時候我會使用它? – faisal1208

+0

把代碼放在onItemSelected()函數 –

0

在onItemSelected()函數

public void onItemSelected(AdapterView<?> parent, View view, int position,long id) { 
//Appending the values to textview for a selected item 
// textViewResult.setText(""); 
    if(!getName(posission).equals("select a name")) 
    { 
    textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1)); 
    } }                             
+0

好吧讓我試試我會告訴你 – faisal1208

+0

檢查我更新的問題 – faisal1208