我想更改Cheesesquare-master Git链接 cheesesquare-master
使用翻新从我的网站加载列表
https://www.youtube.com/watch?v=FFHGkuWUsXg
http://www.jsonschema2pojo.org/
但不幸的是,我无法从我的网站加载。
此MainActivity.java是默认的Cheesesquare主活动。我刚刚更改了适配器片段类(FoodListFragment)。
That Foodlist fragment class initially set by random list using setupRecyclerView
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),
getRandomSublist(Cheeses.sCheeseStrings, 5)));
在这里,我想更改随机列表以从Web加载。
我的MainActivity.java
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new CheeseListFragment(), "Home");
adapter.addFragment(new FoodListFragment(), "Message");
//adapter.addFragment(new FoodlistAdapter(), "Message");
adapter.addFragment(new CheeseListFragment(), "Home Work");
viewPager.setAdapter(adapter);
}
FoodListFragment.Java
public class FoodListFragment extends Fragment {
private RecyclerView mProductRView;
private LinearLayoutManager mLayoutManager;
private ArrayList<LoadAbout> mProductList = new ArrayList<>() ;
private FoodlistAdapter mProductAdapter;
private APIInterface mAPIInterfacel;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_food_list, container, false);
setupRecyclerView(rv);
fetchData(); //Here i got calling error
//Error Description : Java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.test.myapp.Main.APIInterface.getProduct()' on a null object reference
return rv;
}
private void fetchData() {
mAPIInterfacel.getProduct().enqueue(new Callback<ArrayList<LoadAbout>>() {
@Override
public void onResponse(Call<ArrayList<LoadAbout>> call, retrofit2.Response<ArrayList<LoadAbout>> response) {
mProductList.addAll(response.body());
mProductAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ArrayList<LoadAbout>> call, Throwable t) {
Log.d("Error G", "Test: ");
}
});
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
//recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),
// getRandomSublist(Cheeses.sCheeseStrings, 5)));
//mProductAdapter = new FoodlistAdapter(this.mProductList);
//fetchData();
recyclerView.setAdapter(new FoodlistAdapter(getActivity(), mProductList) );
}
private List<String> getRandomSublist(String[] array, int amount) {
// ArrayList<String> list = new ArrayList<>(amount);
// Random random = new Random();
// while (list.size() < amount) {
// //list.add(array[random.nextInt(array.length)]);
// list.add("1 to 5");
// }
List<String> list = Arrays.asList( array );
return list;
}
public static class SimpleStringRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private final TypedValue mTypedValue = new TypedValue();
private int mBackground;
private List<String> mValues;
public static class ViewHolder extends RecyclerView.ViewHolder {
public String mBoundString;
public final View mView;
//public final ImageView mImageView;
public final TextView mTextView;
public final TextView mTextView2;
public ViewHolder(View view) {
super(view);
mView = view;
//mImageView = view.findViewById(R.id.avatar);
mTextView = view.findViewById(android.R.id.text1);
mTextView2 = view.findViewById(android.R.id.text2);
}
@Override
public String toString() {
return super.toString() + " '" + mTextView.getText();
}
}
public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_list_item, parent, false);
view.setBackgroundResource(mBackground);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
final int img_pos = position;
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
if (img_pos == 0) {
} else {
}
}
});
RequestOptions options = new RequestOptions();
}
@Override
public int getItemCount() {
return mValues.size();
}
}
}
FoodlistAdapter.java
public class FoodlistAdapter extends RecyclerView.Adapter<FoodlistAdapter.MyViewHolder> {
private Context mContext;
private ArrayList<LoadAbout> mItemList;
public FoodlistAdapter (Context con, ArrayList<LoadAbout> list) {
this.mContext = con;
this.mItemList = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_food_list, viewGroup, false);
//return null;
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
LoadAbout item = mItemList.get(i);
myViewHolder.mnameTxt.setText(item.getMessage());
}
@Override
public int getItemCount() {
//return 0;
return mItemList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mnameTxt;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mnameTxt = (TextView)itemView.findViewById(R.id.text1);
}
}
}
food_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:state_focused="false"
android:gravity="center_vertical">
<TextView
android:id="@+id/name_text"
android:paddingRight="@dimen/album_title_padding"
android:paddingLeft="@dimen/album_title_padding"
android:paddingTop="@dimen/album_title_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
<android.support.v7.widget.CardView
style="@style/Widget.CardContent"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/album_top"
android:layout_marginTop="@dimen/album_title"
android:layout_marginLeft="@dimen/album_title"
android:layout_marginRight="@dimen/album_title" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/album_margin"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:paddingRight="@dimen/album_title_padding"
android:paddingLeft="@dimen/album_title_padding"
android:paddingTop="@dimen/album_title_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="@dimen/album_title_padding"
android:paddingLeft="@dimen/album_title_padding"
android:paddingTop="@dimen/album_title_padding"
android:paddingBottom="@dimen/album_title_padding"
android:text="@string/cheese_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
APIInterface.java
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface APIInterface {
@GET("about.php")
Call<ArrayList<LoadAbout>> getProduct();
}
APIClient.java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class APIClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(){
retrofit = new Retrofit.Builder()
.baseUrl("http://app.themaxtech.com/api/test/")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
LoadAbout.java
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class LoadAbout {
@SerializedName("success")
@Expose
private Integer success;
@SerializedName("message")
@Expose
private String message;
@SerializedName("poststr")
@Expose
private List<Poststr> poststr = null;
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Poststr> getPoststr() {
return poststr;
}
public void setPoststr(List<Poststr> poststr) {
this.poststr = poststr;
}
}
Poststr.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Poststr {
@SerializedName("dat")
@Expose
private String dat;
@SerializedName("des")
@Expose
private String des;
public String getDat() {
return dat;
}
public void setDat(String dat) {
this.dat = dat;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
}