Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android spinner中添加提示?_Java_Android_Json_Android Spinner - Fatal编程技术网

Java 如何在android spinner中添加提示?

Java 如何在android spinner中添加提示?,java,android,json,android-spinner,Java,Android,Json,Android Spinner,我知道有很多问题与我的问题有关,我看过这些解决方案,但无法从中得到帮助。这就是为什么我要发布这个问题 我的app正在从web服务获取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"}

我知道有很多问题与我的问题有关,我看过这些解决方案,但无法从中得到帮助。这就是为什么我要发布这个问题

我的
app
正在从
web服务
获取
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 spinner中仅显示
名称
。下面是我的
Config.java
code

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("");

 }
}
当我运行
应用程序时,将显示以下结果

根据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));
 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));
}
public void已选择(AdapterView父视图、视图视图、int位置、长id){
//将选定项的值附加到textview
//textViewResult.setText(“”);
如果(!getName(position).equals(“选择名称”)){
textViewResult.setText(“Hi”+getName(position-1)+“您的ID是”+getId(position-1));
}
//textViewResult.setText(“Hi”+getName(position)+“您的ID是”+getId(position));
}
下面是我看到的结果

它不应该在第一次运行时也在
下拉列表中显示文本

它确实显示了我的提示,但结果仍然显示了我的名字和id。当我选择第二个名字时,第三个名字和id将显示出来


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

我想斯宾纳没有任何暗示

但我有一个小解决方案

if(!getName(posission).equals("select a name"))
{
    textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1));
}        
 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));
  }  }                                                                                                                 
你的代码是

users.add(json.getString(Config.TAG_NAME));//this will add name to user array list
在将用户名添加到arraylist之前,请在下面输入代码

将提示添加到数组中

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

我想它会帮助你使用if条件

if(!getName(posission).equals("select a name"))
{
    textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1));
}        
 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));
  }  }                                                                                                                 
在MSelected()函数中

if(!getName(posission).equals("select a name"))
{
    textViewResult.setText("Hi " + getName(position-1) + " your ID is " + getId(position-1));
}        
 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));
  }  }                                                                                                                 
public void已选择(AdapterView父视图、视图视图、int位置、长id){
//将选定项的值附加到textview
//textViewResult.setText(“”);
如果(!getName(posission).equals(“选择名称”))
{
textViewResult.setText(“Hi”+getName(position-1)+“您的ID是”+getId(position-1));
}  }                                                                                                                 

也许这有助于让我知道它是否有效。@SoumyaRauth当然我会马上告诉你的while@SoumyaRauth我无法理解解决方案:(好的,我会尝试让你知道它没有帮助我,它显示了空喷丝头对不起,我的坏,我的
xampp
服务器没有运行,请查看更新