本文介绍了E / RecyclerView:没有连接适配器;跳过布局不显示recyclerview中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮助解决这里的问题我得到的错误
Can any one help with the solution here i am getting the ERROR that
E / RecyclerView:没有连接适配器;跳过布局
E/RecyclerView: No adapter attached; skipping layout
这是我的文件。
OneFragment.java
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
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
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文件。
Below are my xml files.
list_row.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
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>
如果您需要任何档案,请告诉我
Let me know if you need any files
提前感谢。
请完整填写。
推荐答案
你需要 返回抽屉;
而不是 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();
}
}
这篇关于E / RecyclerView:没有连接适配器;跳过布局不显示recyclerview中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!