gpt4 book ai didi

java - 旋转器事件后 Json Listview 不会刷新

转载 作者:行者123 更新时间:2023-11-29 18:39:24 25 4
gpt4 key购买 nike

我的问题是我的 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);
}

我应该使用相同的数组来更新数据吗?

最佳答案

你可以试试

  1. 创建两个对象

          ArrayList<MobileKeyModel> dropDownModels = new ArrayList<MobileKeyModel>();
    ArrayList<Object> addList = new ArrayList<Object>();
  2. 您可以从数据库获取响应

       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);
    }
  3. 将值填充到 ListView

    无效 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);

    }
    }
  4. 模型也在这里

    公共(public)类 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;
    }

    }

  5. 注意您的 json 必须类似于

        {"parentnodesList":[{"key":"your key value here","titulo":"Your title"}]}

关于java - 旋转器事件后 Json Listview 不会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45041531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com