回收视图没有用json填充

时间:2016-05-18 15:30:59

标签: android json android-recyclerview android-volley

我的recyclerview没有填充json这里是我的片段代码,其中包含recyclerview,我从php通过volley库获取json数据请帮帮我。我已发布片段代码,适配器代码以及我的应用程序的日志。

public String get_events="urlfordata";
    public RequestQueue requestQueue;
    public StringRequest stringRequest;
    public String college_name;
    public RecyclerView recyclerView;
    public LinearLayoutManager llm;
    public List<events> events = new ArrayList<events>();
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.events_home,container,false);
            recyclerView = (RecyclerView) v.findViewById(R.id.neostat_home_recycleview);


            SharedPreferences sha1 = getActivity().getSharedPreferences("user_data", Context.MODE_PRIVATE);
            college_name = sha1.getString("college_name",null);
            llm = new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(llm);

            requestQueue = Volley.newRequestQueue(getContext());
            stringRequest = new StringRequest(Request.Method.POST, get_events, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        for (int i=0;i<jsonArray.length();i++)
                        {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            events = new ArrayList<>();
                            events.add(new events(jsonObject.getString("title"),jsonObject.getString("image")));
                            System.out.println(jsonObject.getString("title"));
                        }



                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            })
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> hashMap = new HashMap<String,String>();
                    hashMap.put("college_name","");
                    return hashMap;
                }
            };

            requestQueue.add(stringRequest);


            events_adapter adapter = new events_adapter(events);
            recyclerView.setAdapter(adapter);


            return v;
        }
    }

我的适配器: -

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by cheta on 5/18/2016.
 */
public class events_adapter extends RecyclerView.Adapter<events_adapter.EventHolder> {


    List<events>  events;
    events_adapter(List<events> events)
    {
        this.events =events;

    }
    @Override
    public EventHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_cardview,parent,false);
        EventHolder pvh = new EventHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(EventHolder holder, int position) {

        holder.textView.setText(events.get(position).title);
        Context context = holder.imageView.getContext();
        Picasso.with(context).load(events.get(position).image).fit().into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        return events.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class EventHolder extends RecyclerView.ViewHolder
    {
        ImageView imageView;
        TextView textView;
        CardView cardView;

        EventHolder(View v)
        {
            super(v);
            cardView = (CardView) v.findViewById(R.id.events_home_cardview);
            imageView = (ImageView) v.findViewById(R.id.events_home_image);
            textView = (TextView) v.findViewById(R.id.events_home_title);

        }

    }
}

我的日志: -

05-18 20:56:41.538 26693-26693/com.ph.com.neostat W/System: ClassLoader referenced unknown path: /data/app/com.ph.com.neostat-1/lib/x86
05-18 20:56:41.598 26693-26703/com.ph.com.neostat I/art: Background sticky concurrent mark sweep GC freed 15897(1349KB) AllocSpace objects, 8(176KB) LOS objects, 72% free, 1026KB/3MB, paused 7.468ms total 52.247ms
05-18 20:56:41.741 26693-26693/com.ph.com.neostat W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-18 20:56:41.835 26693-26988/com.ph.com.neostat D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                    [ 05-18 20:56:42.023 26693:26693 D/         ]
                                                                    HostConnection::get() New Host Connection established 0xaa97fda0, tid 26693


                                                                    [ 05-18 20:56:42.157 26693:26988 D/         ]
                                                                    HostConnection::get() New Host Connection established 0xaec12aa0, tid 26988
