使用Volley更新ListView JsonParsing

时间:2014-10-15 14:59:11

标签: android json listview android-volley

我在logcat窗口中获取json响应,该窗口不会更新listview。当我在addcontent()方法中设置断点时,第一次它没有得到json,但是它第三次(我在调试透视窗口中进行了几次)。以下是我的适配器

public class DaibeticRegAdapter extends BaseAdapter implements ListAdapter{
private Context mContext;
//DiagRegPojo pojo = new DiagRegPojo();
DiabeticRegJson drJson = new DiabeticRegJson(); 
public DaibeticRegAdapter(Context c){
    super();
    this.mContext = c;        
    drJson.addContent();       
    }
@Override
public int getCount() {     
    return drJson.getName_list().size();
}

@Override
public Object getItem(int position) {
    return drJson.getName_list().get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View view = convertView;
       if(convertView==null)
       {
           view = new View(mContext);
           view = inflater.inflate(R.layout.diagnostic_reg_list, null);
           TextView textView = (TextView)view.findViewById(R.id.diabetic_reg_item);      
           System.out.println("ArtraYlist Size fom Adapter"+drJson.name_list.size());
           System.out.println("Array LIst Options"+drJson.getName_list().get(position));
           textView.setText(drJson.getName_list().get(position));

       }else{
           view =(View)convertView;
       }    
       return view;
       }    
}

这是一个使用volley

来解析Json的Java类
public class DiabeticRegJson {
String tag_json_arry = "json_array_req";
String TAG  = "TESTING";
String diag_url= Urllist.digRegJson;
private static final String DIAG_REG_NAME = "content";    
public static final String savedCookie = "savedCookie";

public ArrayList<String> name_list=new ArrayList<String>();


public  ArrayList<String> getName_list() {
    return name_list;
}
public  void setName_list(ArrayList<String> name_list) {
    this.name_list = name_list;
}    

public void addContent() {        
    JsonArrayRequest req = new JsonArrayRequest(diag_url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    try {
                        //name_list.clear();
                        // Parsing json array response
                        // loop through each json object                        
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject person = (JSONObject) response.get(i);
                            String diagName = person.getString(DIAG_REG_NAME);                    
                            name_list.add(diagName);                               
                            //pojo.name_list.add(diagName);
                            Log.i("VOLLEY GETTING URLS", diagName); 
                            Log.i("VOLLEY GETTING URLS FROM ARRAYLIST", name_list.get(i));  
                            }                           
                        System.out.println("Size of List " + name_list.size());
                    } catch (JSONException e) {
                        e.printStackTrace();                            
                    }                   
                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());   
                    Log.i("ERROR JSON PARSING", error.getMessage());
                }
            })
            {            
       //**
        // Passing some request headers
         //*
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));                
            headers.put("Set-Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));
            headers.put("Content-Type", "application/x-www-form-urlencoded");                
            //headers.put("Content-Type","application/json");
            headers.put("Accept", "application/x-www-form-urlencoded");                
            return headers;
        } 
    };   
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req, tag_json_arry);
    }
    }   

Plz让我知道缺少什么。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您在检索新数据时没有通知您的适配器,因此它不会更新UI。要使用现有代码修复此问题,您可以将BaseAdapter的引用传递给DiabeticRegJson,并在设置响应后从监听器中调用BaseAdapter.notifyDataSetChanged()

另一种选择是使用ArrayAdapter,让它管理您的String列表 - 只需将字符串添加到适配器,而不是让您的请求对象封装响应。