我的Recyclerview没有显示任何内容 [英] My Recyclerview is not showing anything
本文介绍了我的Recyclerview没有显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将mysql数据库中的项目显示到我的recyclerview中,但是我不知道为什么它不显示任何内容
I want to display Items from the mysql database to my recyclerview but I dont know why it is not displaying anything
Recyclerview
Recyclerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_likely_problems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="@dimen/zerodimen"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context="com.example.system2.tranxav.LIkelyProblemsActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/background" />
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/likelyProblemSwipeToRefresh">
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v7.widget.RecyclerView
android:layout_below="@id/toolbar"
android:id="@+id/likelyProblemRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Recyclerview内容
Recyclerview content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/how_layout"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.CardView
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="welcome"
android:padding="8dp"
android:id="@+id/likelyProblem"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
RecyclerAdapter
RecyclerAdapter
public class LikelyProblemAdapter extends RecyclerView.Adapter<LikelyProblemAdapter.LikelyProblemViewHolder> {
private List<LikelyProblems> mlikeyProblems;
Context context;
RecyclerViewClickListener mlistener;
public interface RecyclerViewClickListener{
void onClick(View view, int position);
}
public LikelyProblemAdapter(List<LikelyProblems> mlikeyProblems, Context context) {
this.mlikeyProblems = mlikeyProblems;
this.context = context;
}
@NonNull
@Override
public LikelyProblemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.activity_likely_problem_recyclerview_item, null, false);
LikelyProblemViewHolder viewHolder = new LikelyProblemViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull LikelyProblemViewHolder holder, int position) {
LikelyProblems likelyProblems = mlikeyProblems.get(position);
holder.likelyProblem.setText(likelyProblems.getProblems());
}
@Override
public int getItemCount() {
return mlikeyProblems.size();
}
class LikelyProblemViewHolder extends RecyclerView.ViewHolder {
TextView likelyProblem;
Context mCxt;
public LikelyProblemViewHolder(View itemView) {
super(itemView);
this.mCxt = mCxt;
likelyProblem = itemView.findViewById(R.id.likelyProblem);
}
}
}
MainActivity
MainActivity
private List<LikelyProblems> mlikelyProblems;
private String LIKELY_PROBLEM_URL = MYURL.url + "vehicle-problems";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_problem);
mRecyclerView = (RecyclerView) findViewById(R.id.likelyProblemRecyclerView); // Instantiate Recyclerview
mlikelyProblems = new ArrayList<>(); // Create a new array list
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
likelyProblemAdapter = new LikelyProblemAdapter(mlikelyProblems, this);
likelyProblemAdapter.notifyDataSetChanged();
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, LIKELY_PROBLEM_URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("info", response);
try {
JSONArray jsonArray = new JSONArray(response);
for (int i=0; i<=jsonArray.length()-1; i++){
JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
likelyProblem = likelyProblemJson.getString("problems");
LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
mlikelyProblems.add(likelyProblems);
mRecyclerView.setAdapter(likelyProblemAdapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
}
我在控制台上收到json响应,但在recyclerview上没有显示,我不知道是什么原因造成的.
I get the json response on the console but it does not show on the recyclerview I dont know what could be the cause of the problem.
推荐答案
问题1:-在循环外设置适配器
问题2:-,您在mlikelyProblems
问题3:-布局
尝试一下
for (int i=0; i<=jsonArray.length()-1; i++){
JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
likelyProblem = likelyProblemJson.getString("problems");
LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
mlikelyProblems.add(likelyProblems);
}
mRecyclerView.setAdapter(likelyProblemAdapter);\
likelyProblemAdapter.notifyDataSetChanged();
像这样更改您的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_likely_problems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="@dimen/zerodimen"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context="com.example.system2.tranxav.LIkelyProblemsActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/background" />
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/likelyProblemSwipeToRefresh">
<android.support.v7.widget.RecyclerView
android:layout_below="@id/toolbar"
android:id="@+id/likelyProblemRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
这篇关于我的Recyclerview没有显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文