当前位置: 首页 > 面试题库 >

E / RecyclerView:未连接适配器;跳过布局而不在recyclerview中显示数据

杨昆
2023-03-14
问题内容

任何人都可以在这里提供解决方案的错误信息

E / RecyclerView:未连接适配器;跳过布局

这是我的文件。

OneFragment.java

public class OneFragment extends Fragment implements SearchView.OnQueryTextListener {


        private static final String TAG = "RecyclerViewExample";
        private List<FeedItem> feedsList;
        private RecyclerView mRecyclerView;
        private MyRecyclerViewAdapter adapter;
        private ProgressBar progressBar;

        private SearchView mSearchView;


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

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View drawer = inflater.inflate(R.layout.fragment_one, container, false);


            mRecyclerView = (RecyclerView) drawer.findViewById(R.id.recycler_view);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            progressBar = (ProgressBar) drawer.findViewById(R.id.progress_bar);
            String url = "http://krishscs.esy.es/RecycleViewExample/RecycleViewExample.php";
            mSearchView = (SearchView) drawer.findViewById(R.id.search_view);
            setupSearchView();
            new DownloadTask().execute(url);
            adapter = new MyRecyclerViewAdapter(getActivity(),feedsList);
            Toast.makeText(getActivity(), "HELLO", Toast.LENGTH_SHORT).show();
            mRecyclerView.setAdapter(adapter);


            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_one, container, false);
        }




        public class DownloadTask extends AsyncTask<String, Void, Integer> {



            @Override
            protected void onPreExecute() {
                progressBar.setVisibility(View.VISIBLE);
            }
            @Override
            protected Integer doInBackground(String... params) {
                Integer result = 0;
                HttpURLConnection urlConnection;
                try {
                    URL url = new URL(params[0]);
                    urlConnection = (HttpURLConnection) url.openConnection();
                    int statusCode = urlConnection.getResponseCode();

                    // 200 represents HTTP OK
                    if (statusCode == 200) {
                        BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        StringBuilder response = new StringBuilder();
                        String line;
                        while ((line = r.readLine()) != null) {
                            response.append(line);
                        }
                        parseResult(response.toString());

                        result = 1; // Successful
                    } else {
                        result = 0; //"Failed to fetch data!";
                    }
                } catch (Exception e) {
                    Log.d(TAG, e.getLocalizedMessage());
                }
                return result; //"Failed to fetch data!";
            }

            @Override
            protected void onPostExecute(Integer result) {
                progressBar.setVisibility(View.GONE);

                if (result == 1) {
                    adapter = new MyRecyclerViewAdapter(getActivity(),feedsList);
                    Toast.makeText(getActivity(), "HELLO", Toast.LENGTH_SHORT).show();
                    mRecyclerView.setAdapter(adapter);
                } else {
                    Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
                }
            }
        }



        private void parseResult(String result) {
            try {
                feedsList = new ArrayList<FeedItem>();
                JSONObject response = new JSONObject(result);
                JSONArray posts = response.optJSONArray("result");
                for (int i = 0; i < posts.length(); i++) {
                    JSONObject post = posts.optJSONObject(i);
                    FeedItem item = new FeedItem();
                    item.setTitle(post.optString("name"));
                    item.setThumbnail(post.optString("image"));
                    feedsList.add(item);
                }
                Log.d("POSTDATA",feedsList.toString() );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        private void setupSearchView() {
            // mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(this);
    //        adapter.setFilter(feedsList);
            // mSearchView.setSubmitButtonEnabled(true);
            mSearchView.setQueryHint("Search here....");
        }

        @SuppressLint("ResourceType")
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            MenuInflater Menuinflater = getActivity().getMenuInflater();
            Menuinflater.inflate(R.layout.searchview_in_offers, menu);
             super.onCreateOptionsMenu(menu,inflater);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Take appropriate action for each action item click
            int id = item.getItemId();
            if (id == R.id.search) {
                // Handle the camera action
                final SearchView searchView = (SearchView)  MenuItemCompat.getActionView(item);
                searchView.setOnQueryTextListener(this);
            }
            return true;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            final List<FeedItem> filteredModelList = filter(feedsList, newText);
            adapter.setFilter(filteredModelList);
            return true;
        }
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
        private List<FeedItem> filter(List<FeedItem> models, String query) {
            query = query.toLowerCase();final List<FeedItem> filteredModelList = new ArrayList<>();
            for (FeedItem model : models) {
                final String name = model.getTitle().toLowerCase();
                if (name.contains(query)) {
                    filteredModelList.add(model);
                }
            }
            return filteredModelList;
        }


    }

MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {


    private List<FeedItem> feedItemList;
    private Context mContext;
    //private OnItemClickListener onItemClickListener;
    View view;


    public MyRecyclerViewAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
        //this.feedItemList = new ArrayList<FeedItem>();
        // we copy the original list to the filter list and use it for setting row values
        //this.feedItemList.addAll(this.feedItemList);
    }
    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
        final FeedItem feedItem = feedItemList.get(i);
        //Download image using picasso library
        if (!TextUtils.isEmpty(feedItem.getThumbnail())) {
            Glide.with(mContext).load(feedItem.getThumbnail()).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(customViewHolder.imageView);
        }

        //Setting text view title
        customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
        customViewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), feedItem.getTitle(), Toast.LENGTH_SHORT).show();            }
        });
      /*  final View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onItemClickListener.onItemClick(feedItem);
                Toast.makeText(view.getContext(),feedItem.getTitle(), Toast.LENGTH_SHORT).show();
            }
        };*/
        /*customViewHolder.imageView.setOnClickListener(listener);
        customViewHolder.textView.setOnClickListener(listener);*/
    }
    class CustomViewHolder extends RecyclerView.ViewHolder {
        protected ImageView imageView;
        protected TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
            this.textView = (TextView) view.findViewById(R.id.title);
        }
    }


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

    public void setFilter(List<FeedItem> countryModels) {
        feedItemList = new ArrayList<>();
        feedItemList.addAll(countryModels);
        notifyDataSetChanged();
    }
}

