Recycler updates with duplicate values - android

I use wipe to delete method to delete a row, When i delete one row from the recycler list view it duplicates the value just below the one I had deleted.
If I delete "Test 3" from the list
it shows a duplicate of "Test 4" in 3rd and 4th position
my whole code used for list creation and deletion, can any one help me with this
public class fragmnetPendingAct extends Fragment {
private SwipeRefreshLayout refreshLayout;
private RecyclerView recyclerView;
ArrayList<String> activitynames;
FirebaseAuth mAuth;
View view;
List<modelclasspendingact> modelclasspendingactList = new ArrayList<>();
public fragmnetPendingAct() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pendingactivity,container,false);
refreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_activitypend);
recyclerView = v.findViewById(R.id.recyclerviewpendingact);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
new ItemTouchHelper(itemtouchHelpercalback).attachToRecyclerView(recyclerView);
//listen to swipe horizontal
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Get new Instance ID token
FirebaseUser mUser = mAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(#NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
// Send token to your backend via HTTPS
getdatafromserver(token);
} else {
// Handle error -> task.getException();
Log.w(TAG, "getInstanceId failed second", task.getException());
}
}
});
refreshitems();
}
});
// Get new Instance ID token
FirebaseUser mUser = mAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(#NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
Log.d(TAG, "onComplete lead: "+token);
// Send token to your backend via HTTPS
getdatafromserver(token);
} else {
// Handle error -> task.getException();
Log.w(TAG, "getInstanceId failed second", task.getException());
}
}
});
return v;
}
public void getdatafromserver(String token){
//new code using curl
String serverurl="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, serverurl,null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, "activity list: "+response);
JSONObject alldata=response.getJSONObject("payload");
JSONArray assetaarray=alldata.getJSONArray("activities");
modelclasspendingactList.removeAll(modelclasspendingactList);
for(int i=0;i<assetaarray.length();i++)
{
JSONObject getactivity=assetaarray.getJSONObject(i);
String activityid = String.valueOf(getactivity.getInt("id"));
String activityname = getactivity.getString("title");
String activitydesc = getactivity.getString("description");
Log.d(TAG, "activity name: "+activityname);
//list crreation
modelclasspendingactList.add(new modelclasspendingact(activityname,activitydesc,activityid));
pendingactAdapter adapter = new pendingactAdapter(modelclasspendingactList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers=new HashMap<String,String>();
headers.put("Accept","*/*");
headers.put("Cache-Control","no-cache");
headers.put("cache-control","no-cache");
headers.put("token",token);
return headers;
}
};
singletonclasshttp.getInstance(getActivity()).addtorequestque(jsonObjectRequest);
}
private void refreshitems()
{
refreshLayout.setRefreshing(false);
}
//swipe function
ItemTouchHelper.SimpleCallback itemtouchHelpercalback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
Log.d(TAG, "onSwiped: "+modelclasspendingactList.get(viewHolder.getAdapterPosition()).getPendid());
Log.d(TAG, "onSwiped: position "+viewHolder.getAdapterPosition());
String activeid = modelclasspendingactList.get(viewHolder.getAdapterPosition()).getPendid();
// Get new Instance ID token
FirebaseUser mUser = mAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(#NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
//send token and activity id to update function
markascompleted(activeid,token);
} else {
// Handle error -> task.getException();
Log.w(TAG, "getInstanceId failed second", task.getException());
}
}
});
Log.d(TAG, "onSwiped: position "+viewHolder.getAdapterPosition());
Log.d(TAG, "onSwiped: first list size "+modelclasspendingactList.size());
pendingactAdapter adapter = new pendingactAdapter(modelclasspendingactList);
modelclasspendingactList.remove(viewHolder.getAdapterPosition());
recyclerView.removeViewAt(viewHolder.getAdapterPosition());
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
// adapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), modelclasspendingactList.size());
// adapter.notifyDataSetChanged();
Log.d(TAG, "onSwiped: list size "+modelclasspendingactList.size());
// pendingactAdapter adapter = new pendingactAdapter(modelclasspendingactList);
// modelclasspendingactList.remove(viewHolder.getAdapterPosition());
// adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
};
private void markascompleted(String activeid,String token)
{
//send data to server
//curl function
String serverurl="xxxxxxxxxxxxxxxxxxxxxxx";
Map<String, Object> jsonParams = new ArrayMap<>();
jsonParams.put("id", activeid);
jsonParams.put("activityStatus", "COMPLETED");
jsonParams.put("targetEmployeeId", "1001");
jsonParams.put("score", "1");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, serverurl,new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, "response: "+response);
try {
if (response.optBoolean("success")==true)
{
Toast.makeText(getActivity(), response.getString("message"),Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getActivity(),"Data Not Updated",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getActivity(),"Data Not Updated",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),"Data Not Saved",Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}
){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers=new HashMap<String,String>();
headers.put("Accept","*/*");
headers.put("Cache-Control","no-cache");
headers.put("Connection","keep-alive");
headers.put("Content-Type","application/json");
headers.put("cache-control","no-cache");
headers.put("token",token);
return headers;
}
};
singletonclasshttp.getInstance(getActivity()).addtorequestque(jsonObjectRequest);
}
}

