
我想显示从mysql数据库到我的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内容
<linearLayoutandroID: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
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@OverrIDepublic 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;}@OverrIDepublic voID onBindVIEwHolder(@NonNull likelyProblemVIEwHolder holder, int position) { likelyProblems likelyProblems = mlikeyProblems.get(position); holder.likelyProblem.setText(likelyProblems.getProblems());}@OverrIDepublic 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); } } }主要活动
private List<likelyProblems> mlikelyProblems;private String liKELY_PROBLEM_URL = MYURL.url + "vehicle-problems"; @OverrIDeprotected 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 one rrorResponse(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上没有显示,我不知道可能是问题的原因.
解决方法:
Issue no 1 :- set adapter outsIDe the loop
Issue no 2 :- you forgot to notify your
likelyProblemAdapterafter adding new data in yourmlikelyProblemsIssue no 3 :- layout
尝试这个
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> 总结 以上是内存溢出为你收集整理的android-我的Recyclerview没有显示任何内容全部内容,希望文章能够帮你解决android-我的Recyclerview没有显示任何内容所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)