当前位置: 首页 > 知识库问答 >
问题:

Android-空白回收视图

吴浩皛
2023-03-14

我能够从URL发送JSON响应,然后当我在RecyclerView上显示相同的响应时,得到的输出是空白的,但选项卡和导航抽屉仍然存在。运行该应用程序时也没有错误。我也在使用截击库。

问题:我在列出服务的RecylerView上没有得到任何输出。我会在这里贴出与RecyclerView相关的代码。另外,我已经在底部发布了堆栈跟踪。

serviceViewAdapter.java(适配器)

public class ServiceViewAdapter extends RecyclerView.Adapter<ServiceViewAdapter.ViewHolderService> {

private LayoutInflater mLayoutInflater;
private ArrayList<Service> listServices = new ArrayList<>();
private VolleySingleton mVolleySingleton;
private ImageLoader mImageLoader;

public ServiceViewAdapter(Context context) {
    mLayoutInflater = LayoutInflater.from(context);
    mVolleySingleton = VolleySingleton.getInstance();
    mImageLoader = mVolleySingleton.getImageLoader();
}

public void setServiceList(ArrayList<Service> listServices){
    this.listServices = listServices;
    notifyItemRangeChanged(0, listServices.size());
}


static class ViewHolderService extends RecyclerView.ViewHolder {

    private ImageView imageViewService;
    private TextView textViewName;
    private TextView textViewOrgName;
    private TextView textViewPrice;
    private RatingBar ratingBarScoreRating;

    public ViewHolderService(View itemView) {
        super(itemView);
        imageViewService = (ImageView) itemView.findViewById(R.id.image_view_list);
        textViewName = (TextView) itemView.findViewById(R.id.textViewName);
        textViewOrgName = (TextView) itemView.findViewById(R.id.textViewOrgName);
        textViewPrice = (TextView) itemView.findViewById(R.id.textViewPrice);
        ratingBarScoreRating = (RatingBar) itemView.findViewById(R.id.ratingBarScoreRating);
    }
}

@Override
public ViewHolderService onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mLayoutInflater.inflate(R.layout.custom_services_all, parent, false);
    ViewHolderService viewHolderService = new ViewHolderService(view);
    return viewHolderService;
}

@Override
public void onBindViewHolder(final ViewHolderService holder, int position) {
    Service currentService = listServices.get(position);

    holder.textViewName.setText(currentService.getName());
    holder.textViewOrgName.setText(currentService.getOrgName());
    holder.textViewPrice.setText(String.valueOf(currentService.getPrice()));
    holder.ratingBarScoreRating.setNumStars(4);

    String imageUrl = currentService.getImageUrl();
    if(imageUrl!=null){
        mImageLoader.get(imageUrl, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                holder.imageViewService.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }
}

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

}

fragmentall.java(显示RecyclerView的片段)

public class FragmentAll extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private VolleySingleton mVolleySingleton;
private ImageLoader mImageLoader;
private RequestQueue mRequestQueue;
private ArrayList<Service> listServices = new ArrayList<>();
private ServiceViewAdapter mServiceViewAdapter;
private RecyclerView recyclerViewServices;

// TODO: Rename and change types and number of parameters
public static FragmentAll newInstance(String param1, String param2) {
    FragmentAll fragment = new FragmentAll();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public static String getRequestUrl() {
    return URL_SEARCH_BASE
            + URL_CHAR_QUESTION
            + URL_PARM_SEARCH
            + URL_CHAR_AMPERSAND
            + URL_PARM_PAGENO + MyApplication.HOME_PAGE_NUMBER;
}

public FragmentAll() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    mVolleySingleton = VolleySingleton.getInstance();
    mRequestQueue = mVolleySingleton.getRequestQueue();

    sendJsonRequest();
}

public void sendJsonRequest() {

    JsonArrayRequest request = new JsonArrayRequest
            (
                    getRequestUrl(),
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            listServices = parseJSONResponse(response);
                            mServiceViewAdapter.setServiceList(listServices);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }
            );
    mRequestQueue.add(request);
}