First I'll explain why you get this behavior, then we'll get to solution.
In this code:
Problem #1:
pendingactAdapter adapter = new pendingactAdapter(modelclasspendingactList);
You are creating an adapter and pass it your current data. Note that the adapter will have its own copy of the list you passed in constructor.
modelclasspendingactList.remove(viewHolder.getAdapterPosition());
This list that you are removing Item from is different from the one you just sent in Adapter, so the Adapter still has old list.
Then you call removed and adapter tries to update values but your removed item is still in the list that adapter has.
Solution:
Create A Function/Method in Adapter that removes the item from the list in adapter.
Problem #2 with Solution:
You are creating a new Adapter instance/object every time a view is swiped, it is unnecessary. Create the Adapter in class instead of function and instantiate it in onCreateView().

A couple things.
First, the removeViewAt is a function of RecyclerView.LayoutManager. I imagine you meant to say recyclerView.LayoutManager.removeViewAt.
Second, the call to removeViewAt should not be necessary as RecyclerView does this automatically when notifyItemRemoved is called correctly.
Third, you do not need to re-bind the entire list when removing an item. You can remove the notifyItemRangeChanged.
Fourth, you do not need to invalidate the entire adapter when removing an item either. You can remove notifyDataSetChanged. In fact, this function should only be used as a last resort.
Initializing your RecyclerView:
pendingactAdapter adapter = new pendingactAdapter(modelclasspendingactList);
RecyclerView recyclerView = view.findViewById(R.id.my_recycler_view);
recyclerView.adapter = adapter;
recyclerView.layoutManager = LinearLayoutManager(getActivity());
So your removal sequence should be
modelclasspendingactList.remove(viewHolder.getAdapterPosition());
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());

Related

Retrieve images in recyclerview from Firebase Firestore

