没有附加适配器;跳过布局
我从url获取json数据到recyclerview 我已经烘烤了响应,我正确地获取了响应,但无法设置。
javaclasses低于
package "";
public class MainRecyclerView extends ActionBarActivity {
RecyclerView recyclerView;
LinearLayout linearLayout;
RecyclerViewListview adapter;
List<Data> dataList = new ArrayList<Data>();
public String URL = "http://toscanyacademy.com/blog/mp.php";
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_recycler);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerViewListview(this, dataList);
recyclerView.setAdapter(adapter);
RequestJsonArray();
}
public void getData(JSONArray jsonArray) {
for (int i = 0; i < jsonArray.length(); i++) {
Data data = new Data();
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
data.setSong_name(jsonObject.getString("song_name"));
data.setSong_year(jsonObject.getInt("song_id"));
data.setSong_author(jsonObject.getString("artist_name"));
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
dataList.add(data);
Toast.makeText(MainRecyclerView.this, dataList.toString(), Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
}
public void RequestJsonArray() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// String songname = response.getString();
getData(response);
Toast.makeText(MainRecyclerView.this, response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainRecyclerView.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
}
第二课
package"";
public class RecyclerViewListview extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater layoutInflater;
List<Data> dataList;
public RecyclerViewListview(Context context, List<Data> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = layoutInflater.inflate(R.layout.recycle_list_raw, parent, false);
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_list_raw, null);
MyHolder holder = new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder parent, int position) {
MyHolder myHolder = (MyHolder) parent;
myHolder.song_name.setText(dataList.get(position).getSong_name());
myHolder.song_year.setText(dataList.get(position).getSong_year());
myHolder.song_year.setText(dataList.get(position).getSong_author());
}
@Override
public int getItemCount() {
return dataList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
public TextView song_name;
public TextView song_year;
public TextView song_author;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
song_name = (TextView) itemView.findViewById(R.id.stitle);
song_year = (TextView) itemView.findViewById(R.id.s_year);
song_author = (TextView) itemView.findViewById(R.id.sauthor);
}
}
}
第三课 公共类数据{
public String song_name;
public int song_year;
public String song_author;
public Data() {
}
/*public Data(String song_name, String song_year, String song_author) {
this.song_name = song_name;
this.song_year = song_year;
this.song_author = song_author;
}*/
public String getSong_author() {
return song_author;
}
public void setSong_author(String song_author) {
this.song_author = song_author;
}
public String getSong_name() {
return song_name;
}
public void setSong_name(String song_name) {
this.song_name = song_name;
}
public int getSong_year() {
return song_year;
}
public void setSong_year(int song_year) {
this.song_year = song_year;
}
}
Logcate
08-12 19:16:59.262 29356-29370/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
08-12 19:24:18.958 2054-2054/com.example.altaf.shopper E/InstantRun: Could not find slices in APK; aborting.
08-12 19:24:18.962 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
08-12 19:24:18.962 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.974 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.982 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.986 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.986 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:19.982 2054-2054/com.example.altaf.shopper E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x7c6
at android.content.res.Resources.getText(Resources.java:230)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3769)
at com.example.altaf.shopper.volley.RecyclerViewListview.onBindViewHolder(RecyclerViewListview.java:46)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
MainRecyclerView
的onCreate()
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
dataList = new ArrayList<>(); // your dataList is empty here...
recyclerView.setAdapter(adapter); // your Adapter is null here. (not initialized)
RequestJsonArray();
您需要调用方法,在将dataList传递给Adapter之前准备它。 在适当的位置调用方法。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_recycler);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
dataList = new ArrayList<>();
RequestJsonArray();
adapter = new RecyclerViewListview(this, dataList);
recyclerView.setAdapter(adapter);
}
现在在getData()
之后的dataList.add(data);
注释栏内,然后清理项目,重建然后运行。
现在您已编辑了问题:在RecyclerAdapter的onBindViewHolder()
中尝试这样做。
myHolder.song_name.setText("" + dataList.get(position).getSong_name());
myHolder.song_year.setText("" + dataList.get(position).getSong_year());
myHolder.song_year.setText("" + dataList.get(position).getSong_author());