如何在android spinner中添加提示?

时间:2017-02-14 13:24:53

标签: java android json android-spinner

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

我的app正在从json获取web-service个数据。以下是我的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

我想要的是每当我app运行text spinner Select a nameandroid:hint="Select a name"

在我的布局文件中,我确实尝试了Selin Marvan K,但它没有用,因为它在微调器中没有显示任何内容

enter image description here

更新1 正如 //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时,会显示以下结果

enter image description here

根据 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

贝娄是我看到的结果

enter image description here

它不应该在clear; clc; v=input('Parameter :');%input for example 'x' and remember the quotes syms(v);%symbolic variable : x in this case y=input('function :');%example exp (x) ,not exp(y) or ay other variable f=matlabFunction(y);%converts y to a command type function f df = matlabFunction(diff(y)); %calculates the differentiation. 中首次运行时显示文字。

它确实向我显示了我的提示,但结果它仍然显示我的名字和ID。当我选择第二个名称时,第三个名称和ID正在显示

我不知道该怎么办。任何帮助都将受到高度赞赏

3 个答案:

答案 0 :(得分:0)

我认为微调器没有提示

但我的解决方案很小

您的代码是

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

我认为它会对你有所帮助

答案 1 :(得分:0)

使用条件

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

答案 2 :(得分:0)

在onItemSelected()函数

size_t N = 20;
size_t M = 20;
std::vector<int> normal;
normal.resize(N * M);

for (size_t i = 0; i < N; ++i)
    for (size_t j = 0; j < M; ++j)
        normal[i + j * N] = j;