I am trying to retrieve images from Firebase Firestore. I am able to retrieve text in RecyclerView successfully however I'm not sure how to retrieve the images.
I had a look at similar questions unfortunately none helped.
ListActivity :
//initialize firestore
db = FirebaseFirestore.getInstance();
//initialize views
mRecyclerView = findViewById(R.id.resultRecycle);
//set recycler view properties
mRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
//show data in recycler view
showData();
}
private void showData() {
db.collection("Item")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
//called when data is retrieved
//show data
for(DocumentSnapshot doc: task.getResult()){
Model model = new
Model(doc.getString("id"),
doc.getString("Title"),
doc.getString("Location"),
//STUCK HERE
);
modelList.add(model);
}
//adapter
adapter = new CustomAdapter(ListActivity.this, modelList);
//set adapter to recycler view
mRecyclerView.setAdapter(adapter);
}
});
CustomAdapter :
public void onItemClick(View view, int position) {
//this will be called when user clicks an item
//show data on toast when clicking
String title = modelList.get(position).getTitle();
String location = modelList.get(position).getLocation();
String url = modelList.get(position).getUrl();
}
#Override
public void onItemLongClick(View view, int position) {
//this will be called when user clicks long item
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int i) {
//bind views /set data
holder.mTitle.setText(modelList.get(i).getTitle());
holder.mLocation.setText(modelList.get(i).getLocation());
Picasso.get().load(modelList.get(i).getUrl()).into(holder.mUrl);
}
Please see the image below link, thank you
Use fileuploader code to upload the image in Firebase Database.
But make sure you put the Fileuploader run on the click of any button. Or it will throw nullpointer Exception if your edittext are empty.
private void Fileuploader(){
if(imguri != null ) {
StorageReference ref = mStorageRef.child("Item");
Toast.makeText(UploadActivity.this, "Upload in progress", Toast.LENGTH_LONG).show();
ref.putFile(imguri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(UploadActivity.this, "Image uploaded successfully!", Toast.LENGTH_LONG).show();
Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful());
Uri downloadUrl = urlTask.getResult();
saveData(downloadUrl);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
The function will automatically start when file upload successfully done.
private void saveData(Uri imguri) {
Map<String, String> data = new HashMap<>();
EditText title = findViewById(R.id.etv_1);
EditText shortDesc = findViewById(R.id.etv_2);
TextView location = findViewById(R.id.etv_3);
String eTitle = title.getText().toString();
String eShortDesc = shortDesc.getText().toString();
String eLocation = location.getText().toString();
String eUrl = imguri.toString();
userid = FirebaseAuth.getInstance().getUid();
getCate();
data.put("Img", eUrl);
data.put("Title", eTitle);
data.put("Location", eLocation);
data.put("Short Description", eShortDesc);
data.put("Category", cate );
data.put("id", userid);
db.collection("Item").add(data);
Toast.makeText(this, "Successfully saved your item", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, Home.class);
startActivity(i);
}
I have an best example of Retrieve images in recyclerview from Firebase Firestore ..link is here ...
https://www.simplifiedcoding.net/firebase-storage-example/
in this website given best hint and tutorial here.. please do follow step by step ...
Hey I think this may hep using Picasso
Picasso.with(context).load(ImageURL).into(imageView);

How to retrieve a part of the data from Firebase Android

Contacts Fragment
ListView usersList;
TextView noUsersText;
ArrayList<String> al = new ArrayList<>();
int totalUsers = 0;
ProgressDialog mProgressDialogue;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
usersList = (ListView)rootView.findViewById(R.id.usersList);
noUsersText = (TextView)rootView.findViewById(R.id.noUsersText);
mProgressDialogue = new ProgressDialog(getActivity());
mProgressDialogue.setMessage("Loading...");
mProgressDialogue.show();
String url = "https://messageplus-cd647.firebaseio.com/Users.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
#Override
public void onResponse(String s) {
doOnSuccess(s);
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError);
}
});
RequestQueue rQueue = Volley.newRequestQueue(getActivity());
rQueue.add(request);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UserDetails.chatWith = al.get(position);
Intent intent = new Intent(getActivity(), Chat.class);
startActivity(intent);
}
});
return rootView;
}
doOnSuccess Method
public void doOnSuccess(String s){
try {
JSONObject obj = new JSONObject(s);
Iterator i = obj.keys();
String key = "";
while(i.hasNext()){
key = i.next().toString();
if(!key.equals(UserDetails.phonenumber)) {
al.add(key);
}
totalUsers++;
}
} catch (JSONException e) {
e.printStackTrace();
}
if(totalUsers <=1){
noUsersText.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
else{
noUsersText.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1 , al));
}
mProgressDialogue.dismiss();
}
}
My Database strucuture - http://ibb.co/eMGhWb
I want to display only the names of the users. How do i achieve this.
with the current code, when i open the fragment It just says "Loading..." and doesnt load anything... do i have to do anything with exporting json file or anything? please help i'm still a beginner
In order to query a Firebase database you only need to create a DatabaseReference, attach a listener and iterate over the DataSnapshot object like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> arrayList = new ArrayList<>();
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("Name").getValue(String.class);
arrayList.add(name);
}
listView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
The output will be only tne name of the users.

RecyclerView does not show anything immediately

I'm trying to fix a problem related to RecyclerView. When I try to show the list of objects for the first time I don't see anything. The data are passed thanks to a volley request that is known to be async. So, I've tried various solutions like onDataSetChanged() but nothing happens. I want to remark that the solution works, the second time I watch the list I can see it without any problems.
Here's my code:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private static MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
adapter = new MyAdapter(this,initData());
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private List<ParentObject> initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}
return parentObject;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}
private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();
// Tag used to cancel the request
String tag_string_req = "request";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
}
Log.d("TRY", "Response: " + response.toString());
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
return childList;
}
Thank you for your help!
well there are couple of points
get list method returning empty list after made volley request.
make sure list filled data from the server then call the set data.
you have to try something like below.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private static MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
// show some loading bar
initData();
}
private void initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}
}
private void setData(List<ParentObject> parentObject) {
// load the data here
adapter = new MyAdapter(this,parentObject);
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}
private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();
// Tag used to cancel the request
String tag_string_req = "request";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
setData(childList);
}
Log.d("TRY", "Response: " + response.toString());
} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
return childList;
}

