运行 recyclerView 程序出现白屏问题
problem white screen with running the program of recyclerView
我正在尝试制作一个将显示数据编程到 recyclerView 中的程序。
我的程序有问题,当我尝试在 recyclerView 中显示数据时,屏幕显示为白色,没有任何显示。如果有人知道代码有什么问题,请帮助我。附上我项目的完整代码。还附上全Logcat.
从一个页面开始 MainActivity
public class MainActivity extends AppCompatActivity {
private static final String HI = "https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/getimagelikedata.php";
private List<MyListData>myListData;
private RecyclerView rv;
private JsonArrayRequest request;
private RequestQueue requestQueue;
private MyAdapter adapter;
public static MyDatabase myDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv=(RecyclerView)findViewById(R.id.recycler_view);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
myListData=new ArrayList();
myDatabase= Room.databaseBuilder(getApplicationContext(),MyDatabase.class,"mydb").allowMainThreadQueries().build();
getData();
}
private void getData() {
request=new JsonArrayRequest(HI, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject ob=response.getJSONObject(i);
MyListData md=new MyListData(ob.getInt("id"),ob.getString("imageurl"),ob.getString("like"));
myListData.add(md);
} catch (JSONException e) {
e.printStackTrace();
}
}
setupData(myListData);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(this);
requestQueue.add(request);
}
private void setupData(List<MyListData> myListData) {
adapter=new MyAdapter( myListData,this);
rv.setAdapter(adapter);
}
}
class MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final String PO ="https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/dislikedata.php?id=" ;
private static final String DE="https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/likedata.php?id=";
private List<MyListData>myListData;
private Context ct;
private JsonArrayRequest request,request1;
private RequestQueue requestQueue,rq;
public MyAdapter(List<MyListData> myListData, Context ct) {
this.myListData = myListData;
this.ct = ct;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_data,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
final MyListData mk=myListData.get(i);
Picasso.with(ct)
.load(mk.getImageurl())
.into(viewHolder.img);
viewHolder.likecount.setText(mk.getLikecount());
if (MainActivity.myDatabase.myDao().isLiked(mk.getId())==1)
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
else
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
viewHolder.likebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LikeDislike_List mt=new LikeDislike_List();
//String uid=mk.getId();
int id=mk.getId();
String imp=mk.getImageurl();
mt.setId(id);
mt.setName(imp);
if (MainActivity.myDatabase.myDao().isLiked(id)!=1){
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
MainActivity.myDatabase.myDao().addData(mt);
String mo=mk.getLikecount();
viewHolder.likecount.setText(mo);
delieteLike(id,viewHolder);
}else {
MainActivity.myDatabase.myDao().delete(mt);
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
String mo=mk.getLikecount();
addLike(id,viewHolder);
}
}
});
}
private void addLike(final int id, final ViewHolder viewHolder) {
request=new JsonArrayRequest(PO + id, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i <response.length(); i++){
try {
JSONObject object=response.getJSONObject(i);
String like=object.getString("like");
viewHolder.likecount.setText(like);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(ct);
requestQueue.add(request);
}
private void delieteLike(final int id, final ViewHolder viewHolder) {
request1 =new JsonArrayRequest(DE + id, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject object=response.getJSONObject(i);
String like=object.getString("like");
viewHolder.likecount.setText(like);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
rq=Volley.newRequestQueue(ct);
rq.add(request1);
}
@Override
public int getItemCount() {
return myListData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img,likebtn;
private TextView likecount;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=(ImageView)itemView.findViewById(R.id.imageview);
likebtn=(ImageView)itemView.findViewById(R.id.like_btn);
likecount=(TextView)itemView.findViewById(R.id.like_count);
}
}
}
public class MyListData {
private int id;
private String imageurl;
private String likecount;
public MyListData(int id, String imageurl, String likecount) {
this.id = id;
this.imageurl = imageurl;
this.likecount = likecount;
}
public int getId() {
return id;
}
public String getImageurl() {
return imageurl;
}
public String getLikecount() {
return likecount;
}
}
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities={LikeDislike_List.class},version = 1, exportSchema = false)
public abstract class MyDatabase extends RoomDatabase {
public abstract LikeDislikeDao myDao();
}
package com.example.myapplication;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName="mydata")
public class LikeDislike_List {
@PrimaryKey
private int id;
@ColumnInfo(name = "user_name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Dao
public interface LikeDislikeDao {
@Insert
public void addData(LikeDislike_List likeDislikeList);
@Query("select * from mydata")
public List<LikeDislike_List>getMyData();
@Query("SELECT EXISTS (SELECT 1 FROM mydata WHERE id=:id)")
public int isLiked(int id);
@Delete
public void delete(LikeDislike_List likeDislikeList);
}
Logcat
02-17 19:04:01.856 7156-7161/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)
02-17 19:04:02.276 7156-7156/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
02-17 19:04:02.280 7156-7156/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
02-17 19:04:02.364 7156-7156/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
02-17 19:04:02.364 7156-7156/? E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
02-17 19:04:02.368 7156-7156/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
02-17 19:04:02.636 7156-7156/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering
02-17 19:04:02.824 7156-7156/? E/dalvikvm: Could not find class 'android.view.textclassifier.TextClassificationManager', referenced from method androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier
02-17 19:04:03.044 7156-7156/com.example.myapplication E/RecyclerView: No adapter attached; skipping layout
02-17 19:04:03.060 7156-7156/com.example.myapplication E/RecyclerView: No adapter attached; skipping layout
先生,您错过了定义 GET 请求的第三个参数
private void getData() {
request=new JsonArrayRequest(HI, Request.Method.GET,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject ob=response.getJSONObject(i);
MyListData md=new MyListData(ob.getInt("id"),ob.getString("imageurl"),ob.getString("like"));
myListData.add(md);
} catch (JSONException e) {
e.printStackTrace();
}
}
setupData(myListData);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(this);
requestQueue.add(request);
}
我正在尝试制作一个将显示数据编程到 recyclerView 中的程序。 我的程序有问题,当我尝试在 recyclerView 中显示数据时,屏幕显示为白色,没有任何显示。如果有人知道代码有什么问题,请帮助我。附上我项目的完整代码。还附上全Logcat.
从一个页面开始 MainActivity
public class MainActivity extends AppCompatActivity {
private static final String HI = "https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/getimagelikedata.php";
private List<MyListData>myListData;
private RecyclerView rv;
private JsonArrayRequest request;
private RequestQueue requestQueue;
private MyAdapter adapter;
public static MyDatabase myDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv=(RecyclerView)findViewById(R.id.recycler_view);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
myListData=new ArrayList();
myDatabase= Room.databaseBuilder(getApplicationContext(),MyDatabase.class,"mydb").allowMainThreadQueries().build();
getData();
}
private void getData() {
request=new JsonArrayRequest(HI, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject ob=response.getJSONObject(i);
MyListData md=new MyListData(ob.getInt("id"),ob.getString("imageurl"),ob.getString("like"));
myListData.add(md);
} catch (JSONException e) {
e.printStackTrace();
}
}
setupData(myListData);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(this);
requestQueue.add(request);
}
private void setupData(List<MyListData> myListData) {
adapter=new MyAdapter( myListData,this);
rv.setAdapter(adapter);
}
}
class MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final String PO ="https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/dislikedata.php?id=" ;
private static final String DE="https://uniqueandrocode.000webhostapp.com/hiren/androidtutorial/likedata.php?id=";
private List<MyListData>myListData;
private Context ct;
private JsonArrayRequest request,request1;
private RequestQueue requestQueue,rq;
public MyAdapter(List<MyListData> myListData, Context ct) {
this.myListData = myListData;
this.ct = ct;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_data,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
final MyListData mk=myListData.get(i);
Picasso.with(ct)
.load(mk.getImageurl())
.into(viewHolder.img);
viewHolder.likecount.setText(mk.getLikecount());
if (MainActivity.myDatabase.myDao().isLiked(mk.getId())==1)
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
else
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
viewHolder.likebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LikeDislike_List mt=new LikeDislike_List();
//String uid=mk.getId();
int id=mk.getId();
String imp=mk.getImageurl();
mt.setId(id);
mt.setName(imp);
if (MainActivity.myDatabase.myDao().isLiked(id)!=1){
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
MainActivity.myDatabase.myDao().addData(mt);
String mo=mk.getLikecount();
viewHolder.likecount.setText(mo);
delieteLike(id,viewHolder);
}else {
MainActivity.myDatabase.myDao().delete(mt);
viewHolder.likebtn.setImageResource(R.drawable.ic_launcher_background);
String mo=mk.getLikecount();
addLike(id,viewHolder);
}
}
});
}
private void addLike(final int id, final ViewHolder viewHolder) {
request=new JsonArrayRequest(PO + id, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i <response.length(); i++){
try {
JSONObject object=response.getJSONObject(i);
String like=object.getString("like");
viewHolder.likecount.setText(like);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(ct);
requestQueue.add(request);
}
private void delieteLike(final int id, final ViewHolder viewHolder) {
request1 =new JsonArrayRequest(DE + id, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject object=response.getJSONObject(i);
String like=object.getString("like");
viewHolder.likecount.setText(like);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
rq=Volley.newRequestQueue(ct);
rq.add(request1);
}
@Override
public int getItemCount() {
return myListData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img,likebtn;
private TextView likecount;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=(ImageView)itemView.findViewById(R.id.imageview);
likebtn=(ImageView)itemView.findViewById(R.id.like_btn);
likecount=(TextView)itemView.findViewById(R.id.like_count);
}
}
}
public class MyListData {
private int id;
private String imageurl;
private String likecount;
public MyListData(int id, String imageurl, String likecount) {
this.id = id;
this.imageurl = imageurl;
this.likecount = likecount;
}
public int getId() {
return id;
}
public String getImageurl() {
return imageurl;
}
public String getLikecount() {
return likecount;
}
}
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities={LikeDislike_List.class},version = 1, exportSchema = false)
public abstract class MyDatabase extends RoomDatabase {
public abstract LikeDislikeDao myDao();
}
package com.example.myapplication;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName="mydata")
public class LikeDislike_List {
@PrimaryKey
private int id;
@ColumnInfo(name = "user_name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Dao
public interface LikeDislikeDao {
@Insert
public void addData(LikeDislike_List likeDislikeList);
@Query("select * from mydata")
public List<LikeDislike_List>getMyData();
@Query("SELECT EXISTS (SELECT 1 FROM mydata WHERE id=:id)")
public int isLiked(int id);
@Delete
public void delete(LikeDislike_List likeDislikeList);
}
Logcat
02-17 19:04:01.856 7156-7161/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)
02-17 19:04:02.276 7156-7156/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
02-17 19:04:02.280 7156-7156/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
02-17 19:04:02.364 7156-7156/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
02-17 19:04:02.364 7156-7156/? E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
02-17 19:04:02.368 7156-7156/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
02-17 19:04:02.636 7156-7156/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering
02-17 19:04:02.824 7156-7156/? E/dalvikvm: Could not find class 'android.view.textclassifier.TextClassificationManager', referenced from method androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier
02-17 19:04:03.044 7156-7156/com.example.myapplication E/RecyclerView: No adapter attached; skipping layout
02-17 19:04:03.060 7156-7156/com.example.myapplication E/RecyclerView: No adapter attached; skipping layout
先生,您错过了定义 GET 请求的第三个参数
private void getData() {
request=new JsonArrayRequest(HI, Request.Method.GET,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject=null;
for (int i=0; i<response.length(); i++){
try {
JSONObject ob=response.getJSONObject(i);
MyListData md=new MyListData(ob.getInt("id"),ob.getString("imageurl"),ob.getString("like"));
myListData.add(md);
} catch (JSONException e) {
e.printStackTrace();
}
}
setupData(myListData);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue= Volley.newRequestQueue(this);
requestQueue.add(request);
}