I have been working on recyclerView but when i run my app it is not showing any data on the activity and app crashes and it give error on this method
Here is my Adaptor class:
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
private ArrayList<info> Info=new ArrayList<>();
public Adaptor(ArrayList<info> info) {
this.Info = info;
}
public void updateList(ArrayList<info> data) {
Info = data;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
info i= Info.get(position);
holder.name.setText(i.getName());
holder.city.setText(i.getCity());
holder.city.setText(i.getUniversity());
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, city, uni;
public ViewHolder(final View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
city = (TextView) v.findViewById(R.id.city);
uni = (TextView) v.findViewById(R.id.university);
}
}
public int getItemCount() {
return Info.size();
}
}
Here is my activity class:
public class Home extends AppCompatActivity implements HomeCallBack {
public ArrayList<info> Info;
private Adaptor myadaptor;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView=(RecyclerView)findViewById(R.id.recycle_view);
Info=new ArrayList<>();
myadaptor=new Adaptor(Info);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(myadaptor);
BackgroundHome backgroundHome=new BackgroundHome(this,this);
backgroundHome.execute();
}
@Override
public void processData(JSONArray data)
{
JSONObject js;
if(data!=null)
{
Info=new ArrayList<>();
try
{
for(int i=0;i<data.length();i++)
{
js =data.getJSONObject(i);
info in=new
info(js.getString("Names"),js.getString("city"),
js.getString("university"));
Info.add( in);
}
myadaptor=new Adaptor(Info);
recyclerView.setAdapter(myadaptor);
} catch (JSONException e) {
e.printStackTrace();
}
myadaptor.updateList(Info);
}
Whenever i run it, it crashes and the error occur in getItemcount() method that it is null but i have initialize it to Info.size();