Firebase recyclerViewAdpter not working properly

In my blog App in post detail Activity i added comments list with simple RecyclerView Adapter that was working fine but i am not able to add delete button for comment so moved to FireBase RecyclerView now i have lots of problems
it not updating in real time
when i comment that not pop out above the edit text layout when i close edit text box then it show my comments
when i write a comment and hit comment button it not show comment but when i writing the second comment on same time after writing three or four line the previous comment pop out and the edit text bot hide under the keyboard why this happening
Activity Code:
mCommentsRecycler.setLayoutManager(mlinearLayoutManager);
}
#Override
public void onStart() {
super.onStart();
// Add value event listener to the post
// [START post_value_event_listener]
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// [START_EXCLUDE]
// mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
final String formImage = String.valueOf(post.postDetailPic);
// [END_EXCLUDE]
if (formImage.length() > 0) {
Picasso.with(getApplicationContext()).load(formImage)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(postPic, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again online if cache failed
Picasso.with(getApplicationContext())
.load(formImage)
//.error(R.drawable.ic_warning_black_24dp)
.into(postPic, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.v("Picasso", "Could not fetch image");
}
});
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.",
Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mPostListener = postListener;
// Listen for comments
mAdapter = new CommentAdepter(Comment.class, CommentViewHolder.class, R.layout.item_comment, mCommentsReference, this);
mCommentsRecycler.setAdapter(mAdapter);
}
#Override
public void onStop() {
super.onStop();
// Remove post value event listener
if (mPostListener != null) {
mPostReference.removeEventListener(mPostListener);
}
// Clean up comments listener
mAdapter.cleanup();
}
#Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.button_post_comment) {
postComment();
}
}
private void postComment() {
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
assert mUser != null;
final String uid = mUser.getUid();
final DatabaseReference commentDb = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);
commentDb.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
User user = dataSnapshot.getValue(User.class);
String authorName = user.name;
String commentUserPic = user.ProfilePic;
// Create new comment object
String commentText = mCommentField.getText().toString();
HashMap < String, Object > dateCreated; {
//Otherwise make a new object set to ServerValue.TIMESTAMP
dateCreated = new HashMap < String, Object > ();
dateCreated.put("timeStamp", ServerValue.TIMESTAMP);
}
HashMap < String, Boolean > commenter_id; {
commenter_id = new HashMap < String, Boolean > ();
commenter_id.put(getUid(), true);
}
Comment comment = new Comment(uid, authorName, commentText, commentUserPic, dateCreated, commenter_id);
// Push the comment, it will appear in the list
mCommentsReference.push().setValue(comment);
// Clear the field
mCommentField.setText(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
private static class CommentViewHolder extends RecyclerView.ViewHolder {
public CommentViewHolder(View itemView) {
super(itemView);
authorView = (TextView) itemView.findViewById(R.id.comment_author);
bodyView = (TextView) itemView.findViewById(R.id.comment_body);
postingTime = (TextView) itemView.findViewById(R.id.comment_post_time);
rm_comment = (Button) itemView.findViewById(R.id.remove_comment);
}
public void setImage(String images) {
this.imageDp = images;
userPic = (ImageView) itemView.findViewById(R.id.comment_photo);
Picasso.with(context).load(imageDp).into(userPic);
}
void bindToPost(Comment comment, View.OnClickListener starClickListener) {
authorView.setText(comment.author);
bodyView.setText(comment.text);
setImage(comment.commentPic);
setViewValue(comment.getTimestampCreatedLong());
rm_comment.setOnClickListener(starClickListener);
}
}
private class CommentAdepter extends FirebaseRecyclerAdapter < Comment, CommentViewHolder > {
private Context mContext;
private List < String > mCommentIds = new ArrayList < > ();
private List < Comment > mComments = new ArrayList < > ();
private String mkey;
CommentAdepter(Class < Comment > CommentClass, Class < CommentViewHolder > viewHolderClass, int modelLayout, DatabaseReference ref, final Context context) {
super(CommentClass, modelLayout, viewHolderClass, mCommentsReference);
mContext = context;
// Create child event listener
// [START child_event_listener_recycler]
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// A new comment has been added, add it to the displayed list
Comment comment = dataSnapshot.getValue(Comment.class);
// mkey = dataSnapshot.getKey();
// [START_EXCLUDE]
// Update RecyclerView
mCommentIds.add(dataSnapshot.getKey());
mComments.add(comment);
notifyItemInserted(mComments.size() - 1);
// [END_EXCLUDE]
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so displayed the changed comment.
Comment newComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Replace with the new data
mComments.set(commentIndex, newComment);
// Update the RecyclerView
notifyItemChanged(commentIndex);
} else {
Log.w(TAG, "onChildChanged:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so remove it.
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Remove data from the list
mCommentIds.remove(commentIndex);
mComments.remove(commentIndex);
// Update the RecyclerView
notifyItemRemoved(commentIndex);
} else {
Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
// A comment has changed position, use the key to determine if we are
// displaying this comment and if so move it.
Comment movedComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("PostDetailActivity", "postComments:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load comments.",
Toast.LENGTH_SHORT).show();
}
};
ref.addChildEventListener(childEventListener);
// [END child_event_listener_recycler]
// Store reference to listener so it can be removed on app stop
}
#Override
public int getItemCount() {
return mComments.size();
}
#Override
protected void populateViewHolder(final CommentViewHolder viewHolder, final Comment model, final int position) {
final DatabaseReference commentRef = getRef(position);
final String commentKey = commentRef.getKey();
if (model.commenter_id.containsKey(gUid())) {
viewHolder.rm_comment.setVisibility(View.VISIBLE);
}
viewHolder.bindToPost(model, new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "clicked " + commentKey, Toast.LENGTH_SHORT).show();
mCommentsReference.child(commentKey).removeValue();
}
});
}
}
private String gUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
}
if u need layout file please comment....
I added ability to delete an item in a FirebaseRecyclerAdapter and generally followed the advice in this post:
how to implement a SetOnItemClickListener FirebaseRecyclerViewAdapter
I used the first solution, adding an ImageView.setOnClickListener() call within populateViewHolder(). Its working fine for me.
I'm not quite sure what the #3 problem is above but I was having a problem whereby the data didn't get populated the first time it was called. I found my solution in this post, namely ensuring that the RecyclerView height was set to match_parent:
FirebaseRecyclerAdapter - populateViewHolder is not populating the data for first time it runs?