FeedItem.java

public class FeedItem {

    private String title;
    private String thumbnail;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getThumbnail() {
        return thumbnail;
    }
    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }
}

以下是我的xml文件

list_row.xml

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    cardview:cardCornerRadius="8dp"
    cardview:cardElevation="3dp"
    cardview:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_alignParentTop="true"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:hint="Name"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="#444"
            android:textSize="18dp"
            android:textStyle="bold"/>


    </RelativeLayout>
</android.support.v7.widget.CardView>

fragment_one.java

<RelativeLayout 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"
    tools:context="com.example.charan.geofencefire.offers_window_layout">

    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_view"
        android:focusable="false"
        android:focusableInTouchMode="false">
    </android.support.v7.widget.SearchView>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/search_view" />

</RelativeLayout>

让我知道您是否需要任何文件

提前致谢。

请充分满足需求。


问题答案:

你需要 return drawer; 而不是inflater.inflate(R.layout.fragment_one, container, false);里面onCreateView()

编辑

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View drawer = inflater.inflate(R.layout.fragment_one, container, false);

            feedsList = new ArrayList<FeedItem>();
            mRecyclerView = (RecyclerView) drawer.findViewById(R.id.recycler_view);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            progressBar = (ProgressBar) drawer.findViewById(R.id.progress_bar);
            String url = "http://krishscs.esy.es/RecycleViewExample/RecycleViewExample.php";
            mSearchView = (SearchView) drawer.findViewById(R.id.search_view);
            setupSearchView();
            new DownloadTask().execute(url);
            adapter = new MyRecyclerViewAdapter(getActivity(),feedsList);
            Toast.makeText(getActivity(), "HELLO", Toast.LENGTH_SHORT).show();
            mRecyclerView.setAdapter(adapter);


            // Inflate the layout for this fragment
            return drawer;
        }

试试这个

private void parseResult(String result) {
            try {
                feedsList = new ArrayList<FeedItem>();
                JSONObject response = new JSONObject(result);
                JSONArray posts = response.optJSONArray("result");
                for (int i = 0; i < posts.length(); i++) {
                    JSONObject post = posts.optJSONObject(i);
                    FeedItem item = new FeedItem();
                    item.setTitle(post.optString("name"));
                    item.setThumbnail(post.optString("image"));
                    feedsList.add(item);
                }
                adapter = new MyRecyclerViewAdapter(getActivity(),feedsList);

                mRecyclerView.setAdapter(adapter);
                Log.d("POSTDATA",feedsList.toString() );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


 类似资料:
  • 我正在制作一个音乐播放器应用程序,其中我正在使用一个加载器将歌曲数据加载到适配器,该适配器将使用一个RecyclerView显示。然而,我得到了一个奇怪的错误,我的适配器方法不能工作。只调用适配器的构造函数方法。我还得到了“没有连接适配器;跳过布局”,尽管在stack Overflow中浏览了所有可用的解决方案。 需要注意的几点: 我已经尝试了recyclerview中“未附加适配器;跳过布局”(

  • 当应用程序启动时,我得到一个空白屏幕,当我检查日志时,我得到:E/回收人员视图:没有连接适配器;跳过布局错误 我不知道为什么?任何想法,它似乎没有附加回收器视图或添加任何数据,我附加了Main活动、DataAdapter和数据类。 主要活动 数据适配器 下面是我的数据类,将从中提取数据 我ncluded.java 收集器和设置器 这是jsonResponse类,在接口中引用 感谢任何帮助。

  • 我已经创建了一个片段,在该片段中有一个recycle view,但是当我的片段被加载时,什么也没有显示,它给出了这个错误“E/recycle view:没有连接适配器;跳过布局”。下面是适配器和片段类的代码,任何帮助都将不胜感激 适配器类: 片段类: fragment _ view _ all _ my _ recipes . XML view_all_recipe_item.xml

  • 我制作了一个基本的购物清单应用程序,它利用回收器视图来显示列表项目。我正在尝试使用带有片段的导航添加设置屏幕。我遇到了我的回收器视图的问题 主活动.kt HomeFragment.kt 设置Fragment.kt activity_main.xml fragment\u home.xml navigation.xml 不确定是否需要更多信息。提前道歉-我是android studio的新手

  • 一切正常,但数据没有显示。 但是当我签入“logcat”时,我发现了这个错误消息: 2021 10月20日11:29:37.387 7327-7327/com。实例myplay E/RecyclerView:未连接适配器;跳过布局 2021-10-20 11:29:37.388 7327-7327/com.example.myplayE/RecyClerView:未连接适配器;跳过布局 主要活动。