private ArrayList<Service> parseJSONResponse(JSONArray response) {

    ArrayList<Service> listServices = new ArrayList<>();
    if (response != null && response.length() > 0) {

        try {

            for (int i = 0; i < response.length(); i++) {
                JSONObject serviceObject = response.getJSONObject(i);
                long id = serviceObject.getLong(KEY_ID);
                String name = serviceObject.getString(KEY_NAME);
                long price = serviceObject.getLong(KEY_PRICE);

                String smallPictureUrl = null;
                if (serviceObject.has(KEY_IMAGE_THUMBNAIL)) {
                    smallPictureUrl = serviceObject.getString(KEY_IMAGE_THUMBNAIL);
                }

                JSONObject postedUserObject = serviceObject.getJSONObject(KEY_POSTED_USER);
                JSONObject profileObject = postedUserObject.getJSONObject(KEY_PROFILE);

                String orgName = profileObject.getString(KEY_ORG_NAME);
                int rating = profileObject.getInt(KEY_RATING);

                Service service = new Service();
                service.setId(id);
                service.setName(name);
                service.setPrice(price);
                service.setOrgName(orgName);
                service.setRating(rating);
                service.setImageUrl(smallPictureUrl);

                listServices.add(service);
            }
        } catch (Exception e) {

        }

    }
    return listServices;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_all, container, false);
    recyclerViewServices = (RecyclerView) view.findViewById(R.id.listAllServices);
    recyclerViewServices.setLayoutManager(new LinearLayoutManager(getActivity()));
    mServiceViewAdapter = new ServiceViewAdapter(getActivity());
    recyclerViewServices.setAdapter(mServiceViewAdapter);
    sendJsonRequest();
    return view;
}
}

fragment_all.xml(主要片段布局)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"

<android.support.v7.widget.RecyclerView
    android:id="@+id/listAllServices"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

custom_services_all.xml(片段中的单个项)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<ImageView
    android:layout_width="54dp"
    android:layout_height="80dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textViewName"
    android:layout_toRightOf="@+id/imageView"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textViewOrgName"
    android:layout_below="@+id/textViewName"
    android:layout_toRightOf="@+id/imageView"
    />

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBarScoreRating"
    android:layout_below="@+id/textViewOrgName"
    android:layout_toRightOf="@+id/imageView"
 />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textViewPrice"
    android:layout_below="@+id/textViewName"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

</RelativeLayout>

StackTrace(日志输出)

