【发布时间】: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 正在显示
我不知道该怎么办。任何帮助将不胜感激
【问题讨论】:
-
让我知道那个是否有效。
-
@SoumyaRauth 等会儿我会告诉你的
-
@SoumyaRauth 我无法理解解决方案:(
标签: java android json android-spinner