我正在尝试一个简单的应用程序,其中我使用从网络调用获得的值(json值)填充微调器。但是,当从微调器中选择一个项目时,我无法获得如何获取关联的json值。任何人都可以提供一篇关于如何做到的文章/参考吗?
这是我的json回复
{
item: {
name: "xyz",
brands: [
{
id: 123,
name: "abc"
}
]
}
}
这就是我解析json的方式。
public class user_activity extends ActionBarActivity {
RequestQueue queue;
ArrayList<String> brand_list = new ArrayList<String>();
Spinner spinner;
String brand_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_activity);
this.spinner= (Spinner) findViewById(R.id.spinner);
loadUser();
}
public void loadUser() {
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please Wait..");
pDialog.show();
queue = Volley.newRequestQueue(this);
String url = "http://myApiUrl";
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
pDialog.hide();
try {
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
String b = array.getJSONObject(i).getString("name");
brand_list.add(b);
}
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(user_activity.this,android.R.layout.simple_dropdown_item_1line,brand_list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(),spinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
} catch (JSONException e) {
e.printStackTrace();
pDialog.hide();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", String.valueOf(error));
}
}
);
queue.add(postRequest);
}
我想发送相关的id值i,e&#34; 123&#34;通过按钮意图进行下一个活动。
编辑: 修改后的代码如下
但是函数集微调器总是返回empty
public class user_activity extends ActionBarActivity {
String phone_number;
String access_token;
Button enterButton;
String brand_id;
TextView uName;
RequestQueue queue;
ArrayList<String> brand_list = new ArrayList<String>();
ArrayList<String> _ids = new ArrayList<String>();
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_activity);
this.phone_number = getIntent().getStringExtra("phone");
this.access_token = getIntent().getStringExtra("token");
enterButton = (Button)findViewById(R.id.buttonEnter);
uName = (TextView) findViewById(R.id.tvName);
this.spinner= (Spinner) findViewById(R.id.spinner);
loadUser(phone_number, access_token);
// nextActivity(b[0]);
}
public void loadUser(String phone, String token) {
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please Wait..");
pDialog.show();
queue = Volley.newRequestQueue(this);
final String[] message = new String[1];
final String[] my_id = new String[1];
String url = "http://api_url";
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
pDialog.hide();
try {
message[0] = response.getJSONObject("item").getString("name");
uName.setText(message[0]);
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
String b = array.getJSONObject(i).getString("name");
String id = array.getJSONObject(i).getString("id");
_ids.add(id);
brand_list.add(b);
}
String id = setSpinner();
} catch (JSONException e) {
e.printStackTrace();
pDialog.hide();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", String.valueOf(error));
}
}
);
queue.add(postRequest);
}
public String setSpinner(){
ArrayAdapter<String> adapter;
final String[] id = new String[1];
adapter = new ArrayAdapter<String>(user_activity.this,android.R.layout.simple_dropdown_item_1line,brand_list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id[0] = _ids.get(i);
// Toast.makeText(getBaseContext(), id[0] + ":" + spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Toast.makeText(getBaseContext(), id[0].toString() , Toast.LENGTH_SHORT).show();
return id[0];
}
答案 0 :(得分:3)
有很多方法。
一种方法是创建 function callfunc($name,$params=false){
//Call pre
//Call real function
call_user_func($name,$params);
//and the last, call post
}
id
arrayList
将 ArrayList<String> _ids = new ArrayList<String>();
的值添加到此JSON
arryList
然后在 String id = array.getJSONObject(i).getString("id");
_ids.add(id);
spinner.setOnItemSelectedListener(....)
答案 1 :(得分:3)
我认为您需要先将数据存储在数组列表中,如下所示。
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
JSONObject mItemObject = array.getJSONObject(i);
String b = mItemObject.getString("name");
brand_list.add(b);
JSONArray mBrandsArray = mItemObject.getJSONArray("brands");
JSONObject mBrandsObject = mBrandsArray.getJSONObject(0);
String brandsId = mBrandsObject.getString("id");
String brandsName = mBrandsObject.getString("name");
brand_id.add(brandId); //make this array list likebrand_list
brand_name.add(brandName); //make this array list like ..
}
然后在你的微调器中
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(),spinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
Intent mIntent = new Intent(this, NewActivity.class);
// load data using putExtra
mIntent.putExtra("id", brand_id.get(i));
...
...
startActivity(mIntent);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
答案 2 :(得分:0)
创建一个名为Brand的类,如下所示
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
现在更改解析代码并使Brand对象代替获取名称并将其添加到 class Brand {
int id;
String name;
@Override
public String toString() {
return name;
}
}
。一旦用户选择了任何内容,您就可以获得所选索引,找到品牌对象并将id从对象发送到另一个活动。
答案 3 :(得分:0)
您需要创建自己的OnItemSelectedListener
接口实现,并通过spinner.setOnItemSelectedListener(listener)
将其放置在微调器上。您的listener
对象应该是
listener = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
JSONObject selectedObject = globalItemsObject.optJSONObject(position);
//make some actions with "selectedObject"
}
public void onNothingSelected(AdapterView<?> parent) {
}
};