任何人都可以在这里提供解决方案的错误信息
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();
}
}