【问题标题】:How to add hint in android spinner?如何在 android spinner 中添加提示?
【发布时间】:2018-02-21 17:58:23
【问题描述】:

我知道有很多与我的问题相关的问题,我查看了这些解决方案,但无法从他们那里获得帮助。所以这就是我发布这个问题的原因。

我的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 正在显示

我不知道该怎么办。任何帮助将不胜感激

【问题讨论】:

标签: java android json android-spinner


【解决方案1】:

我认为微调器没有提示

但我有一个小解决方案

你的代码是

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

在将用户名添加到arraylist之前,把代码放在下面

ie.add 你的提示到你的数组

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

我觉得对你有帮助

【讨论】:

  • 好的,我会试着告诉你
  • 它并没有帮助我,它只显示了空的微调器
  • 对不起,我的xampp 服务器没有运行,请查看更新
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    使用 if 条件

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

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 2011-09-19
      • 2012-03-14
      • 2012-12-02
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      相关资源
      最近更新 更多