how to impliment delete and edit button in recyclerView using firebase

i am making a simple blog app using firebase as backend. in app i am showing comments list in post detail activity i want to implement edit and delete button in the front of every comment and want to visible it only if that comment write by the current user
this is my postDetailActivity
where should i need to implement the delete button
plz... help me if u need any other info comment....
updated to FirbaseRecyclerView but now delete button not working
public class PostDetailActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "PostDetailActivity";
public static final String EXTRA_POST_KEY = "post_key";
private DatabaseReference mPostReference;
private DatabaseReference mCommentsReference;
private ValueEventListener mPostListener;
protected String mPostKey;
private FirebaseRecyclerAdapter<Comment, CommentViewHolder> mAdapter;
private ImageView postPic;
private TextView mTitleView;
private TextView mBodyView;
private EditText mCommentField;
private Button mCommentButton;
public Button del_comment;
private RecyclerView mCommentsRecycler;
private LinearLayoutManager mlinearLayoutManager;
private String commentKey;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
// Get post key from intent
mPostKey = getIntent().getStringExtra(EXTRA_POST_KEY);
if (mPostKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_POST_KEY");
}
// Initialize Database
mPostReference = FirebaseDatabase.getInstance().getReference()
.child("posts").child(mPostKey);
mCommentsReference = FirebaseDatabase.getInstance().getReference()
.child("post-comments");
// Initialize Views
// mAuthorView = (TextView) findViewById(R.id.post_author);
mTitleView = (TextView) findViewById(R.id.post_title);
mBodyView = (TextView) findViewById(R.id.post_body);
postPic = (ImageView) findViewById(R.id.form_details_image);
mCommentField = (EditText) findViewById(R.id.field_comment_text);
mCommentButton = (Button) findViewById(R.id.button_post_comment);
del_comment = (Button) findViewById(R.id.remove_comment);
mCommentsRecycler = (RecyclerView) findViewById(R.id.recycler_comments);
mCommentButton.setOnClickListener(this);
mCommentsRecycler.setHasFixedSize(true);
mlinearLayoutManager = new LinearLayoutManager(this);
mCommentsRecycler.setLayoutManager(mlinearLayoutManager);
}
private Query getcommentQuery(DatabaseReference mCommentsReference) {
return mCommentsReference.child(mPostKey);
}
#Override
public void onStart() {
super.onStart();
// Add value event listener to the post
// [START post_value_event_listener]
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// [START_EXCLUDE]
// mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
final String formImage = String.valueOf(post.postDetailPic);
// [END_EXCLUDE]
if (formImage.length() > 0) {
Picasso.with(getApplicationContext()).load(formImage)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(postPic, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again online if cache failed
Picasso.with(getApplicationContext())
.load(formImage)
//.error(R.drawable.ic_warning_black_24dp)
.into(postPic, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.v("Picasso","Could not fetch image");
}
});
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.",
Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mPostListener = postListener;
// Listen for comments
Query commentQuery = getcommentQuery(mCommentsReference);
mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class,R.layout.item_comment,
CommentViewHolder.class, commentQuery) {
#Override
protected void populateViewHolder(final CommentViewHolder viewHolder,final Comment model,final int position) {
final DatabaseReference commentRef = getRef(position);
commentKey = commentRef.getKey();
if (model.commenter_id.containsKey(gUid())){
viewHolder.rm_comment.setVisibility(View.VISIBLE);
}
viewHolder.bindToPost(model, new View.OnClickListener() {
#Override
public void onClick(View view) {
mCommentsReference.child(mPostKey).child(commentKey).removeValue();
}
});
}
};
mCommentsRecycler.setAdapter(mAdapter);
}
#Override
public void onStop() {
super.onStop();
// Remove post value event listener
if (mPostListener != null) {
mPostReference.removeEventListener(mPostListener);
}
// Clean up comments listener
mAdapter.cleanup();
}
#Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.button_post_comment) {
postComment();
}
}
private void postComment() {
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
assert mUser != null;
final String uid = mUser.getUid();
final DatabaseReference commentDb = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);
commentDb.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
User user = dataSnapshot.getValue(User.class);
String authorName = user.name;
String commentUserPic = user.ProfilePic;
// Create new comment object
String commentText = mCommentField.getText().toString();
HashMap<String, Object> dateCreated;
{
//Otherwise make a new object set to ServerValue.TIMESTAMP
dateCreated = new HashMap<String, Object>();
dateCreated.put("timeStamp", ServerValue.TIMESTAMP);
}
HashMap<String,Boolean> commenter_id ;
{
commenter_id = new HashMap<String, Boolean>();
commenter_id.put(getUid(),true);
}
Comment comment = new Comment(uid, authorName, commentText, commentUserPic, dateCreated, commenter_id);
// Push the comment, it will appear in the list
mCommentsReference.push().setValue(comment);
// Clear the field
mCommentField.setText(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
private Context mContext;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private List<String> mCommentIds = new ArrayList<>();
private List<Comment> mComments = new ArrayList<>();
private String mkey;
// mContext = context;
// mDatabaseReference = ref;
// Create child event listener
// [START child_event_listener_recycler]
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
taskDeletion(dataSnapshot);
// A new comment has been added, add it to the displayed list
Comment comment = dataSnapshot.getValue(Comment.class);
mkey = dataSnapshot.getKey();
// [START_EXCLUDE]
// Update RecyclerView
mCommentIds.add(dataSnapshot.getKey());
mComments.add(comment);
mAdapter.notifyItemInserted(mComments.size() - 1);
// [END_EXCLUDE]
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so displayed the changed comment.
Comment newComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Replace with the new data
mComments.set(commentIndex, newComment);
// Update the RecyclerView
mAdapter.notifyItemChanged(commentIndex);
} else {
Log.w(TAG, "onChildChanged:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so remove it.
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Remove data from the list
mCommentIds.remove(commentIndex);
mComments.remove(commentIndex);
// Update the RecyclerView
mAdapter.notifyItemRemoved(commentIndex);
} else {
Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
// A comment has changed position, use the key to determine if we are
// displaying this comment and if so move it.
Comment movedComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("PostDetailActivity", "postComments:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load comments.",
Toast.LENGTH_SHORT).show();
}
};
// ref.addChildEventListener(childEventListener);
// [END child_event_listener_recycler]
// Store reference to listener so it can be removed on app stop
// mChildEventListener = childEventListener;
private String gUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
}

Categories

Resources