我的问题是我在listview上有一个产品列表,当我从我的微调器中选择一个值时我保存并发送它以从我的mysql数据库中检索不同的值,问题是我的listview不会在我的微调器上执行onclick事件后更新,我做错了什么?我不知道我是否设法正确解释了我想说的话,谢谢你的帮助
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected (AdapterView < ? > parent, View view,int pos, long id)
{
filtracat();
}
private void filtracat() {
cat = spinner.getSelectedItemPosition()+1;
valor = String.valueOf(cat);
Toast.makeText(getApplicationContext(),valor,Toast.LENGTH_SHORT).show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(JSON_CAT, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideDialog();
//parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Anuncios item = new Anuncios();
item.setKey(obj.getString("key"));
item.setTitulo(obj.getString("titulo"));
item.setImageID(obj.getString("imageID"));
item.setModelo(obj.getString("modelo"));
item.setPreco(obj.getString("preco"));
//gonna set category here
// item.setCategoria(obj.getString("categoria"));
//add to array
array2.add(item);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("valor", valor);
return params;
}
};
AppController.getmInstance().addToRequesQueue(jsonArrayRequest);
}
我的listview中的原始数据来自
private void sendRequest() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideDialog();
//parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Anuncios item = new Anuncios();
item.setKey(obj.getString("key"));
item.setTitulo(obj.getString("titulo"));
item.setImageID(obj.getString("imageID"));
item.setModelo(obj.getString("modelo"));
item.setPreco(obj.getString("preco"));
//gonna set category here
// item.setCategoria(obj.getString("categoria"));
//add to array
array.add(item);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getmInstance().addToRequesQueue(jsonArrayRequest);
}
我应该使用相同的数组来更新数据吗?
答案 0 :(得分:0)
你可以尝试
创建两个对象
ArrayList<MobileKeyModel> dropDownModels = new ArrayList<MobileKeyModel>();
ArrayList<Object> addList = new ArrayList<Object>();
您可以从数据库获取响应
private void filtracat() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray loginNodes = jsonObject.getJSONArray("parentnodesList");
for (int i = 0; i < loginNodes.length(); i++) {
JSONObject jo = loginNodes.getJSONObject(i);
String key = jo.getString("key");
String titulo = jo.getString("titulo");
addList.add(key);
addList.add(titulo);
}
pupulateDropDownList();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
//Respose Error
} catch (Exception e) {
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
将值填充到listview
void pupulateDropDownList(){
if (addList != null) {
for (Iterator<Object> i = addList.iterator(); i.hasNext(); ) {
MobileKeyModel menuModel = new MobileKeyModel(i.next().toString(), i.next().toString());
dropDownModels.add(menuModel);
}
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_text, dropDownModels);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown);
listView.setAdapter(adapter);
}
}
模特也在这里
公共类MobileKeyModel {
public String key = "";
public String titulo = "";
public MobileKeyModel(String key, String titulo) {
this.key = key;
this.titulo = titulo;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
}
注意您的json必须看起来像
{"parentnodesList":[{"key":"your key value here","titulo":"Your title"}]}