05-28 12:28:06.057 164-1644/com.piserve.geejo.mskv4 I/art:延迟启用-xcheck:jni 05-28 12:28:06.183
1644-1654/com.piserve.geejo.mskv4 I/art:调试器不再活动05-28 12:28:06.270 164-1644/com.piserve.geejo.mskv4 I/ViewUtils:App:Theme现在已不推荐使用。请改为使用Android:Theme。05-28 12:28:06.463 1644-1664/com.piserve.geejo.mskv4 d/OpenGlengRenderer:呈现请求的脏区域:true 05-28 12:28:06.476 164-1644/com.piserve.geejo.mskv4 d/:HostConnection::Get()建立的新主机连接0xABB86940,tid 1644 05-28 12:28:06.514 164-1644/com.piserve.geejo.mskv4 d/Atlas:正在验证映射...05-28 12:28:06.682
1644-1664/com.piserve.geejo.mskv4 D/libegl:已加载/system/lib/EGL/libegl_emulation.so 05-28 12:28:06.683
1644-1664/com.piserve.geejo.mskv4 D/libegl:已加载/system/lib/EGL/libglesv1_cm_emulation.so 05-28 12:28:06.724
1644-1664/com.piserve.geejo.mskv4 D/libegl:已加载t()在布局过程中被Android.Widget.ImageView{31787f8 v.ed........id 0,0-0,0#7f0b006b app:id/selector}不正确调用:运行第二个布局传递05-28 12:28:07.219
1644-1644/com.piserve.geejo.mskv4 w/view:requestLayout()被Android.Widget.ImageView{1e42a4d1 v.ed........i.0,0-0,0

requestLayout()在布局过程中被Android.Widget.ImageView{3b3e0b36 v.ed.........I.0,0-0,0#7f0b006b app:id/selector}不正确调用:运行第二个布局pass 05-28 12:28:07.243
1644-1644/com.piserve.geejo.mskv4 I/choreographer:跳过了30帧!应用程序可能在其主线程上做了太多的工作。05-28 12:28:07.997 164-1644/com.piserve.geejo.mskv4 I/编舞:跳过了44帧!应用程序可能在其主线程上做了太多的工作。05-28 12:28:11.577 1644-1668/com.piserve.geejo.mskv4 D/volley:[165]BasicNetwork.LogSlowRequests:请求的HTTP响应=<[]HTTP://myServicekart.com/public/search?search=&pageNo=1 0x7b216334 NORMAL 1>[lifetime=4932],[size=262811],[rc=200],[retrycount=0]05-28 12:28:11.597 1644-1644/com.piserve.geejo.mskv4 D/volley:[1]请求完成:4962

共有1个答案

何正德
2023-03-14

我认为您的smallPictureUrl仍然包含Null。这就是为什么您的RecyclerView没有显示,因为到现在为止listServices是空的。

您正在解析poster对吗??。来自烂番茄API??。Poster也是一个包含三个Poster URL的Json对象。在这三个URL之间,您必须解析缩略图,对吗?

所以在这里你要写

`JSONObject objPoster =   serviceObject.getJSONObject("posters");
 String smallPictureUrl = null;
            if (serviceObject.has(KEY_IMAGE_THUMBNAIL)) {
                smallPictureUrl = serviceObject.getString(KEY_IMAGE_THUMBNAIL);
            }

我希望它有帮助..

 类似资料:
  • 我正在尝试在我的 上实现 ,但我没有得到任何结果。 我遵循了这个教程和这个技巧,但是没有人为我工作。 我已经实现了: 但它不让我编译,因为它们是不同的< code > viewmoder ,因为我创建了两个< code > viewmoder 类,但它们< code >扩展了Recycler。ViewHolder所以我不明白... 我正在尝试这样做,因为我有一个,我希望当列表为空时,它会显示一个,

  • 我有 主视图 在我的模板适配器上,我有另一个用于子视图的回收器适配器 代码工作,但是当用户在android设备上滚动时,父scrollView不允许我滚动回收器视图(1)

  • 每当我在片段的OnCreateView中添加这段代码时,我的模拟器就会变成空白??我有一个活动,在按下一个按钮后,它变为空白,因为包含这个片段的另一个活动被称为。 知道吗?这是日志 12-26 19:50:25.575 2254-2304/com。实例乌梅拉西夫。带OpenGLRenderer的chattris:未能在表面0xa36f3080上设置EGL\u交换行为,错误=EGL\u成功 12-2

  • 我正在构建我的第一个Spring Boot应用程序。但是我不能正确地得到我的requestMapping控制器的回答。 这是我的主要课程: 这是我的RestController: 如果我看一下日志,我可以看到“/hi”映射: 但是当我访问:http:localhost:8080/hi时,我看到了一个空白页面,我期望看到“Hello World”文本。 为什么我得到一个空白页面? ---编辑----

  • 我试图为我的应用程序实现一个搜索过滤器,但我不知道如何去做。我在这里看到一个问题,但这只会让我更加困惑。这是我目前所做的:我在我的主活动中这样调用片段类 主类活动 RecycleView位于JobsFragment: 完整的StackTrace

  • 下面是我的JNI代码: JNIEXPORT jint JNICAL Java_org_opencv_samples_tutorial3_Sample3Native_FindSquares( } final_mat通道类型正在变为CV_32FC1。如何将频道类型转换为CV_8UC4,请帮我找出解决方法。 编辑:我已将finat_mat图像更改为CV_8UC3 但我得到空白(黑色)图像的结果??