I have a problem to updae my recycle when updating scroll up/down.
I have 3 (Custom ítems with respect views) but my onBindViewHolder only calls 2, when I try down list, my list not update correctly aparently It makes a cut (0.2 sec)
I don't know why not update correctly with 3 (ítems with 3 images)
My holder : (Why count 2?) but I when down, count 3 ...
public class CAPusheenAdapter_Fragment_0_7 extends RecyclerView.Adapter<CAPusheenAdapter_Fragment_0_7.ViewHolder> {
private ArrayList<CustomItem_0_ALL> pusheenArrayList;
private int itemLayout;
private Context context;
DBHelper db ;
RecyclerView recyclerView;
public CAPusheenAdapter_Fragment_0_7(ArrayList<CustomItem_0_ALL> data, int itemLayout, Context context, RecyclerView recyclerView){
setHasStableIds(true);
this. pusheenArrayList = data;
this. itemLayout = itemLayout;
this. context = context;
db = new DBHelper(context);
this. recyclerView = recyclerView;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ArrayList<TextView> allTextViews;
//public TextView text1,text2,text3,text4,text5,text6,text7;
public LinearLayout l1;
public ViewHolder(View itemView) {
super(itemView);
int numOfTextViews=6;
allTextViews = new ArrayList<TextView>(); allTextViews.add(0,null);
l1 = (LinearLayout)itemView.findViewById(R.id.l1);
for(int i=1;i<=numOfTextViews;i++){
allTextViews.add(i,Config.getViewsComplext(context,itemView, "text" + i));
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//final int itemPosition = Fragment_0.recyclerView.getChildPosition(view); // todo SOO COOL LINE !
final int itemPosition = recyclerView.getChildPosition(view); // todo SOO COOL LINE !
//Toast.makeText(context, "No plantejada", Toast.LENGTH_SHORT).show();
//MainActivity.FragmentReutil2(new Class[]{Fragment_0_4_1.class}, 0, context);
}
});
}
}
#Override
public CAPusheenAdapter_Fragment_0_7.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
Config.totalConfigFull2(context, itemLayoutView);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(CAPusheenAdapter_Fragment_0_7.ViewHolder viewHolder, int position) {
CustomItem_0_ALL pusheen = pusheenArrayList.get(position);
ArrayList<String> texts = new ArrayList<>();
texts.add(0,null);
texts.add(1,pusheen.getText1());
texts.add(2,pusheen.getText2());
texts.add(3,pusheen.getText3());
texts.add(4,pusheen.getText4());
texts.add(5,pusheen.getText5());
texts.add(6,pusheen.getText6());
for ( int i = 1 ; i < viewHolder.allTextViews.size() ; i+=1 ){
if(texts.get(i).length()>0){
viewHolder.allTextViews.get(i).setText(texts.get(i));
viewHolder.allTextViews.get(i).setVisibility(View.VISIBLE);
}else{
viewHolder.allTextViews.get(i).setVisibility(View.GONE);
}
}
//viewHolder.l1.setBackgroundResource(pusheen.getImg());
int w =Config.getWigthHeightScreen(context)[0];
int h =Config.getWigthHeightScreen(context)[1];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, (int) (h/2.5));
viewHolder.l1.setBackgroundResource(pusheen.getImg());
viewHolder.l1.setLayoutParams(lp);
}
#Override
public int getItemCount() {
return this.pusheenArrayList.size();
}
}
Note : only call/count elements from screen not call out elements, only call when you move list up/down
I found problem :
This is the problem : and solution
AsyncTask loading image RecyclerView
Related
hi I want to make a recycler view in two different fragments in which one fragment has limit recycler view list which is shown only 4 lists and another fragment in which all the list item is shown, thanks in advance.
main class code,
private void populatelist() {
List<reviewModel> reviewModelList = new ArrayList<>();
for (int i = 1; i < 20; i++) {
int imges = R.drawable.ic_userlogin;
String names = "User Name is " + i;
String dates = "New Dates is " + i;
String detail = "User details about is " + i;
reviewModel models = new reviewModel(names, dates, detail, 4, imges);
reviewModelList.add(models);
}
setupRecycle(reviewModelList);
}
private void setupRecycle(List<reviewModel> reviewModelList) {
if (adaptOverView == null)
adaptOverView = new reviewAdapt(this, 5);
adaptOverView.setReviewList(reviewModelList);
LinearLayoutManager layout = new LinearLayoutManager(this);
layout.setSmoothScrollbarEnabled(true);
recyclerOverView.setLayoutManager(layout);
recyclerOverView.setHasFixedSize(true);
recyclerOverView.setAdapter(adaptOverView);
}
adapter class code,
private Context mContext;
private List<reviewModel> reviewList;
private int limit;
public void setReviewList(List<reviewModel> list){
this.reviewList = list;
this.notifyDataSetChanged();
}
public reviewAdapt(Context mContext, int limit) {
this.mContext = mContext;
this.limit = limit;
}
public reviewAdapt(Context mContext, List<reviewModel> reviewList, int limit) {
this.mContext = mContext;
this.reviewList = reviewList;
this.limit = limit;
}
#NonNull
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.review_design, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if (position < limit) {
reviewModel model = reviewList.get(position);
Log.d("TAG", "onBindViewHolder_Limit: "+limit);
Log.d("TAG", "onBindViewHolder_Position_Is_InLimit: "+position);
holder.textViewName.setText(model.getName());
holder.textViewDate.setText(model.getDate());
holder.ratingBarRecycle.setRating(model.getRatingBar());
holder.textViewDetails.setText(model.getDetails());
holder.imageViewRecycle.setImageResource(model.getImg());
}
}
#Override
public int getItemCount() {
return reviewList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewName, textViewDate, textViewDetails;
RatingBar ratingBarRecycle;
ImageView imageViewRecycle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.name_review);
textViewDate = itemView.findViewById(R.id.date_review);
textViewDetails = itemView.findViewById(R.id.details_review);
ratingBarRecycle = itemView.findViewById(R.id.review_ratingsBar);
imageViewRecycle = itemView.findViewById(R.id.profile_image);
}
}
}
As per my understanding , You are using limit variable for restricting listing size for fragment. But you are not using it rightly. If I am not wrong, Try to restrict item count for in adapter as below -
#Override
public int getItemCount() {
return limit;
}
I want to save data in recyclerview using ArrayList dynamically, but I'm having a problem.
Every time I add data in recycler view, it replaces the previous data and shows only the last data I have entered.
I'm passing that value using SharedPreference to next activity like this
builder.setNeutralButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences sp = getSharedPreferences(mypreference,0);
SharedPreferences.Editor editor = sp.edit();
size = sp.getInt("size",size);
size = size+1;
editor.putString("name"+size , scanResult);
editor.putInt("size" , size);
editor.commit();
finish();
}
});
This is the activity where I'm adding data in ArrayList;
Results.java
ArrayList<String> data = new ArrayList<>();
pref = getSharedPreferences(mypreference,MODE_PRIVATE);
int size = pref.getInt("size",0);
String scanResult = pref.getString("name"+size,"");
data.add(scanResult);
listView_result = findViewById(R.id.listResult);
LinearLayoutManager layoutManager = new
LinearLayoutManager(getApplicationContext());
listView_result.setLayoutManager(layoutManager);
ResultAdapter adapter = new ResultAdapter(Results.this, data);
listView_result.setAdapter(adapter);
This the adapter for recyclerview
ResultAdapter.java
public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ViewHolder> {
private Context context;
private ArrayList<String> code;
ResultAdapter(Context context , ArrayList<String> code) {
this.context = context;
this.code = code;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_text , parent , false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position)
{
holder.txt.setText( code.get(position));
}
#Override
public int getItemCount() {
return code.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
ViewHolder(View itemView) {
super(itemView);
txt = itemView.findViewById(R.id.textView_list);
}
}
}
int size = pref.getInt("size",0);
String scanResult = pref.getString("name"+size,"");
data.add(scanResult);
Above should be
int size = pref.getInt("size",0);
for (int i = 0; i < size; ++i) {
String scanResult = pref.getString("name"+i,"");
data.add(scanResult);
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContactViewHolder> {
static final String myTag = "DocsUpload";
ArrayList<RecyclerItem> listItems = new ArrayList<>();
RecyclerView.ViewHolder holder;
int clickcount=0;
private Context mContext;
List list = new ArrayList();
Boolean signUpModeActive = true;
public MyAdapter(ArrayList<RecyclerItem> listItems, Context mContext) {
this.listItems = listItems;
this.mContext = mContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view, mContext, listItems);
return contactViewHolder;
}
#Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem itemList = listItems.get(position);
holder.txtTitle.setText(itemList.getTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public CheckBox checkBox;
public TextView txtDescription;
public ImageView txtOptionDigit;
ArrayList<RecyclerItem> listitems = new ArrayList<RecyclerItem>();
Context ctx;
public ContactViewHolder(View view, Context ctx, ArrayList<RecyclerItem> listitems) {
super(view);
this.listitems = listitems;
this.ctx = ctx;
view.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxxml);
}
boolean m=false;
#Override
public void onClick(View v) {
clickcount=clickcount+1;
Log.e("count", String.valueOf(clickcount));
int position = getAdapterPosition();
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle();
if (signUpModeActive=true) {
signUpModeActive = false;
listItems.get(position).setTitle("");
listItems.get(position).setTitle(getname + " " +"absent");
} else {
signUpModeActive = true;
listItems.get(position).setTitle("");
listItems.get(position).setTitle(getname + " " +"present");
}
notifyDataSetChanged();
}
}
public void getsetadapt(String pos) {
list.add(pos);
Log.e("listdata", String.valueOf(list));
for (int i=0;i < list.size();i++)
{
Log.i("Value "+i, String.valueOf(list.get(i)));
i++;
}
}
when I click any list items it want add absent to existing text and if I click it again it want to replace absent with present..can any one know where iam going wrong ...and if run this code at my emulator it works and it is adding the text it is not replacing. For example the if I click student oviya its adding text absent near to oviya but if I click it again it is not replacing its is adding present next to that again and again
Just change your adapter to the following adapter and it will work fine
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContactViewHolder> {
static final String myTag = "DocsUpload";
ArrayList<RecyclerItem> listItems = new ArrayList<>();
RecyclerView.ViewHolder holder;
int clickcount = 0;
private Context mContext;
List list = new ArrayList();
Boolean signUpModeActive = true;
public MyAdapter(ArrayList<RecyclerItem> listItems, Context mContext) {
this.listItems = listItems;
this.mContext = mContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_2, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view, mContext, listItems);
return contactViewHolder;
}
#Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem itemList = listItems.get(position);
holder.txtTitle.setText(itemList.getTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public CheckBox checkBox;
public TextView txtDescription;
public ImageView txtOptionDigit;
ArrayList<RecyclerItem> listitems = new ArrayList<RecyclerItem>();
Context ctx;
public ContactViewHolder(View view, Context ctx, ArrayList<RecyclerItem> listitems) {
super(view);
this.listitems = listitems;
this.ctx = ctx;
view.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxxml);
}
boolean m = false;
#Override
public void onClick(View v) {
clickcount = clickcount + 1;
Log.e("count", String.valueOf(clickcount));
int position = getAdapterPosition();
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle().replace(" absent" , "").replace(" present" , "");
if (signUpModeActive ) {
signUpModeActive = false;
listItems.get(position).setTitle(getname + " " + "absent");
} else {
signUpModeActive = true;
listItems.get(position).setTitle(getname + " " + "present");
}
notifyDataSetChanged();
}
}
public void getsetadapt(String pos) {
list.add(pos);
Log.e("listdata", String.valueOf(list));
for (int i = 0; i < list.size(); i++) {
Log.i("Value " + i, String.valueOf(list.get(i)));
i++;
}
}
}
Actually the problem was in line
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle();
you were setting text as
listItems.get(position).setTitle(getname + " " + "absent");
OR
listItems.get(position).setTitle(getname + " " + "present");
This will result as Name absent or Name present as result and will add a new absent or present to it
There are different problems in your code.
1
First of all, your variables are not named correctly so it's very confusing. For example you use listitems and listItems which are almost exactly the same. Plus listitems is used twice for 2 different variables with the exact same case in the same scope!
A variable should describe its content (you should understand what it is when you read the name, for example a list of students will not be named 'list' but 'students' or 'studentsList') AND you should avoid to have 2 variables with the exact same name in the same scope (if you have to start doing something like this.myvariable = myvariable it means something is wrong in your implementation).
2 Manage your title differently
What you want is the name + a status depending on if present or not.
So when you do that:
listItems.get(position).setTitle("");
you just loose the name as listItems and listitems are the exact same list.
So I'd suggest to have a getName() method on RecyclerItem to return the name. Remove the set/getTitle ones.
Then you manage the present/absent with a boolean.
Basically, like that:
if (signUpModeActive = true) {
signUpModeActive = false;
item.setIsPresent(false);
} else {
signUpModeActive = true;
item.setIsPresent(true);
}
It means you need to add a setIsPresent method to the RecyclerItem.
Then you, in the onBindViewHolder you do:
#Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem item = mListItems.get(position);
String name = item.getName();
String status = item.isPresent() ? "present" : "absent";
String title = name + " " + status;
holder.txtTitle.setText(title);
}
It'd be even better to use a string resource with a placeholder to manage the text of the status.
Recyclerview has 4 items i.e; textView1, textView2, EditText1 and Checkbox1.
Recyclerview has got 24 rows as well. EditText is invisible on initial stage thenit will be visible only when the corresponding checkbox checked by the user. EditText accepts only numbers.
The app is working fine so far this much.
Now I need to get value of all EditTexts and need to display it on another Textview which is not in the part of Recyclerview?
Recyclerview Screenshot- Output Link
Code Samples.
ExamFragment.java
public class ExamFragment extends Fragment {
RecyclerView recyclerView;
ExamFragmentAdapter adapter;
ArrayList<tblChapters> datalistChapters = new ArrayList<>();
TextView txtQcount,txtQCounttotal;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_exam, container, false);
txtQCounttotal=(TextView) v.findViewById(R.id.txtQCounttotal);
txtQcount=(TextView) v.findViewById(R.id.txtQCount);
recyclerView=(RecyclerView)v.findViewById(R.id.recycler_view);
conn = new ConnectionClass(); //connection initialisation
datalistChapters = conn.getChaptersAndCount(modeid, subjectid);
adapter = new ExamFragmentAdapter(datalistChapters, getActivity());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
return v;
}
}
ExamFragmentAdapter.java
public class ExamFragmentAdapter extends RecyclerView.Adapter<ExamFragmentAdapter.MyViewHolder> {
private LayoutInflater inflater;
MyViewHolder holder;
Context mContext;
ArrayList<tblChapters> chapterList=new ArrayList<>();
public ExamFragmentAdapter(ArrayList<tblChapters> chapterList, Context context) {
inflater = LayoutInflater.from(context);
this.chapterList = chapterList;
mContext=context;
}
#Override
public ExamFragmentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.chapter_list_row, parent, false);
holder = new MyViewHolder(view, new MyCustomEditTextListener());
return holder;
}
#Override
public void onBindViewHolder(final ExamFragmentAdapter.MyViewHolder holder, final int position) {
holder.title.setTextColor(Color.BLACK);
holder.slno.setTextColor(Color.BLACK);
holder.noOfQst.setTextColor(Color.BLACK);
holder.noOfQst.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
holder.noOfQst.setGravity(Gravity.CENTER);
holder.title.setText(chapterList.get(position).getTitle()); // Setting Chapter title
holder.slno.setText(String.valueOf(position + 1)+"."); //Setting sl no
holder._myCustomEditTextListener.updatePosition(position);
holder.noOfQst.setText(chapterList.get(position).getNoofQstns()); //Setting no of qstn
if (chapterList.get(position).isVisibled()) {
holder.noOfQst.setVisibility(View.VISIBLE);
} else {
holder.noOfQst.setVisibility(View.INVISIBLE);
}
//in some cases, it will prevent unwanted situations
holder.cbox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.cbox.setChecked(chapterList.get(position).isSelected());
holder.cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set your object's last status
chapterList.get(position).setSelected(isChecked);
chapterList.get(position).setVisibled(isChecked);
//if checkbox checked display EditText(No of qstns), else hide it.
if (holder.cbox.isChecked()) {
holder.noOfQst.setVisibility(View.VISIBLE);
holder.noOfQst.requestFocus();
holder.noOfQst.setText("10");
chapterList.get(position).setNoofQstns(holder.noOfQst.getText().toString());
/* txtQcount.setText("0");
if (txtQcount.getText().toString().equals("")) {
txtQcount.setText("0");
}
txtQcount.setText(Integer.valueOf(txtQcount.getText().toString())+Integer.parseInt(holder.noOfQst.getText().toString()));*/
}
else {
holder.noOfQst.setVisibility(View.INVISIBLE);
holder.noOfQst.setText(""); //remove entered value when uncheck
chapterList.get(position).setNoofQstns("");
}
}
});
}
// we make TextWatcher to be aware of the position it currently works with
// this way, once a new item is attached in onBindViewHolder, it will
// update current position MyCustomEditTextListener, reference to which is kept by ViewHolder
private class MyCustomEditTextListener implements TextWatcher
{
private int position;
private String oldval;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// no op
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
chapterList.get(position).setNoofQstns(charSequence.toString());
int j = i;
j = i2;
j = i3;
}
#Override
public void afterTextChanged(Editable editable) {
}
}
#Override
public int getItemCount() {
return chapterList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
CheckBox cbox;
TextView slno;
EditText noOfQst;
public MyCustomEditTextListener _myCustomEditTextListener;
public MyViewHolder(View itemView,MyCustomEditTextListener myCustomEditTextListener) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.txtTitle);
cbox = (CheckBox) itemView.findViewById(R.id.cboxChapter);
slno = (TextView) itemView.findViewById(R.id.txtRowSlno);
noOfQst = (EditText) itemView.findViewById(R.id.etNoOfQstns);
this._myCustomEditTextListener = myCustomEditTextListener;
try {
if (noOfQst.getVisibility() == View.VISIBLE) {
holder.noOfQst.setVisibility(View.INVISIBLE);
//adding textchange listener to no of qstn(EditText)
noOfQst.addTextChangedListener(myCustomEditTextListener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can register an AdapterDataObserver for the recycler view adapter.
For each item in the adapter when it's updated (user updates the EditText), the adapter can call notifyItemChanged(int position, Object payload). the observer will receive the payload in its onItemRangeChanged (int positionStart, int itemCount, Object payload) callback, you can pass whatever you want in the payload object so that you accumulate the values of all the EditTexts.
You can also unregister the observer in your Activity/Fragment onStop by calling unregisterAdapterDataObserver
I am using a cardview with recyclerview with hardcoded values such as string arrays. But now i want to add new cardview on each button click with user enter values and user select image and keep remain all cardviews on app exit.I mean to say that cardview should be added one by one only.Any solution plz guide me.
cardview Adapter class
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
private static ArrayList<FeddProperties> dataSet;
private static Context context;
public CardViewDataAdapter(Context context, ArrayList<FeddProperties> os_versions) {
this.context = context;
dataSet = os_versions;
}
#Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {
FeddProperties fp = dataSet.get(i);
viewHolder.vehicleNumber.setText(fp.getTitle());
viewHolder.iconView.setImageResource(fp.getThumbnail());
viewHolder.feed = fp;
}
#Override
public int getItemCount() {
return dataSet.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView vehicleNumber;
public ImageView iconView;
public FeddProperties feed;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
vehicleNumber = (TextView) itemLayoutView
.findViewById(R.id.vehiclenumber);
iconView = (ImageView) itemLayoutView
.findViewById(R.id.iconId);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), InformingUser.class);
v.getContext().startActivity(intent);
((MainActivity) context).getSupportFragmentManager().beginTransaction().replace
(R.id.containerView, new InformingUser()).commit();
//Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
}
Java file....
private void initContrls()
{
SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, 0);
vehiclecatory = prefs.getString("vehicle_category", "");
vehicletype = prefs.getString("vehicle_type", "");
String versions = prefs.getString("city", "")+" "+prefs.getString("dis", "")+" "+prefs.getString("number", "");
String vehicleCompany = prefs.getString("company", "")+" "+prefs.getString("model", "");
if(vehiclecatory.equals("1"))
{
if (vehicletype.equals("1"))
{
icons = R.drawable.contwowheel;
}
else if (vehicletype.equals("2"))
{
icons = R.drawable.comfourwheel;
}
else if (vehicletype.equals("3"))
{
icons = R.drawable.comheavy;
}
}
else if (vehiclecatory.equals("2"))
{
if (vehicletype.equals("1"))
{
icons = R.drawable.nontwowheel;
}
else if (vehicletype.equals("2"))
{
icons = R.drawable.nonfourwheel;
}
else if (vehicletype.equals("3"))
{
icons = R.drawable.nonheavy;
}
}
os_versions = new ArrayList<FeddProperties>();
for (int i = 0; i < 2; i++) {
FeddProperties feed = new FeddProperties();
feed.setTitle(versions);
feed.setVehicleCompany(vehicleCompany);
feed.setThumbnail(icons);
os_versions.add(feed);
}
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
mAdapter = new CardViewDataAdapter(context,os_versions);
recyclerView.setAdapter(mAdapter);
}
here there is an example to add cardview dinamically
In this example cardview are create with for:
for (int index = 0; index < 20; index++) {
DataObject obj = new DataObject("Some Primary Text " + index,
"Secondary " + index);
results.add(index, obj);
}
but you can inser into onClick action :)