无法使用改进在布局中附加适配器的问题来获取JSON数据。我在Manifest文件中提到了Internet权限,并在gradle中导入了所有必需的库。
我的日志:
E/RecyclerView: No adapter attached; skipping layout
W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
I/dalvikvm: Could not find method android.widget.ImageView.setImageIcon, referenced from method android.support.v7.widget.AppCompatImageView.setImageIcon
W/dalvikvm: VFY: unable to resolve virtual method 13331: Landroid/widget/ImageView;.setImageIcon (Landroid/graphics/drawable/Icon;)V
D/dalvikvm: VFY: replacing opcode 0x6f at 0x0000
**E/RecyclerView: No adapter attached; skipping layout**
**D/com.example.chintankotadia.apidemo.activity.MainActivity: onResponse:[com.example.chintankotadia.apidemo.model.Example@4221c8b8, com.example.chintankotadia.apidemo.model.Example@4221c920, com.example.chintankotadia.apidemo.model.Example@4221c940, com.example.chintankotadia.apidemo.model.Example@4221c988, com.example.chintankotadia.apidemo.model.Example@4221c9e8, com.example.chintankotadia.apidemo.model.Example@4221ca08, com.example.chintankotadia.apidemo.model.Example@4221ca68, com.example.chintankotadia.apidemo.model.Example@4221ca88, com.example.chintankotadia.apidemo.model.Example@4221caa8, com.example.chintankotadia.apidemo.model.Example@4221cb30, com.example.chintankotadia.apidemo.model.Example@4221ccb0, com.example.chintankotadia.apidemo.model.Example@4221ccd0, com.example.chintankotadia.apidemo.model.Example@4221cd70, com.example.chintankotadia.apidemo.model.Example@4221cd90, com.example.chintankotadia.apidemo.model.Example@4221cdb0, com.example.chintankotadia.apidemo.model.Example@4221d690, com.example.chintankotadia.apidemo.model.Example@422**
**E/RecyclerView: No layout manager attached; skipping layout**
D/dalvikvm: GC_CONCURRENT freed 292K, 11% free 7902K/8839K, paused 3ms+2ms, total 37ms
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: setComposingText on inactive InputConnection
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
private List<Example> list;
private RecyclerView recyclerView;
private MainAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_demo);
list = new ArrayList<>();
loadResponse();
}
private void loadResponse() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
ReqInterface request =
retrofit.create(ReqInterface.class);
Call<List<Example>> call = request.getMyJSON();
call.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call,
Response<List<Example>> response) {
if (response.isSuccessful()) {
List<Example> exampleList = response.body();
Example example = null;
for (int i = 0; i < exampleList.size(); i++) {
example = new Example();
String userid =
String.valueOf(exampleList.get(i).getUserId());
String title = exampleList.get(i).getTitle();
String body = exampleList.get(i).getBody();
example.setUserId(Integer.valueOf(userid));
example.setTitle(title);
example.setBody(body);
list.add(example);
}
Log.d(TAG, "onResponse:" + list);
adapter = new MainAdapter(MainActivity.this, list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
try {
JSONObject jObjError = new
JSONObject(response.errorBody().string());
Toast.makeText(MainActivity.this,
jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<List<Example>> call, Throwable t)
{
Toast.makeText(MainActivity.this, "Error in fetching
data", Toast.LENGTH_SHORT).show();
}
});
}
}
MainAdapter.java
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder> {
private List<Example> list;
private Context context;
public MainAdapter(MainActivity context, List<Example> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_main, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Example example = list.get(position);
holder.userId.setText(example.getUserId());
holder.title.setText(example.getTitle());
holder.body.setText(example.getBody());
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView userId, title, body;
public MyViewHolder(View itemView) {
super(itemView);
userId = itemView.findViewById(R.id.tv_userid_no);
title = itemView.findViewById(R.id.tv_title_text);
body = itemView.findViewById(R.id.tv_body_text);
}
}
}
ReqInterface.java
public interface ReqInterface {
@GET("/posts")
Call<List<Example>> getMyJSON();
}
我的模型类 Example.java
public class Example {
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("body")
@Expose
private String body;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
答案 0 :(得分:1)
您需要向LayoutManager
添加RecyclerView
。
尝试添加此内容:
recyclerView.setLayoutManager(new LinearLayoutManager(this));
仅供参考我们可以使用LayoutManager
个LinearLayoutManager
,而不只是<form method="post" class="form-horizontal" action="test/user/add" id="submitForm">
.....
</form>
。
答案 1 :(得分:1)
You have to add Layout Manager to your RecyclerView From xml
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
From code
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
答案 2 :(得分:1)
In onCreate()
of your activity,
recyclerView = findViewById(R.id.rv_demo);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);