Cast_detail_fragment:
public class Cast_detail_fragment extends Fragment {
VolleySingleton volleySingleton;
RequestQueue requestQueue;
Cast_detail_adapter adapter;
private ArrayList<cast_class_details> castlist=new ArrayList<>();
RecyclerView cast_recycler;
public Cast_detail_fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cast_detail_fragment, container, false);
cast_recycler= (RecyclerView) view.findViewById(R.id.fragment_cast_recycler);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
cast_recycler.setLayoutManager(layoutManager);
//cast_recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter=new Cast_detail_adapter(getActivity());
cast_recycler.setAdapter(adapter);
getdata();
adapter.setcastlist(castlist);
// Toast.makeText(getActivity(),"far",Toast.LENGTH_LONG).show();
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
}
private void getdata() {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json?apikey=54wzfswsa4qmjg8hjwa64d4c", (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parseData(response);
// setData();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "error volley", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(objectRequest);
}
private void parseData(JSONObject response) {
try {
String chara;
String name;
JSONArray cast= response.getJSONArray("cast");
JSONObject cast_object;
for(int i=0;i<cast.length();i++){
cast_object=cast.getJSONObject(i);
name=cast_object.getString("name");
//Toast.makeText(this, name, Toast.LENGTH_LONG).show();
JSONArray character=cast_object.getJSONArray("characters");
if(!character.isNull(0)){
chara=character.getString(0);
}else{
chara="";
}
cast_class_details cast_class_object=new cast_class_details();
cast_class_object.character=chara;
cast_class_object.name=name;
castlist.add(cast_class_object);
}
Toast.makeText(getActivity(), "done", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(),String.valueOf(castlist.size()),Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(),"error",Toast.LENGTH_SHORT).show();
}
}
}
Fragment_cast_detail.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mohd.materialdesign.Cast_detail_fragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:id="@+id/fragment_cast_recycler"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</FrameLayout>
Class_detail_adapter:
public LayoutInflater layoutinflater;
ArrayList<cast_class_details> castlist=new ArrayList<cast_class_details>();
public Cast_detail_adapter(Context context)
{
layoutinflater=LayoutInflater.from(context);
}
public void setcastlist(ArrayList<cast_class_details> castlist){
this.castlist=castlist;
notifyItemRangeChanged(0,castlist.size());
// Toast.makeText(this,String.valueOf(castlist.size()),Toast.LENGTH_SHORT).show();
}
@Override
public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= layoutinflater.inflate(R.layout.custom_cast_row,parent,false);
Myholder viewholder=new Myholder(view);
return viewholder;
}
@Override
public void onBindViewHolder(Myholder holder, int position) {
cast_class_details cast_class_object=castlist.get(position);
// holder.name.setText(cast_class_object.name);
// holder.character.setText(cast_class_object.character);
holder.name.setText(String.valueOf(castlist.size()));
holder.character.setText("yo");
}
@Override
public int getItemCount() {
return castlist.size();
}
class Myholder extends RecyclerView.ViewHolder{
TextView name,character;
public Myholder(View itemView) {
super(itemView);
name= (TextView) itemView.findViewById(R.id.cast_name);
character= (TextView) itemView.findViewById(R.id.cast_character);
}
}
}
custom_cast_row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/cast_name"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/cast_character"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp" />
</LinearLayout>
我已经显示了toast来检查片段是否正常工作并且它完全正常。 Toast显示“完成”和获取的演员表数量。 但仍然片段显示空白似乎适配器或回收者视图似乎不起作用。 请帮忙,谢谢。
答案 0 :(得分:0)
public void setcastlist(ArrayList<cast_class_details> castlist){
this.castlist=castlist;
notifyItemRangeChanged(0,castlist.size());
// Toast.makeText(this,String.valueOf(castlist.size()),Toast.LENGTH_SHORT).show();
}
替换为
public void setcastlist(ArrayList<cast_class_details> castlist){
this.castlist=castlist;
notifyDataSetChanged();
// Toast.makeText(this,String.valueOf(castlist.size()),Toast.LENGTH_SHORT).show();
}
有关详细信息,请参阅https://stackoverflow.com/a/32769238/3817374。
答案 1 :(得分:0)
下面:
getdata(); //<<<
adapter.setcastlist(castlist);
setcastlist
在调用getdata
方法后执行,无需等待调用parseData
方法,在castlist
ArrayList中添加新项目。
要使其工作,请adapter.setcastlist
或parseData
方法移动onResponse
:
@Override
public void onResponse(JSONObject response) {
parseData(response);
castlist.clear();
adapter.setcastlist(castlist);
}