I want to write a program to retrieve information from the server and display it in the Recycler View,But I have two problems.
When I add data to the table it will be added to the Recycler View list But when I delete, the list doesn't change.
2.Photos of each section cannot be loaded.
home.java
public class Home extends Fragment {
public Home() {
// Required empty public constructor
}
SwipeRefreshLayout srl;
RecyclerView rv;
FloatingActionButton add;
ArrayList<Post> al;
PostAdapter pa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
srl = getActivity().findViewById(R.id.srl);
rv = getActivity().findViewById(R.id.rv);
// add = getActivity().findViewById(R.id.add);
AndroidNetworking.initialize(getContext());
al = new ArrayList<>();
pa = new PostAdapter(getContext(),al);
rv.setHasFixedSize(true);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(pa);
update();
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
update();
}
});
}
private void update(){
String id = al.size() == 0 ? "0" : al.get(0).getId();
AndroidNetworking.post(Urls.host+Urls.post)
.addBodyParameter("id",id)
.build()
.getAsObjectList(Post.class, new ParsedRequestListener<List<Post>>() {
#Override
public void onResponse(List<Post> response) {
srl.setRefreshing(false);
for (Post p : response){
al.add(0,p);
pa.notifyDataSetChanged();
}
}
#Override
public void onError(ANError anError) {
srl.setRefreshing(false);
}
});
}
}
postadapter.java
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
}
}
post.java
public class Post {
String title,text, date,id;
int imageUrl;
public Post(int imageUrl, String title, String text, String time, String id) {
this.imageUrl = imageUrl;
this.title = title;
this.text = text;
this.date = time;
this.id = id;
}
public int getImage() {
return imageUrl;
}
public void setImage(int imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Try this
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
ArrayList<Post> newDataList;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
public ArrayList<Post> getDataSet() {
return al;
}
public void refresh(ArrayList<Post> list) {
newDataList = new ArrayList<>();
newDataList.addAll(list);
al.clear();
al.addAll(newDataList);
notifyDataSetChanged();
}
}
Now at time when you wants to add or delete try getting adapter.getDataSet()
and update list with adapter.refresh(yourList)
Related
Hi I wanna set my recycleview clickable . for example when click on recycleview's each view toast its parameters (like its name) . please don't rank negative I'm a newbie.
this is my mainclass to show recycleview
public class HomeFragment2 extends Fragment {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ProgressBar progressBar;
View view ;
int RecyclerViewItemPosition ;
ArrayList<String> ImageTitleNameArrayListForClick;
String ID = "id";
String SECTION = "section";
String TIME = "time";
String NAME = "name";
Button button;
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
public static HomeFragment2 newInstance() {
return new HomeFragment2();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home2, container, false);
GetDataAdapter1 = new ArrayList<>();
recyclerView =v.findViewById(R.id.recyclerView1);
progressBar =v.findViewById(R.id.progressBar1);
button =v.findViewById(R.id.button);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
JSON_DATA_WEB_CALL();
}
});
return v;
}
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(URLS.recycle_fetch,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setId(json.getInt(ID));
GetDataAdapter2.setName(json.getString(SECTION));
GetDataAdapter2.setSubject(json.getString(TIME));
GetDataAdapter2.setPhone_number(json.getString(NAME));
// Adding image title name in array to display on RecyclerView click event.
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getActivity());
recyclerView.setAdapter(recyclerViewadapter);
}
}
and my adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
cardView = (CardView) view.findViewById(R.id.mainCard);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
holder.Id.setText(getDataAdapter1.getName());
holder.Section.setText(String.valueOf(getDataAdapter1.getId()));
holder.Time.setText(getDataAdapter1.getPhone_number());
holder.Name.setText(getDataAdapter1.getSubject());
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView Id;
public TextView Section;
public TextView Time;
public TextView Name;
public ViewHolder(View itemView) {
super(itemView);
Id =itemView.findViewById(R.id.textView2);
Section =itemView.findViewById(R.id.textView4);
Time =itemView.findViewById(R.id.textView6);
Name =itemView.findViewById(R.id.textView8);
}
}
}
any body can help me? i wanna get cardview's parameters when click each of them .
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
//////////////////////////////////here you define your layout you wanna be clicked/////////////////////////
cardView = (CardView) view.findViewById(R.id.mainCard);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
holder.Id.setText(getDataAdapter1.getName());
holder.Section.setText(String.valueOf(getDataAdapter1.getId()));
holder.Time.setText(getDataAdapter1.getPhone_number());
holder.Name.setText(getDataAdapter1.getSubject());
///////////////////////////////// and here you add your on click listener////////////////////////////////
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView Id;
public TextView Section;
public TextView Time;
public TextView Name;
public ViewHolder(View itemView) {
super(itemView);
Id =itemView.findViewById(R.id.textView2);
Section =itemView.findViewById(R.id.textView4);
Time =itemView.findViewById(R.id.textView6);
Name =itemView.findViewById(R.id.textView8);
}
}
}
I want to create this card view inside recycler View.
I got this problem.I can't retrieve the data inside of the card views.
I want to whenever that Check Box is true, Get the data(Meal, price, and count which is in this example (1)) and add to the list in my Main Activity.
I am stuck on this for 6 hour straight. If someone know the solution please help. I am dying over here.
MainActivity:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
CardData data;
List<CardData> list=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Hawk.init(getApplicationContext()).build();
data = new CardData("Meal","1","12","http");
list.add(data);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
final RecyclerView.Adapter adapter=new MyAdapter(getApplicationContext(),list);
recyclerView.setAdapter(adapter);
}
}
Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<CardData> data;
private Context context;
public MyAdapter(Context context,List<CardData> data){
this.data=data;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_contents,viewGroup,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {
myViewHolder.Meal.setText(data.get(i).Yemek);
myViewHolder.price.setText(data.get(i).Qiymet);
//Glide.with(context).load(data.get(i).url).into(myViewHolder.img);
myViewHolder.count.getText();
myViewHolder.card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myViewHolder.checkBox.setChecked(!myViewHolder.checkBox.isChecked());
data.get(i).checked = myViewHolder.checkBox.isChecked();
}
});
myViewHolder.add.setFocusable(true);
myViewHolder.remove.setFocusable(true);
myViewHolder.add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String countS = myViewHolder.count.getText().toString();
int countI = Integer.valueOf(countS);
countI += 1;
String countN = String.valueOf(countI);
myViewHolder.count.setText(countN);
}
});
myViewHolder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String sayS = myViewHolder.count.getText().toString();
int sayI = Integer.valueOf(sayS);
if (sayI>0){
sayI -= 1;}
String sayN = String.valueOf(sayI);
myViewHolder.count.setText(sayN);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView Meal;
TextView price;
TextView count;
CardView card;
ImageView img;
CheckBox checkBox;
Button add;
Button remove;
MyViewHolder(View view){
super(view);
this.Meal = (TextView) view.findViewById(R.id.Meal);
this.price = (TextView) view.findViewById(R.id.Cost);
this.count = (TextView) view.findViewById(R.id.Count);
this.add = (Button) view.findViewById(R.id.addButton);
this.remove = (Button) view.findViewById(R.id.removeButton);
this.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
this.img = (ImageView) view.findViewById(R.id.img);
this.card = (CardView) view.findViewById(R.id.card_view);
}
}
}
Data:
public class CardData {
public CardData() {
}
public String Meal;
public String Portion;
public String Cost;
public String url;
public Boolean checked;
public CardData(String meal, String portion, String cost, String url) {
Meal = meal;
Portion = portion;
Cost = cost;
this.url = url;
}
public Boolean isChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public String getMeal() {
return Meal;
}
public void setMeal(String meal) {
Meal = meal;
}
public String getPortion() {
return Portion;
}
public void setPortion(String portion) {
Portion = portion;
}
public String getCost() {
return Cost;
}
public void setCost(String cost) {
Cost = cost;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
How do i set data in RecylerView using Retrofit.I have tried but i was not able to performed it successfully.
Here is my code:
JSON DATA is :
{
"model": [
{
"id": "1",
"brand_name": "Audi",
"brand_logo": "1495456122.",
},
{
"id": "3",
"brand_name": "BMW",
"brand_logo": "1495451144.",
}
]
}
BrandSelectActivity.java
public class BrandSelectActivity extends AppCompatActivity implements View.OnClickListener {
ProgressDialog pDialog;
Button btn_addVehicle;
BrandListRecyclerAdapter adapter;
RecyclerView recyclerView_brand;
public ImageView carBrandImg;
public TextView carBrandName;
private static String TAG = BrandSelectActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brand_select);
carBrandImg = (ImageView)findViewById(R.id.carBrandImg);
carBrandName = (TextView)findViewById(R.id.carBrandName);
recyclerView_brand = (RecyclerView) findViewById(R.id.recycler_view);
pDialog = new ProgressDialog(getApplicationContext());
pDialog.setCancelable(true);
pDialog.setMessage("Please Wait...");
pDialog.show();
ArrayList<BrandModel> brandModelArrayList = new ArrayList<BrandModel>();
BrandListRecyclerAdapter brandAdapter = new BrandListRecyclerAdapter(BrandSelectActivity.this, brandModelArrayList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView_brand.setLayoutManager(mLayoutManager);
recyclerView_brand.setAdapter(brandAdapter);
brandAdapter.setItemClickListener(new BrandListRecyclerAdapter.MyClickListerer() {
#Override
public void onItemClick(int position, View view) {
Intent sosIntent = new Intent(BrandSelectActivity.this, ModelSelectActivity.class);
startActivity(sosIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
callBrandSelect();
}
private void callBrandSelect() {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<BrandSelectResponse> call = apiService.saveBrand("1");
call.enqueue(new Callback<BrandSelectResponse>() {
#Override
public void onResponse(Call<BrandSelectResponse> call, retrofit2.Response<BrandSelectResponse> response) {
BrandSelectResponse br= response.body();
List<BrandSelectResponse.ModelBean> modelList = br.getModel();
modelList.size();
modelList.get(0).getBrand_logo();
modelList.get(0).getBrand_name();
pb.dismiss();
}
#Override
public void onFailure(Call<BrandSelectResponse>call, Throwable t) {
Toast.makeText(BrandSelectActivity.this,"Opps ..!!Failed to
connect to our server.. Try Again later..",Toast.LENGTH_SHORT).show();
Log.e(TAG, t.toString());
}
});
}
#Override
public void onClick(View v) {
}
}
BrandListRecyclerAdapter.java
public class BrandListRecyclerAdapter extends RecyclerView.Adapter<BrandListRecyclerAdapter.MyViewHolder> {
Context mcontext;
List<BrandSelectResponse> brandModelArrayList;
public LayoutInflater inflater;
public static MyClickListerer myClickListerer;
public BrandListRecyclerAdapter(Context context, ArrayList<BrandSelectResponse> brandModelArrayList) {
this.mcontext = context;
this.brandModelArrayList=brandModelArrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_brand, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
BrandSelectResponse current = brandModelArrayList.get(position);
holder.setData((BrandSelectResponse) brandModelArrayList,position);
holder.carBrandName.setText(current.getBrand_name());
holder.carBrandImg.setImageResource(current.getBrand_logo());
}
public interface MyClickListerer {
void onItemClick(int position, View view);
}
public void setItemClickListener(MyClickListerer myClickListerer) {
this.myClickListerer = myClickListerer;
}
#Override
public int getItemCount() {
return brandModelArrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public ImageView carBrandImg;
public TextView carBrandName;
BrandSelectResponse brandModelArrayList;
LinearLayout current;
int position;
public MyViewHolder(View itemView) {
super(itemView);
carBrandImg = (ImageView) itemView.findViewById(R.id.carBrandImg);
carBrandName = (TextView) itemView.findViewById(R.id.carBrandName);
}
public void setData(BrandSelectResponse current, int position) {
carBrandName.setText(brandModelArrayList.getBrand_name());
carBrandImg.setImageResource(brandModelArrayList.getBrand_logo());
this.position = position;
this.brandModelArrayList = current;
}
#Override
public void onClick(View v) {
myClickListerer.onItemClick(getAdapterPosition(), v);
}
}
}
BrandSelectResponse.java
public class BrandSelectResponse implements Serializable{
private List<ModelBean> model;
public BrandSelectResponse(List<ModelBean> model) {
this.model = model;
}
public List<ModelBean> getModel() {
return model;
}
public void setModel(List<ModelBean> model) {
this.model = model;
}
public static class ModelBean {
private String id;
private String brand_name;
private String brand_logo;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getBrand_logo() {
return brand_logo;
}
public void setBrand_logo(String brand_logo) {
this.brand_logo = brand_logo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}
You give to adapter EMPTY list.
You never add data to brandModelArrayList.
Next time try to look at the logcat, what is the exception and which line is the problem.
I am trying to fetch data from my Parse database and display it into a RecyclerView. From my knowledge I cannot see what I have done wrong and when I run the app not errors occur, and when opening this very fragment the app doesn't crash just shows a blank screen.
Any idea what is the problem?
Businesses
package zafir.com.app;
import com.parse.ParseClassName;
import com.parse.ParseObject;
#ParseClassName("Businesses")
public class Businesses extends ParseObject
{
private String Name;
public String getName()
{
return getString("Name");
}
public void setName(String name)
{
put("Name", name);
}
public String getCategory()
{
return getString("Category");
}
public void setCategory(String category)
{
put("Category", category);
}
public String getEmail()
{
return getString("Email");
}
public void setEmail(String email)
{
put("Email", email);
}
public String getLocation()
{
return getString("Location");
}
public void setLocation(String location)
{
put("Location", location);
}
public String getPhone()
{
return getString("Phone");
}
public void setPhone(String phone)
{
put("Phone", phone);
}
public String getWebsite()
{
return getString("Website");
}
public void setWebsite(String website)
{
put("Website", website);
}
}
RecyclerAdapter
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView zName;
public TextView zPhone;
public TextView zEmail;
public TextView zWebsite;
public TextView zLocation;
public TextView zCategory;
public ViewHolder(View itemView)
{
super(itemView);
zName = (TextView) itemView.findViewById(R.id.name);
zPhone = (TextView) itemView.findViewById(R.id.phone);
zEmail = (TextView) itemView.findViewById(R.id.email);
zWebsite = (TextView) itemView.findViewById(R.id.website);
zLocation = (TextView) itemView.findViewById(R.id.location);
zCategory = (TextView) itemView.findViewById(R.id.category);
}
}
public RecyclerAdapter(Context context,List<Businesses> data)
{
inflater=LayoutInflater.from(context);
this.data= data;
}
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View recView = inflater.inflate(R.layout.recycler_layout, parent, false);
ViewHolder ViewHolder = new ViewHolder(recView);
return ViewHolder;
}
#Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int position)
{
Businesses businesses = data.get(position);
TextView name = viewHolder.zName;
name.setText(businesses.getName());
TextView phone = viewHolder.zPhone;
phone.setText(businesses.getPhone());
TextView email = viewHolder.zEmail;
email.setText(businesses.getEmail());
TextView website = viewHolder.zWebsite;
website.setText(businesses.getWebsite());
TextView location = viewHolder.zLocation;
location.setText(businesses.getLocation());
TextView category = viewHolder.zCategory;
category.setText(businesses.getCategory());
}
#Override
public int getItemCount()
{
return data.size();
}
}
Categories(Fragment)
public class Categories extends Fragment
{
List<Businesses> data = new ArrayList<>();
private RecyclerView zRecyclerView;
private RecyclerAdapter zAdapter;
private RecyclerView.LayoutManager zLayoutManager;
public Categories()
{
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_categories,container, false);
zRecyclerView = (RecyclerView) rootView.findViewById(R.id.recview_categories);
zRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
getData();
return super.onCreateView(inflater, container, savedInstanceState);
}
public void getData()
{
final List<Businesses> data = new ArrayList<>();
ParseQuery<Businesses> query = new ParseQuery<>("Businesses");
query.findInBackground(new FindCallback<Businesses>()
{
#Override
public void done(List<Businesses> list, ParseException e)
{
if(e == null)
{
for(Businesses businesses : list)
{
Businesses bizList = new Businesses();
bizList.setNames(businesses.getNames());
bizList.setPhone(businesses.getPhone());
bizList.setEmail(businesses.getEmail());
bizList.setWebsite(businesses.getWebsite());
bizList.setLocation(businesses.getLocation());
bizList.setCategory(businesses.getCategory());
data.add(bizList);
}
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
}
}
});
}
}
Everything looks good. You just have to call notifyDataSetChanged() on zAdapter whenever you are changing the data. So change your code like this:
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
zAdapter.notifyDataSetChanged();
I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);