05-18 20:56:42.355 26693-26988/com.ph.com.neostat I/OpenGLRenderer: Initialized EGL, version 1.4
05-18 20:56:43.596 26693-26988/com.ph.com.neostat W/EGL_emulation: eglSurfaceAttrib not implemented
05-18 20:56:43.596 26693-26988/com.ph.com.neostat W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa75e620, error=EGL_SUCCESS
05-18 20:56:43.784 26693-26693/com.ph.com.neostat I/Choreographer: Skipped 99 frames!  The application may be doing too much work on its main thread.
05-18 20:56:45.295 26693-26693/com.ph.com.neostat I/Choreographer: Skipped 86 frames!  The application may be doing too much work on its main thread.
05-18 20:56:45.621 26693-26699/com.ph.com.neostat W/art: Suspending all threads took: 38.568ms
05-18 20:57:25.469 26693-26988/com.ph.com.neostat W/EGL_emulation: eglSurfaceAttrib not implemented
05-18 20:57:25.470 26693-26988/com.ph.com.neostat W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab27ec00, error=EGL_SUCCESS
05-18 20:57:27.763 26693-26988/com.ph.com.neostat E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb2eef960
05-18 20:57:36.429 26693-26988/com.ph.com.neostat W/EGL_emulation: eglSurfaceAttrib not implemented
05-18 20:57:36.429 26693-26988/com.ph.com.neostat W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab27ef40, error=EGL_SUCCESS
05-18 20:57:37.093 26693-26988/com.ph.com.neostat E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb2eef8f0
05-18 20:57:43.260 26693-26699/com.ph.com.neostat W/art: Suspending all threads took: 79.213ms

                                                         --------- beginning of system
05-18 20:57:44.483 26693-26693/com.ph.com.neostat W/FragmentManager: moveToState: Fragment state for classified_home{9dbcc53 #0 id=0x7f0c006a android:switcher:2131492970:1} not updated inline; expected state 3 found 2
05-18 20:57:44.747 26693-26988/com.ph.com.neostat W/EGL_emulation: eglSurfaceAttrib not implemented
05-18 20:57:44.748 26693-26988/com.ph.com.neostat W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab27ef60, error=EGL_SUCCESS
05-18 20:57:45.461 26693-26988/com.ph.com.neostat E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb2eef5e0
05-18 20:57:48.924 26693-26693/com.ph.com.neostat W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=561.48926, y[0]=977.46094, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=7782899, downTime=7776256, deviceId=0, source=0x1002 }
05-18 20:57:48.924 26693-26693/com.ph.com.neostat W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=561.48926, y[0]=977.46094, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=7782899, downTime=7776256, deviceId=0, source=0x1002 }
05-18 20:57:48.924 26693-26693/com.ph.com.neostat W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=561.48926, y[0]=977.46094, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=7782899, downTime=7776256, deviceId=0, source=0x1002 }
05-18 20:57:48.924 26693-26693/com.ph.com.neostat W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=561.48926, y[0]=977.46094, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=7782899, downTime=7776256, deviceId=0, source=0x1002 }

1 个答案:

答案 0 :(得分:0)

正如塞尔文在评论中所说,请记住以下事项。

1。 events_adapter adapter = new events_adapter(events); - ,以便在获取数据(http响应)后为RecyclerView设置适配器,或者在此之前设置适配器,请致电adpter.notifyDataSetChanged();这样RecyclerView将刷新视图。

2。 events = new ArrayList<>(); in loop - 在每个循环上创建events对象,而您只想创建一次。

3。而不是System.out.println("blah blah blah");使用Android Logging API - Log

将以下更改改为onResponse方法

解决方案1 ​​
在发出请求或创建适配器实例之前调用此行events = new ArrayList<>();。     

for (int i=0;i<jsonArray.length();i++){
    JSONObject jsonObject = jsonArray.getJSONObject(i);    
    events.add(new events(jsonObject.getString("title"),jsonObject.getString("image")));
    Log.i("TAG","Title "+jsonObject.getString("title"));
}
adapter.notifyDataSetChanged();

解决方案2

events = new ArrayList<>();
for (int i=0;i<jsonArray.length();i++){
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    events.add(new events(jsonObject.getString("title"),jsonObject.getString("image")));
    Log.i("TAG","Title "+jsonObject.getString("title"));
}
events_adapter adapter = new events_adapter(events);
recyclerView.setAdapter(adapter);