I need to create a RecyclerView inside a custom view. Following my code:
public class CitySinglePageGridView extends LinearLayout {
Context context ;
View mView ;
RecyclerView recyclerView ;
RecyclerView.LayoutManager layoutManager ;
RecyclerView.Adapter adapter;
public CitySinglePageGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context ;
LayoutInflater inflater;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.module_city_single_page_gridview,this,true);
recyclerView = (RecyclerView) mView.findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
adapter = new CityCardviewAdapter();
recyclerView.setAdapter(adapter);
}
}
The problem is when I run the application nothing shows in the custom view, but when I'm using this RecyclerView in a Fragment or Activity everything works fine.
Following my custom adapter:
public class CityCardviewAdapter extends RecyclerView.Adapter<CityCardviewAdapter.ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_card_item_city, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 5;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
protected TextView title;
protected TextView desc ;
protected TextView distance ;
protected TextView likeCount ;
protected ImageView thumbnail ;
protected ImageView like ;
protected ImageView Distance ;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
desc = (TextView) itemView.findViewById(R.id.desc);
distance = (TextView) itemView.findViewById(R.id.distance);
likeCount = (TextView) itemView.findViewById(R.id.likeCount);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
like = (ImageView) itemView.findViewById(R.id.like);
Distance = (ImageView) itemView.findViewById(R.id.nav);
}
}
}
I set getItemCount to 5 to test the application.
Related
I'm new to android studio . I'm trying to display items in RecycleView but it's doesn't show anything.
This is what android studio says:
E/RecyclerView: No layout manager attached; skipping layout
My Fragment :
public class Articles extends Fragment {
View root;
RecyclerView recyclerView;
NewsList[] myListData = new NewsList[] {
new NewsList("Article 1", "Some Author"),
new NewsList("Article2", "Some Author"),
new NewsList("Article3", "Some Author"),
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
return root;
}
Here is the code of my Adapter class :
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private NewsList[] listdata;
// RecyclerView recyclerView;
public NewsAdapter(NewsList[] listdata) {
this.listdata = listdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final NewsList myListData = listdata[position];
holder.textView1.setText(listdata[position].getTitle());
holder.textView2.setText(listdata[position].getAuthor());
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getTitle(),Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView1;
public TextView textView2;
public RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
}
}
Previously I had an error where the app crashed when opened the fragment but now it's skips the RecyclerView.
I will be gratefull if you help me to resolve this issue.
You need to add a layoutManager to your recycler view:
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
// Add this line
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()))
recyclerView.setAdapter(adapter);
return root;
}
It's mandatory to have a Layout manager for your Recyclerview. For example, you can use a linear one by adding this after inflating your view :
recyclerView.setLayoutManager(new LinearLayoutManager( context ));
where context is whatever your context is
I am creating a RecyclerView which shows imageview and textview. But Imageview is giving me error when i am setting data in adapter class then it is giving error below:
Imageview cannot be applied to TypedArray
I tried Integer data type also but it is also giving error. Please help to get out of this.
Recycler Adapter Code is here:
public class RecyclerAdpater extends RecyclerView.Adapter<RecyclerAdpater.ViewHolder> {
ArrayList<NewModel>newModels;
Context context;
private int limit = 4;
RecyclerInterface recyclerInterface;
public RecyclerAdpater(ArrayList<NewModel>newModels, Context context){
this.newModels = newModels;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_layout, parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(newModels.get(position).getSociallogo()); //here i am getting error.
holder.textView.setText(newModels.get(position).getSocailtext());
}
#Override
public int getItemCount() {
if(newModels.size() > limit){
return limit;
}else {
return newModels.size();
}
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView textView, viewtext;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.sociallogo);
textView = itemView.findViewById(R.id.socialtext);
imageView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(context, WebviewActivity.class);
intent.putExtra("URL NAME", newModels.get(getAdapterPosition()).getHref());
context.startActivity(intent);
}
}
Fragment code when i am setting the adapter:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_for_you, container, false);
recyclerView = view.findViewById(R.id.recyclerview1);
TypedArray logimages = getResources().obtainTypedArray(R.array.social_image);
String[] logotext = getResources().getStringArray(R.array.social_name);
String [] href = getResources().getStringArray(R.array.social_url);
newModels = new ArrayList<>();
for(int i =0; i<logimages.length();i++) {
NewModel newModel = new NewModel(logimages, logotext[i], href[i]);
newModels.add(newModel);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);
readapter= new RecyclerAdpater(newModels,getContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(readapter);
return view;
}
NewMode.class file:
public class NewModel {
TypedArray sociallogo;
String socailtext;
String href;
public NewModel(TypedArray sociallogo, String socailtext, String href){
this.sociallogo = sociallogo;
this.socailtext= socailtext;
this.href = href;
}
public TypedArray getSociallogo(){
return sociallogo;
}
public String getSocailtext(){
return socailtext;
}
public String getHref(){
return href;
}
}
I have a list view app and using a custom adapter, but I can't hide a specific row from the adapter.
My codes
-: In java :-
Adapter
class Adapter extends ArrayAdapter {
int[] imageArray;
String[] titleArray;
String[] descriptionArray;
public Adapter(Context context, String[] titles1, String [] description1, int[] img1) {
super(context, R.layout.settings_list_row, R.id.settingsTitles, titles1);
this.imageArray = img1;
this.titleArray = titles1;
this.descriptionArray = description1;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.settings_list_row,parent,false);
ImageView myImage = (ImageView) row.findViewById(R.id.settingsIcons);
TextView myTitle = (TextView) row.findViewById(R.id.settingsTitles);
TextView myDescription = (TextView) row.findViewById(R.id.settingsDescriptions);
myImage.setImageResource(imageArray[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
Main activity
String [] titles = {"ti1","ti2","ti3","ti4","ti5"};
String [] descriptions = {"de1","de2","de3","de4","de5"};
int [] images = {R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image};
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv = (ListView) findViewById(R.id.listView);
final Adapter adapter = new Adapter(getApplicationContext(), titles, descriptions, images);
lv.setAdapter(adapter);
}
-: To remove :-
adapter.remove(position == 0);
But not working!!
In your ListAdapter, you would simply filter out the items you do not want displayed by returning modified values for getCount(), getItem() and getItemId() as appropriate.
For Example
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_list_item, null);
holder.tvName = (TextView) rowView.findViewById(R.id.textViewName);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
if (items[position].NeedForCollapsed){
System.out.println("set Visibility.Gone");
holder.tvName.setVisibility(View.GONE);
holder.img.setVisibility(View.GONE);
return rowView;
}
//new values
holder.tvName.setText(items[position].name);
holder.img.setImageResource(GetImageID(items[position]));
return rowView;
}
public class Holder
{
TextView tvName;
ImageView img;
}
I am trying to implement RecyclerView and CardView and I am working with json.
All my code is working fine and RecyclerView adapter is also not showing any error.
But in my main activity when i try to call adapter it is showing a error.
I took all the reference from here
Foodlist.java
public class FoodList extends RecyclerView.Adapter<FoodList.FoodViewHolder>{
private final Activity context;
LayoutInflater inflater;
public static class FoodViewHolder extends RecyclerView.ViewHolder {
TextView txt1;
TextView txt2;
TextView txt3;
TextView txt4;
CardView cv;
FoodViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
txt1 = (TextView) itemView.findViewById(R.id.txt1);
txt2 = (TextView) itemView.findViewById(R.id.txt2);
txt3 = (TextView) itemView.findViewById(R.id.txt3);
txt4 = (TextView) itemView.findViewById(R.id.txt4);
}
}
List<Food> food;
public FoodList(Activity context, List<Food> food) {
this.food = food;
inflater = context.getLayoutInflater();
this.context = context;
}
#Override
public FoodViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examplelistview, viewGroup, false);
FoodViewHolder fvh = new FoodViewHolder(v);
return fvh;
}
#Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
View view;
view = inflater.inflate(R.layout.examplelistview, null, true);
if (view == null) {
holder.txt1 = (TextView)view.findViewById(R.id.txt1);
holder.txt2 = (TextView) view.findViewById(R.id.txt2);
holder.txt3 = (TextView) view.findViewById(R.id.txt3);
holder.txt4 = (TextView) view.findViewById(R.id.txt4);
view.setTag(holder);
}
else {
holder = (FoodViewHolder) view.getTag();
}
final Food item = food.get(position);
holder.txt1.setText(item.txt1);
holder.txt2.setText(item.txt2);
holder.txt3.setText(item.txt3);
holder.txt4.setText(item.txt4);
}
#Override
public int getItemCount() {
return food.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Food.java:
public class Food {
public String txt1, txt2, txt3, txt4;
}
FoodViewHolder.java
public class FoodViewHolder {
TextView txt1, txt2, txt3, txt4;
}
MainActivity.java
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(llm);
final FoodList adapter = new FoodList(MainActivity.this,food);
rv.setAdapter(adapter);
And I want to call my adapter in json below in another function:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setAdapter(adapter<here it is showing the error>, new DialogInterface.OnClickListener() {
}
What should i do?
Thanks in Advance
i have a custom listview with some textviews and a button. On button click i want to save the listview values to server, to do this task i'm using asyntask but my code crashed on pregress dialog. i'm having problem how to pass activity's instance for using progress dialog
public class ListViewAdapter extends BaseAdapter{
Context context;
public arrayList<Planning> arraylist;
private static LayoutInflater inflater = null;
public PlanningList_activity planActivity;
public ListView mListview;
Db_sqlite databaseHelper;
public ListViewAdapter(Context context,ArrayList<Plan> arraylist) {
this.context = context;
this.planArraylist = plan0Arraylist;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
databaseHelper = new Db_sqlite(context);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView convertView.findViewById(R.id.Plan_no);
holder.tvDrName = (TextView) convertView.findViewById(R.id.doc_name);
holder.tvterrcode= (TextView) convertView.findViewById(R.id.mon);
holder.tvAdr = (TextView) convertView.findViewById(R.id.adr);
holder.tvClassDesc = (TextView) convertView.findViewById(R.id.class_desc);
holder.tvspeciality = (TextView) convertView.findViewById(R.id.ff_code);
holder.tvAreaName = (TextView) convertView.findViewById(R.id.area_name);
holder.tvShift = (TextView) convertView.findViewById(R.id.shift);
holder.btn_visited =(Button)convertView.findViewById(R.id.button1);
holder.btn_visited.setTag(position);
holder.btn_visited.setOnClickListener(ButtonClickListener);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Planning p = planArraylist.get(position);
holder.tvdrCode.setText(p.getDocCode());
holder.tvDrName.setText(p.getDocName());
holder.tvterrcode.setText(p.getTerrCode());
holder.tvAdr.setText(p.getAdr());
holder.tvClassDesc.setText(p.getClassDesc());
holder.tvspeciality.setText(p.getSP());
holder.tvAreaName.setText(p.getAreaName());
holder.tvShift.setText(p.getMornEven());
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvterrcode;
TextView tvAdr;
TextView tvClassDesc;
TextView tvspeciality;
TextView tvAreaName;
TextView tvShift;
Button btn_visited;
}
Probably context is null . use mContext which you are passing in SaveDCR class constructor for creating progress bar:
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Saving...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.e( "sending dvr to sqlite","yes");
}
And from Button click pass context as:
new SaveDCR(v.getContext()).execute(dvr);