I can't retrieve my data from database into recycler view. but when i add the data, the data successfully store into database. I cannot identified which part having a problem. Hoping someone can help me to solve this problem.
This is my adapter code.
public class UsersRecyclerAdapter extends RecyclerView.Adapter<UsersRecyclerAdapter.ViewHolder> {
Context context;
ArrayList<UsersItem> usersItemArrayList;
DatabaseReference databaseReference;
public UsersRecyclerAdapter(Context context, ArrayList<UsersItem> usersItemArrayList) {
this.context = context;
this.usersItemArrayList = usersItemArrayList;
databaseReference = FirebaseDatabase.getInstance().getReference();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.activity_user_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
UsersItem USERS = usersItemArrayList.get(position);
holder.textBloodPressure.setText(USERS.getBloodPressure());
holder.textOxygenRate.setText(USERS.getOxygenRate());
holder.textPulseRate.setText(USERS.getPulseRate());
holder.textMedication.setText(USERS.getMedication());
holder.textTodayActivity.setText(USERS.getTodayActivity());
holder.textWaterIntake.setText(USERS.getWaterIntake());
holder.buttonUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewDialogUpdate viewDialogUpdate = new ViewDialogUpdate();
viewDialogUpdate.showDialog(context, USERS.getUserID(), USERS.getBloodPressure(), USERS.getOxygenRate(), USERS.getPulseRate(), USERS.getMedication(), USERS.getTodayActivity(), USERS.getWaterIntake());
}
});
holder.buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewDialogConfirmDelete viewDialogConfirmDelete = new ViewDialogConfirmDelete();
viewDialogConfirmDelete.showDialog(context, USERS.getUserID());
}
});
}
#Override
public int getItemCount() {
return usersItemArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textBloodPressure, textOxygenRate, textPulseRate, textMedication, textTodayActivity, textWaterIntake;
Button buttonDelete;
Button buttonUpdate;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textBloodPressure = itemView.findViewById(R.id.textBloodPressure);
textOxygenRate = itemView.findViewById(R.id.textOxygenRate);
textPulseRate = itemView.findViewById(R.id.textPulseRate);
textMedication = itemView.findViewById(R.id.textMedication);
textTodayActivity = itemView.findViewById(R.id.textTodayActivity);
textWaterIntake = itemView.findViewById(R.id.textWaterIntake);
buttonDelete = itemView.findViewById(R.id.buttonDelete);
buttonUpdate = itemView.findViewById(R.id.buttonUpdate);
}
}
enter image description here
This is add health details code. where data from this page will will store in "USERS" table and retrieve into recycler view.
public class AddJournal extends AppCompatActivity {
DatabaseReference databaseReference;
RecyclerView recyclerView;
ArrayList<UsersItem> usersItemArrayList;
UsersRecyclerAdapter adapter;
Button buttonAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_journal);
//start sini
FirebaseDatabase.getInstance().setPersistenceEnabled(true); // work offline
Objects.requireNonNull(getSupportActionBar()).hide();
databaseReference = FirebaseDatabase.getInstance().getReference();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
usersItemArrayList = new ArrayList<>();
buttonAdd = findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewDialogAdd viewDialogAdd = new ViewDialogAdd();
viewDialogAdd.showDialog(AddJournal.this);
}
});
readData();
}
private void readData() {
databaseReference.child("USERS").orderByChild("bloodPressure").addValueEventListener(new ValueEventListener() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
usersItemArrayList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
UsersItem USERS = dataSnapshot.getValue(UsersItem.class);
usersItemArrayList.add(USERS);
}
adapter = new UsersRecyclerAdapter(AddJournal.this, usersItemArrayList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
public class ViewDialogAdd {
public void showDialog(Context context) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.activity_alert_dialog_add_new_user);
EditText textBloodPressure = dialog.findViewById(R.id.textBloodPressure);
EditText textOxygenRate = dialog.findViewById(R.id.textOxygenRate);
EditText textPulseRate = dialog.findViewById(R.id.textPulseRate);
EditText textMedication = dialog.findViewById(R.id.textMedication );
EditText textTodayActivity = dialog.findViewById(R.id.textTodayActivity);
EditText textWaterIntake = dialog.findViewById(R.id.textWaterIntake);
Button buttonAdd = dialog.findViewById(R.id.buttonAdd);
Button buttonCancel = dialog.findViewById(R.id.buttonCancel);
buttonAdd.setText("ADD");
buttonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String id = "USERS" + new Date().getTime();
String bloodPressure = textBloodPressure.getText().toString();
String oxygenRate = textOxygenRate.getText().toString();
String pulseRate = textPulseRate.getText().toString();
String medication = textMedication.getText().toString();
String todayActivity = textTodayActivity.getText().toString();
String waterIntake = textWaterIntake.getText().toString();
if (bloodPressure.isEmpty() || oxygenRate.isEmpty() || pulseRate.isEmpty() || medication.isEmpty() || todayActivity.isEmpty() || waterIntake.isEmpty()) {
Toast.makeText(context, "Please Enter All data...", Toast.LENGTH_SHORT).show();
} else {
databaseReference.child("USERS").child(id).setValue(new UsersItem(id, bloodPressure, oxygenRate, pulseRate, medication, todayActivity, waterIntake));
Toast.makeText(context, "DONE!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
}
}
Related
I am very new to Android programming. I can't find a solution for my current problem which I've been trying to solve for days.
I want to click on item of populated Array List and get the position i.e. the searched word result in Cardview (neither ItemClickListener nor OnSuggestionListener did work here. Here is my code of MainActivity and I would be very thankful if somebody could help me out:
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init View
recyclerView = (RecyclerView) findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar = (MaterialSearchBar) findViewById(R.id.search_bar);
database = new Database(this);
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
loadSuggestList();
materialSearchBar.addTextChangeListener (new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String> suggest = new ArrayList<>();
for (String search : suggestList) {
if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
#Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener (new MaterialSearchBar.OnSearchActionListener() {
#Override
public void onSearchStateChanged(boolean enabled) {
if (!enabled)
adapter = new SearchAdapter(getBaseContext(), database.getLughats());
recyclerView.setAdapter(adapter);
}
#Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
#Override
public void onButtonClicked(int buttonCode) {
}
});
adapter = new SearchAdapter(this, database.getLughats());
recyclerView.setAdapter(adapter);
}
private void startSearch(String text) {
adapter = new SearchAdapter(this, database.getLughatByWort(text));
recyclerView.setAdapter(adapter);
}
private void loadSuggestList() {
suggestList = database.getWorts();
materialSearchBar.setLastSuggestions(suggestList);
}
}```
Let me know if I should post here also my AdapterCode for the ViewHolder.
Thank you for your help in advance!
Here ist my AdapterCode:
class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RecyclerItemClick itemClickListener;
public TextView wort, praeteritum, partizip2, artikelGrammatik, uebersetzung1, uebersetzung2;
public ImageButton button1, button2;
public SearchViewHolder(#NonNull View itemView) {
super(itemView);
wort = (TextView)itemView.findViewById(R.id.wort);
praeteritum = (TextView)itemView.findViewById(R.id.praeteritum);
partizip2 = (TextView)itemView.findViewById(R.id.partizip2);
artikelGrammatik = (TextView)itemView.findViewById(R.id.artikelGrammatik);
uebersetzung1 = (TextView)itemView.findViewById(R.id.uebersetzung1);
uebersetzung2 = (TextView)itemView.findViewById(R.id.uebersetzung2);
button1 = (ImageButton)itemView.findViewById(R.id.button_id_1);
button2 = (ImageButton) itemView.findViewById(R.id.button_id_2);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
this.itemClickListener.onItemClickListener(v, getLayoutPosition());
}
public void setItemClickListener(RecyclerItemClick ic) {
this.itemClickListener = ic;
}
}
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private Context context;
private List<Lughat> lughats;
public SearchAdapter(Context context, List<Lughat> lughats) {
this.context = context;
this.lughats = lughats;
}
#NonNull
#Override
public SearchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View itemView = inflater.inflate(R.layout.layout_item, parent, false);
return new SearchViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final SearchViewHolder holder, final int position) {
holder.wort.setText(lughats.get(position).getWort());
holder.praeteritum.setText(lughats.get(position).getPraeteritum());
holder.partizip2.setText(lughats.get(position).getPartizip2());
holder.artikelGrammatik.setText(lughats.get(position).getArtikelGrammatik());
holder.uebersetzung1.setText(lughats.get(position).getUebersetzung1());
holder.uebersetzung2.setText(lughats.get(position).getUebersetzung2());
CharSequence praet;
praet = holder.praeteritum.getText();
if (praet.length() == 0) {
holder.praeteritum.setVisibility(View.GONE);
holder.button1.setVisibility(View.GONE);
} else {
holder.praeteritum.setVisibility(View.VISIBLE);
holder.button1.setVisibility(View.VISIBLE);
}
CharSequence part2;
part2 = holder.partizip2.getText();
if (part2.length() == 0) {
holder.partizip2.setVisibility(View.GONE);
} else {
holder.partizip2.setVisibility(View.VISIBLE);
}
CharSequence artGr;
artGr = holder.artikelGrammatik.getText();
if (artGr.length() == 0) {
holder.artikelGrammatik.setVisibility(View.GONE);
holder.button2.setVisibility(View.GONE);
} else {
holder.artikelGrammatik.setVisibility(View.VISIBLE);
holder.button2.setVisibility(View.VISIBLE);
}
CharSequence ueb2;
ueb2 = holder.uebersetzung2.getText();
if (ueb2.length() == 0) {
holder.uebersetzung2.setVisibility(View.GONE);
} else {
holder.uebersetzung2.setVisibility(View.VISIBLE);
}
holder.button1.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.button2.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.setItemClickListener(new RecyclerItemClick() {
#Override
public void onItemClickListener(View v, int position) {
Toast.makeText(context, "Begriff", Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return lughats.size();
}
}
If I do it also with OnClickListener it does not work too, it does not call the method startSearch.
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence text = materialSearchBar.getText();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
startSearch(text.toString());
}
How to get value from one adapter to another adapter when clicking on the favorite button then it shows how many we clicked on favorite image button and also shows all data in another activity. Below is my Adapter Class & ViewHolder class (i.e AdapterFav.java & ContactViewHolder.java)
AdapterFav.java
public class AdapterFav extends
RecyclerView.Adapter<AdapterFav.ContactViewHolder>{
private ArrayList<Contactfav> contactfavList;
private static Context mContext;
public AdapterFav(ArrayList<Contactfav> contactfavList, Context applicationContext) {
this.contactfavList = contactfavList;
this.mContext = applicationContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.contact_row, null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contactfav contactfav = (Contactfav) contactfavList.get(position);
holder.tvContactName.setText(contactfav.getContactName());
holder.tvPhoneNumber.setText(contactfav.getContactNumber());
//Log.e("Id is", contactfav.getContactNumber());
holder.ivContactImage.setImageBitmap(contactfav.getContactImage());
holder.id=contactfav.getCid();
}
#Override
public int getItemCount() {
return contactfavList.size();
}
}
ContactViewHolder.java
public static class ContactViewHolder extends RecyclerView.ViewHolder {
ImageView ivContactImage;
ImageButton ibFavBtn;
TextView tvContactName;
TextView tvPhoneNumber;
Contactfav contactfav=new Contactfav();
String id;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
ibFavBtn = (ImageButton) itemView.findViewById(R.id.fav);
ibFavBtn.setOnClickListener(new View.OnClickListener() {
Boolean b = true;
#Override
public void onClick(View view) {
if (b == true) {
ibFavBtn.setImageResource(R.drawable.star2);
Log.e("From get", id);
b = false;
} else {
ibFavBtn.setImageResource(R.drawable.star);
b = true;
}
}
});
}
}
I am searching a string when i click on search icon its work properly and i get a list.. but when i click on keyboard search icon API not hit its shows Failure every time because it cannot get a parameter.i searched but i cannot get solution and also i want to know that what is actionid . please suggest me. My code is here:-.
public class Medicine_search_price extends AppCompatActivity {
public static RecyclerView Recycle_medicine;
Search_medicine model;
ImageView search_medicine1, Medicine_cart, backarrow;
public static ImageView noproduct;
List<Result> mArray_patient_deatil = new ArrayList<Result>();
public static EditText edit_search_medicine;
Adapter_medicine_view medicine;
public static TextView mTitle;
String user_id, count, search_medicine;
RelativeLayout relativeLayout;
LinearLayout mainlayout;
ProgressBar progressBar;
Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medicine_search_price);
DATAGET();
FINDVIEWBYID();
CLICKLISTINER();
}
private void DATAGET() {
user_id = M.getNumber(getApplicationContext());
count = M.getStatus(getApplicationContext());
}
private void FINDVIEWBYID() {
noproduct = (ImageView) findViewById(R.id.no_product_found);
progressBar = (ProgressBar) findViewById(R.id.proress_bar);
backarrow = (ImageView) findViewById(R.id.medicine_search_price_arrow);
edit_search_medicine = (EditText) findViewById(R.id.search_medicine_txt);
search_medicine1 = (ImageView) findViewById(R.id.search_medicine_b);
Recycle_medicine = (RecyclerView) findViewById(R.id.rcy_medicine_search);
relativeLayout = (RelativeLayout) findViewById(R.id.badge_layout1);
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
mTitle = (TextView) toolbarTop.findViewById(R.id.badge_notification_1);
mainlayout = (LinearLayout) findViewById(R.id.mainLayout);
Medicine_cart = (ImageView) toolbarTop.findViewById(R.id.medicine_search_cart);
}
private void CLICKLISTINER() {
mTitle.setText(count);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Medicine_search_price.this, Preview_screen.class);
startActivity(mIntent);
}
});
search_medicine1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search_medicine = edit_search_medicine.getText().toString();
if (search_medicine.length() <= 2) {
Toast.makeText(Medicine_search_price.this, "Please Enter Atleast Three Characters", Toast.LENGTH_SHORT).show();
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainlayout.getWindowToken(), 0);
getdata(search_medicine);
}
}
});
backarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Medicine_search_price.this, MainActivity.class);
startActivity(mIntent);
}
});
edit_search_medicine.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionid, KeyEvent keyEvent) {
if (actionid== EditorInfo.IME_ACTION_SEARCH) {
getdata(search_medicine);
}
return false;
}
});
}
private void getdata(String search) {
progressBar.setVisibility(View.VISIBLE);
RestAdapter restAdapter1 = new RestAdapter.Builder().setEndpoint(AppConst.MAIN).build();
final UsersAPI searchmedicine = restAdapter1.create(UsersAPI.class);
searchmedicine.pproducts(search, new Callback<FinalSearchMedicine>() {
#Override
public void success(FinalSearchMedicine searchmedicineModel, Response response) {
Integer status = searchmedicineModel.getSuccess();
if (status == 1) {
mArray_patient_deatil = searchmedicineModel.getResult();
Recycle_medicine.setVisibility(View.VISIBLE);
medicine = new Adapter_medicine_view(Medicine_search_price.this, mArray_patient_deatil);
LinearLayoutManager layoutManager1 = new GridLayoutManager(Medicine_search_price.this, 2);
Recycle_medicine.setLayoutManager(layoutManager1);
Recycle_medicine.setAdapter(medicine);
Toast.makeText(Medicine_search_price.this, "" + mArray_patient_deatil.size() + " " + "Products", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(Medicine_search_price.this, "Failureeeee", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
i get my answer so silly mistake. search_medicine is null every time because cannot get string from edit text on action button click..
if (i == EditorInfo.IME_ACTION_SEARCH) {
search_medicne=edit_search_medicine.getText().toString();
getdata(search_medicne);
}
I have a simple fragment which retrieve data from Firebase database.
I use firebase recycler view to display retrieving data. And after scrolling or screen rotation I can't force recycler view (or linear layout manager) restore scroll position.
I found here some answers but they don't work.
My code is:
public class NewsListFragment extends ParentNewsFragment {
static int color_naval, color_black;
private boolean mProcessLikes = false;
private DatabaseReference mDatabaseLikes;
private DatabaseReference mDatabaseViews;
private FirebaseAuth mAuth;
private int position = 0;
public static NewsListFragment getInstance() {
return new NewsListFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDatabaseReference = FirebaseDatabase.getInstance().getReference()
.child("App_news");
mDatabaseLikes = FirebaseDatabase.getInstance().getReference().child("news_likes");
mDatabaseViews = FirebaseDatabase.getInstance().getReference().child("news_views");
mAuth = FirebaseAuth.getInstance();
mQuery = mDatabaseReference.orderByChild("pos").startAt("100");
color_naval = getResources().getColor(R.color.colorPrimary);
color_black = getResources().getColor(R.color.colorBlack);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.rv_choose, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv_choose);
lm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(lm);
rv.setHasFixedSize(true);
return rootView;
}
#Override
public void onPause() {
super.onPause();
position = lm.findFirstVisibleItemPosition();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", position);
}
#Override
public void onViewStateRestored(#Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if(savedInstanceState != null) {
position = savedInstanceState.getInt("position");
}
}
#Override
public void onResume() {
super.onResume();
if (position != 0) {
lm.scrollToPosition(position);
showToast(position+"");
}
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<NewsList, NewsListViewHolder> adapter =
new FirebaseRecyclerAdapter<NewsList, NewsListViewHolder>(
NewsList.class,
R.layout.frag_newslist_card_view,
NewsListViewHolder.class,
mQuery
) {
#Override
protected void populateViewHolder(NewsListViewHolder viewHolder, final NewsList model, int position) {
final String post_key = getRef(position).getKey();
viewHolder.setDate(model.getDate()+",");
viewHolder.setTime(model.getTime());
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getContext(), model.getImage());
viewHolder.setEye(model.getCode());
viewHolder.setThumb(post_key);
viewHolder.thumb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mProcessLikes = true;
mDatabaseLikes.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLikes){
if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){
mDatabaseLikes.child(post_key).child(mAuth.getCurrentUser().getUid())
.removeValue();
mProcessLikes = false;
}
else {
mDatabaseLikes.child(post_key).child(mAuth.getCurrentUser().getUid())
.setValue("like");
mProcessLikes = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDatabaseViews.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())) {
mDatabaseViews.child(post_key).child(mAuth.getCurrentUser().getUid())
.setValue("view");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mChooserListener.chooseNews(model.getCode());
}
});
}
};
rv.setAdapter(adapter);
}
public static class NewsListViewHolder extends RecyclerView.ViewHolder {
//some text here
}
}
method showToast in the end shows real number of position but recyclerview starts from the beginning.
any ideas?
Make sure you are not reloading data from the server or wherever you are retrieving data.
Add is not null check in your fragment's onCreateView or activity's onCreate like:
if(savedInstanceState != null){
...
}else{
loadData();
}
Replace your linear layout manager with something similar to the following. I have used this implementation many times when using RecyclerView in fragments. Let me know how it goes
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
yourItem.setLayoutManager(mLayoutManager);
Please i need help on this one. Have been working on it for days but cant figure it out.
I have a recycleView to display a list of names but its displaying empty list, but i can print the list in console.
here,s the code fetching the list
public List<BackendlessUser> userList = new ArrayList<BackendlessUser>();
BackendlessDataQuery query = new BackendlessDataQuery();
Backendless.Data.of( BackendlessUser.class ).find( query, new AsyncCallback<BackendlessCollection<BackendlessUser>>()
{
#Override
public void handleResponse( BackendlessCollection<BackendlessUser> response )
{
userList = response.getCurrentPage();
for( BackendlessUser user : userList ){
Log.d( "USERS : ", user.getEmail() );
}
Log.d("USER LIST", String.valueOf( userList.size() ));
}
#Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
and the recyclerView adapter that is supposed to adapt the list to the view ..
private void setupRecyclerView(#NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(userList));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<BackendlessUser> mValues;
public SimpleItemRecyclerViewAdapter(List<BackendlessUser> items) {
mValues = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).getProperty("idno").toString());
holder.mContentView.setText(mValues.get(position).getProperty("fname").toString());
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.getProperty("idno").toString());
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.getProperty("idno").toString());
context.startActivity(intent);
}
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public BackendlessUser mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
}
#Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}