I am trying to get a TextView value item within the RecyclerView. My RecyclerView layout file has two TextViews, one is Product Name and the other for the Quantity as you can see here
=== This is my cart_items_layout_item ===
<TextView
android:id="#+id/cart_list_product_name"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Product Name"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/cart_list_product_quantity"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Product Quantity"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:gravity="end"
android:textStyle="bold"/>
This is my activity_cart xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cart_list"
android:layout_width="match_parent"
android:layout_height="542dp"
android:layout_above="#+id/next"
android:layout_below="#id/header_color"
android:layout_marginBottom="113dp">
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="#+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/button"
android:text="CONFIRM ORDER/S"
android:textColor="#color/colorPrimaryDark"
android:layout_margin="10dp" />
<Button
android:id="#+id/sendSMSButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="69dp"
android:background="#drawable/button"
android:text="Send"
android:textColor="#color/colorPrimaryDark" />
<TextView
android:id="#+id/smsphoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cart_list"
android:text="0918"/>
This is my RecyclerView Adapter Class
#Override
protected void onStart() {
super.onStart();
final DatabaseReference cartListRef =
FirebaseDatabase.getInstance().getReference().child("Cart List");
FirebaseRecyclerOptions<Cart> options =
new FirebaseRecyclerOptions.Builder<Cart>()
.setQuery(cartListRef.child("User View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products"), Cart.class)
.build();
FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter =
new FirebaseRecyclerAdapter<Cart, CartViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull CartViewHolder
holder, int i, #NonNull final Cart model) {
holder.txtProductName.setText(model.getProductName());
holder.txtProductQuantity.setText("Quantity = " +
model.getQuantity());
holder.itemView.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View view)
{
CharSequence options[] = new CharSequence[]
{
"Edit",
"Removed"
};
AlertDialog.Builder builder = new
AlertDialog.Builder(CartActivity.this);
builder.setTitle("Cart Options");
builder.setItems(options, new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface
dialogInterface, int i)
{
if (i == 0)
{
Intent intent = new
Intent(CartActivity.this, ProductDetailsActivity.class);
intent.putExtra("pid",model.getPid());
startActivity(intent);
}
if(i == 1)
{
cartListRef.child("User View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products")
.child(model.getPid())
.removeValue()
.addOnCompleteListener(new
OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull
Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CartActivity.this, "Item removed successfully",
Toast.LENGTH_SHORT).show();
// Intent intent = new
Intent(CartActivity.this, Home.class);
//
startActivity(intent);
}
}
});
}
if(i == 1)
{
cartListRef.child("Admin View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products")
.child(model.getPid())
.removeValue()
.addOnCompleteListener(new
OnCompleteListener<Void>() {
#Override
public void
onComplete(#NonNull Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CartActivity.this, "Item removed successfully",
Toast.LENGTH_SHORT).show();
// Intent intent =
new Intent(CartActivity.this, Home.class);
//
startActivity(intent);
}
}
});
}
}
});
builder.show();
}
});
}
#NonNull
#Override
public CartViewHolder onCreateViewHolder(#NonNull ViewGroup
parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout,
parent, false);
CartViewHolder holder = new CartViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
=== this is my Cart class/Model ===
public class Cart
{
private String
date,discount,pid,productDesc,productName,productSupplier,quantity,time;
public Cart()
{
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductSupplier() {
return productSupplier;
}
public void setProductSupplier(String productSupplier) {
this.productSupplier = productSupplier;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Cart(String date, String discount, String pid, String productDesc,
String productName, String productSupplier, String quantity, String time)
{
this.date = date;
this.discount = discount;
this.pid = pid;
this.productDesc = productDesc;
this.productName = productName;
this.productSupplier = productSupplier;
this.quantity = quantity;
this.time = time;
}
=== CartViewHolder ===
public class CartViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
{
public TextView txtProductName, txtProductQuantity;
private ItemClickListener itemClickListener;
public CartViewHolder(#NonNull View itemView) {
super(itemView);
txtProductName = itemView.findViewById(R.id.cart_list_product_name);
txtProductQuantity =
itemView.findViewById(R.id.cart_list_product_quantity);
}
#Override
public void onClick(View view)
{
itemClickListener.onClick(view, getAdapterPosition(), false);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
As you can see in this Image, i use the cart class/model to pass the data to them, then i created another class called CartViewHolder i use this class to set the data into Recycler View. i want to get this values and put it in the message particularly in the SMS. i dont know how to pass it i tried several codes for it.
private void sendSmsBySIntent ()
{
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
//Want to Intent the Items in Here the Product Name and the Quantity
smsSIntent.putExtra("PUT THE ITEMS HERE");
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
you can use your List<Cart> cart model list item to send product and quantity to
sms.
private void sendSmsBySIntent (Cart model)
{
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsSIntent.putExtra("pName",model.getProductName());
smsSIntent.putExtra("pQty",model.getQuantity());
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
You need the list you sent to the adapter. Send it as a parameter to the sendSmsBySIntent() function and make a for loop where you build a String.
ie.:
private void sendSmsBySIntent (List<Cart> yourList){
StringBuilder extra = new StringBuilder();
for (int i = 0; i < yourList.size(); i++) {
//Build the string the way you want
extra.append(yourList.get(i).getName()).append(" ").append(yourList.get(i).getQuantity);
}
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
//Want to Intent the Items in Here the Product Name and the Quantity
smsSIntent.putExtra(extra.toString());
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
Edit:
I changed the function inside () , check it.
Now, when you call the function you have to pass the list you are working with. Like so: sendSmsBySIntent(placeHereYourCartList)
Related
I am new to this, I am trying to load images from Firestore into a Grid layout recyclerview, the Toast in Firestore onSuccess method shows "Success" message, but the images aren't showing, I am not sure where I did wrong. I used the Image Url saved in Firestore from Firebase Storage.
Could someone please help? Thank you in advance.
Firestore structure:
The PhotoActivity:
public class PhotoActivity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private LinearLayout confirmLayout;
private ImageView pickedImageView;
private EditText titleEditText, photoDescriptionEditText;
private ProgressBar progressBar;
private String title, photoDescription;
private Date datePhoto;
private Uri pickedImageUrl;
private FirebaseFirestore db;
private FirebaseAuth mAuth;
private String userId;
private CollectionReference collectionReference;
private StorageReference storageReference;
private FloatingActionButton fab;
private RecyclerView recyclerView;
private List<Photo> photoList;
private PhotoAdapter photoAdapter;
private ProgressBar circularProgressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
confirmLayout = (LinearLayout) findViewById(R.id.confirmPhotoUploadLayout);
pickedImageView = (ImageView) findViewById(R.id.pickedImage);
titleEditText = (EditText) findViewById(R.id.photoTitle);
photoDescriptionEditText = (EditText) findViewById(R.id.photoDescription);
progressBar = (ProgressBar) findViewById(R.id.progressbarPhoto);
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewPhoto);
circularProgressbar = (ProgressBar) findViewById(R.id.progress_circularPhoto);
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
userId = mAuth.getCurrentUser().getUid();
storageReference = FirebaseStorage.getInstance().getReference(userId);
collectionReference = FirebaseFirestore.getInstance().collection("main").document(userId).collection("photo");
fab = (FloatingActionButton) findViewById(R.id.fabPhoto);
circularProgressbar.setVisibility(View.VISIBLE);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
photoList = new ArrayList<>();
photoAdapter = new PhotoAdapter(this, photoList);
recyclerView.setAdapter(photoAdapter);
showPhotoGrid();
}
private void showPhotoGrid() {
collectionReference
.orderBy("datePhoto", Query.Direction.DESCENDING)
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
assert queryDocumentSnapshots != null;
if (!queryDocumentSnapshots.isEmpty()){
//if contains photos
circularProgressbar.setVisibility(View.GONE);
Toast.makeText(PhotoActivity.this, "Success", Toast.LENGTH_SHORT).show();
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot documentSnapshot : list){
Photo photo = documentSnapshot.toObject(Photo.class);
assert photo != null;
photo.setID(documentSnapshot.getId());
photoList.add(photo);
}
photoAdapter.notifyDataSetChanged();
} else {
//show no photos
circularProgressbar.setVisibility(View.GONE);
Toast.makeText(PhotoActivity.this, "No photos added yet", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
circularProgressbar.setVisibility(View.GONE);
Toast.makeText(PhotoActivity.this, "Error in showing images: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
public void addPhoto(View view) {
showImageChooser();
}
private void showImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
pickedImageUrl = data.getData();
Picasso.with(this).load(pickedImageUrl).into(pickedImageView);
confirmLayout.setVisibility(View.VISIBLE);
fab.setVisibility(View.GONE);
photoAdapter.notifyDataSetChanged();
}
}
public void hideConfirmLayout(View view) {
pickedImageView.setImageDrawable(null);
confirmLayout.setVisibility(View.GONE);
fab.setVisibility(View.VISIBLE);
}
public void uploadPhoto(View view) {
title = titleEditText.getText().toString().trim();
photoDescription = photoDescriptionEditText.getText().toString().trim();
if (title.isEmpty()) {
titleEditText.setError("Please give a title");
} else if (photoDescription.isEmpty()) {
photoDescription = "No description for this photo";
}
if (pickedImageUrl != null) {
if (title.isEmpty()) {
titleEditText.setError("Required");
} else {
StorageReference imageReference = storageReference.child(title + "." + getFileExtension(pickedImageUrl));
imageReference.putFile(pickedImageUrl).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setProgress(0);
}
}, 1000);
String thatImageUrl = taskSnapshot.getStorage().getDownloadUrl().toString();
Photo photo = new Photo(userId, title, photoDescription, thatImageUrl, datePhoto);
collectionReference.add(photo).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(PhotoActivity.this, "Saved to gallery", Toast.LENGTH_SHORT).show();
confirmLayout.setVisibility(View.GONE);
pickedImageView.setImageDrawable(null);
titleEditText.setText(null);
photoDescriptionEditText.setText(null);
fab.setVisibility(View.VISIBLE);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(PhotoActivity.this, "Firestore error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(PhotoActivity.this, "Storage error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(#NonNull UploadTask.TaskSnapshot taskSnapshot) {
progressBar.setVisibility(View.VISIBLE);
double progress = 100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount();
progressBar.setProgress((int) progress);
}
});
}
} else {
Toast.makeText(this, "No photo is selected", Toast.LENGTH_LONG).show();
}
}
private String getFileExtension(Uri uri) {
//get photo type: jpeg, png
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
}
The Photo object class
public class Photo implements Serializable {
private String ID;
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
private String photoTitle;
private String photoDescription;
private String photoUrl;
#ServerTimestamp
private Date datePhoto;
public Date getDatePhoto() {
return datePhoto;
}
public void setDatePhoto(Date datePhoto) {
this.datePhoto = datePhoto;
}
public Photo() {
}
public Photo(String ID, String photoTitle, String photoDescription, String photoUrl, Date datePhoto) {
this.ID = ID;
this.photoTitle = photoTitle;
this.photoDescription = photoDescription;
this.photoUrl = photoUrl;
this.datePhoto = datePhoto;
}
public String getPhotoTitle() {
return photoTitle;
}
public void setPhotoTitle(String photoTitle) {
this.photoTitle = photoTitle;
}
public String getPhotoDescription() {
return photoDescription;
}
public void setPhotoDescription(String photoDescription) {
this.photoDescription = photoDescription;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
the PhotoAdapter:
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {
private Context context;
private List<Photo> photoList;
public PhotoAdapter(Context context, List<Photo> photoList) {
this.context = context;
this.photoList = photoList;
}
#NonNull
#Override
public PhotoViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new PhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.single_photo_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull PhotoViewHolder holder, int position) {
Photo photo = photoList.get(position);
if (photo.getPhotoUrl() != null && !photo.getPhotoUrl().isEmpty()){
Picasso.with(context).load(photo.getPhotoUrl()).into(holder.showImage);
}
}
#Override
public int getItemCount() {
return photoList.size();
}
public class PhotoViewHolder extends RecyclerView.ViewHolder {
ImageView showImage;
public PhotoViewHolder(View inflate) {
super(inflate);
showImage = inflate.findViewById(R.id.singlePhotoImageView);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient0"
tools:context=".PhotoActivity">
<TextView
android:id="#+id/titlePhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/quicksand_bold"
android:gravity="center"
android:padding="#dimen/_20sdp"
android:text="PHOTO GALLERY"
android:textColor="#color/white"
android:textSize="#dimen/_14sdp"
android:layout_alignParentTop="true"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/titlePhoto"/>
<LinearLayout
android:id="#+id/confirmPhotoUploadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/white_rounded"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="#dimen/_20sdp"
android:paddingBottom="#dimen/_10sdp"
android:visibility="gone">
<EditText
android:id="#+id/photoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_16sdp"
android:layout_marginTop="#dimen/_16sdp"
android:background="#null"
android:hint="Photo title" />
<EditText
android:id="#+id/photoDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_16sdp"
android:layout_marginEnd="#dimen/_16sdp"
android:layout_marginBottom="#dimen/_16sdp"
android:background="#null"
android:hint="Write something about this photo..." />
<ProgressBar
android:id="#+id/progressbarPhoto"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
tools:visibility="visible" />
<ImageView
android:id="#+id/pickedImage"
android:layout_width="match_parent"
android:layout_height="#dimen/_300sdp"
android:layout_marginBottom="#dimen/_14sdp"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_10sdp"
android:gravity="center">
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/red_rounded_bg"
android:onClick="hideConfirmLayout"
android:text="cancel"
android:textColor="#color/white" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:background="#drawable/rounded_button"
android:onClick="uploadPhoto"
android:text="save to gallery"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/_20sdp"
android:clickable="true"
android:focusable="true"
android:onClick="addPhoto"
android:src="#drawable/ic_add" />
<ProgressBar
android:id="#+id/progress_circularPhoto"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_100sdp"
android:layout_centerInParent="true"
android:visibility="gone"
tools:visibility="visible"/>
</RelativeLayout>
single photo Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/singlePhotoImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I found a solution based on this post--> How to use getdownloadurl in recent versions?
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//get the uri and carry out action here
//uri = correct filepath
}
now the images are showing in the recyclerview, yay ! Thanks for the help!
It seems like your urls are wrong.
taskSnapshot.getDownloadUrl returns Task, not the Uri or Url string. You need to add OnSuccessListener callback to get download Uri
Try to save urls by this way:
taskSnapshot.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
String url = downloadUrl.toString()
// save this url to Firestore
}
Check this in Official documentation
New to all this....I have created the following code:
Custom Class Object
public class Category {
private String documentID;
private int id;
private String description;
private boolean active;
private int sort;
public Category(){
//public no-arg constructor needed
}
public Category(int id, String description, boolean active, int sort) {
this.id = id;
this.description = description;
this.active = active;
this.sort = sort;
}
#Exclude
public String getDocumentID() {
return documentID;
}
public void setDocumentID(String documentID) {
this.documentID = documentID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
#Override
public String toString() {
return description;
}
}
MainActivity Class
public class MainActivity extends AppCompatActivity {
// Local XML obect declarations...
private Spinner spinner_Category;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Load the CATEGORY spinner with the ACTIVE categories from the Firestore
spinner_Category = (Spinner)findViewById(R.id.spinner_category);
populateCategories();
spinner_Category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Category category = (Category) parent.getSelectedItem();
displayCategoryData(category);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
String message = "Nothing selected";
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
private void populateCategories(){
// DBHelper dbHelper = DBHelper.getInstance(this);
DBHelper dbHelper = new DBHelper(this);
ArrayList<Category> categoryList = dbHelper.getAllCategories();
ArrayAdapter<Category> adapterCategories = new ArrayAdapter<Category>(this,
android.R.layout.simple_spinner_item, categoryList);
adapterCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_Category.setAdapter(adapterCategories);
}
private void displayCategoryData(Category category) {
String description = category.getDescription();
int sort = category.getSort();
boolean active = category.isActive();
String userData = "Description: " + description + "\nSort: " + sort + "\nActive: " + active;
Toast.makeText(this, userData, Toast.LENGTH_LONG).show();
}
}
Method from my DBHelper class (a general class object to read/write to the Firestore dBase)
/* Function: getAllCategories
* Return a collection of ACTIVE cattegories from the Firestore */
public ArrayList<Category> getAllCategories() {
final ArrayList<Category> categoryList = new ArrayList<>();
CollectionReference colref = db.collection(COLLECTION_CATEGORIES);
colref.whereEqualTo(CATEGORY_ACTIVE, true)
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot dcSnapshot: queryDocumentSnapshots) {
Category c = dcSnapshot.toObject(Category.class);
c.setDocumentID(dcSnapshot.getId());
categoryList.add(c);
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, e.toString());
}
});
return categoryList;
}
And finally the activity_main XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="10sp"
android:text="App Title"
android:textColor="#color/cardview_dark_background"
android:textSize="18sp" />
<Spinner
android:id="#+id/spinner_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="16dp" />
The issue is this code may populate the Spinner with Category objects but it doesn't display, initially select or trigger a OnItemSelected event. As this code is practically identical to other code I have used reading records from an SQLite database rather than a Firestore database then I am assuming it's the QueryDocumentSnapshot.toObject() method that alters the collection in some way.
Any advise will be greatly appreciated...
After populating the list,and adding the list to the adapter, run this command:
adapterCategories.notifyDataSetChanged();
Instead of using the default layout resource for the adapter, create a custom one and use it. So in your layout folder, create a layout such as this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
/>
and name it spinner_textbox.xml. Then in your activity, instead of this:
ArrayAdapter<Category> adapterCategories = new ArrayAdapter<Category>(this,
android.R.layout.simple_spinner_item, categoryList);
adapterCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
do this:
ArrayAdapter<Category> adapterCategories = new ArrayAdapter<Category>(this,
R.layout.spinner_textbox, categoryList);
adapterCategories.setDropDownViewResource(R.layout.spinner_textbox);
Remember to remove the 'android' so that it finds your custom layout from R.
Here is my Orderofday.java where i am getting data from URL and in String proceeding_file i get pdf link. i have shown the data present on every card but could not achieve to open a pdf link on card button clicked.
public class Orderofday extends AppCompatActivity {
private static final String TAG = Orderofday.class.getSimpleName();
private RecyclerView recyclerView;
private OrderList mAdapter;
ProgressDialog progess;
List<OrdersModel> myList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orderofday);
progess = new ProgressDialog(Orderofday.this);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new OrderList(myList, Orderofday.this);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
getResult();
}
public void getResult() {
String url = "http://senate.gov.pk/ne/getdata/order_of_day_json.php";
//detect internet and show the data
if (isNetworkStatusAvialable(getApplicationContext())) {
progess.setMessage("Loading data....");
progess.show();
StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response", response);
try {
JSONObject mainList = new JSONObject(response);
JSONArray result = mainList.getJSONArray("data");
if (result.length() > 0) {
for (int i = 0; i < result.length(); i++) {
mainList = result.getJSONObject(i);
OrdersModel model = new OrdersModel();
model.title_en = mainList.getString("title_en");
model.YearID = mainList.getString("YearID");
model.session_title_en = mainList.getString("session_title_en");
model.sitting_date = mainList.getString("sitting_date");
model.proceeding_file = mainList.getString("proceeding_file");
myList.add(model);
}
mAdapter.notifyDataSetChanged();
progess.dismiss();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
progess.dismiss();
}
});
Log.d("params", strReq.getUrl() + " and " + strReq.getBodyContentType() + " abd " + strReq.getBodyContentType());
// Adding String request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, TAG);
} else {
Toast.makeText(getApplicationContext(), "Please check your Internet Connection", Toast.LENGTH_SHORT).show();
}
}
//check internet connection
public static boolean isNetworkStatusAvialable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if (netInfos != null) {
return netInfos.isConnected();
}
}
return false;
}
}
Here is my OrderList.java class where i am setting the text from OderModel.java
public class OrderList extends RecyclerView.Adapter<OrderList.MyViewHolder> {
List<OrdersModel> list;
Context c;
public OrderList(List<OrdersModel> list, Context c) {
this.list = list;
this.c = c;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.orders_list, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
OrdersModel movie = list.get(position);
holder.name.setText(movie.gettitle_en());
holder.type.setText(movie.getYearID());
holder.session_title.setText((movie.getsession_title_en()));
holder.sitting_date.setText(movie.getsitting_date());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, type , session_title , sitting_date , proceeding_file;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
type = view.findViewById(R.id.type);
session_title = view.findViewById(R.id.session_title);
sitting_date = view.findViewById(R.id.sitting_date);
}
}
}
And here is my OrderModel.java where i made getters and setter
public class OrdersModel {
String title_en;
String YearID;
String session_title_en;
String sitting_date;
String proceeding_file;
public String gettitle_en() {
return title_en;
}
public void settitle_en(String title_en) {
this.title_en = title_en;
}
public String getYearID() {
return YearID;
}
public void setYearID(String YearID) {
this.YearID = YearID;
}
public String getsession_title_en() {
return session_title_en;
}
public void setsession_title_en(String session_title_en) {
this.session_title_en = session_title_en;
}
public String getsitting_date() {
return sitting_date;
}
public void setsitting_date(String session) {
this.sitting_date = sitting_date;
}
public String getproceeding_file() {
return proceeding_file;
}
public void setproceeding_file(String date) {
this.proceeding_file = proceeding_file;
}
String file;
}
Now i need to open a pdf against every card view clicked i got url of pdf in procedding_file (String) but couldn't get it. Kindly help me out to achieve this and here is xml.
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="#dimen/_5sdp"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="5dp"
android:text="Title_ID"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/type"
android:layout_below="#id/name"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="YearID"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/session_title"
android:layout_below="#id/type"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="Session Title"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sitting_date"
android:layout_below="#id/session_title"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="Date "
android:textColor="#android:color/black" />
<Button
android:id="#+id/view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sitting_date"
android:layout_alignParentRight="true"
android:text="Click to View Pdf"
android:textColor="#000000"
android:textSize="#dimen/_10sdp">
</Button>
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Above answer is correct. However a better solution and the rightest one is to create an interface for the clicklistenet and implement it in tour activity
Initialize viewButton in ViewHolder.
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, type , session_title , sitting_date , proceeding_file;
public Button viewButton;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
type = view.findViewById(R.id.type);
session_title = view.findViewById(R.id.session_title);
sitting_date = view.findViewById(R.id.sitting_date);
viewButton = view.findViewById(R.id.view_button);
}
}
Then set onClickListener on viewButton and openPdf
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
OrdersModel movie = list.get(position);
holder.name.setText(movie.gettitle_en());
holder.type.setText(movie.getYearID());
holder.session_title.setText((movie.getsession_title_en()));
holder.sitting_date.setText(movie.getsitting_date());
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openPdf(movie.getproceeding_file())
}
});
}
Suggestion: You should move your PdfViewer in another activity and redirect user to that activity when click viewButton.
initialise view_pdf button in MyViewHolder class. like others.
parse proceeding file like others ~ movie.getproceeding_file()
under the onBindViewHolder put below snippet of code:
holder.proceeding_file.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String pdf_url = movie.getproceeding_file();
// do not forget to concate preceding url of the pdf
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
}
});
I'm new to Android programming, my problem is when update one record in another Activity from my MainActivity, the RecyclerView is not refreshed.
I have a ManinActivity with a RecyclerView, an activity_Detail for inserting and updating records and an activity_CardView to view the items stored in the database.
Here is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
rvNoteAdapter rvNoteAdapter;
private Toolbar mToolbar;
private FloatingActionButton fab;
private String rowID = null;
NoteDatabaseAdapter note_database;
FloatingActionButton btnAddNewRecord;
android.widget.LinearLayout parentLayout;
LinearLayout layoutDisplayPeople;
TextView tvNoRecordsFound;
ArrayList<create_note> notes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getAllWidgets();
note_database = new NoteDatabaseAdapter(MainActivity.this,6);
bindWidgetsWithEvent();
//displayAllRecords();
Recycler_do();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String note_type = data.getStringExtra(Constants.COLUMN_NOTE_TYPE);
String note_text = data.getStringExtra(Constants.COLUMN_NOTE_TEXT);
String note_id = data.getStringExtra(Constants.ID);
create_note note = new create_note();
note.setType(note_type);
note.setText(note_text);
if (requestCode == Constants.ADD_RECORD) {
//sQLiteHelper.insertRecord(firstname, lastname);
note_database.insert(note);
//recyclerAdapterNote.addItem(0,note);
} else if (requestCode == Constants.UPDATE_RECORD) {
note.setId(note_id);
//sQLiteHelper.updateRecord(firstname, lastname, rowID);
note_database.update(note);
}
Recycler_do();
}
}
private void getAllWidgets() {
btnAddNewRecord = (FloatingActionButton) findViewById(R.id.fab);
parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
layoutDisplayPeople = (LinearLayout) findViewById(R.id.layoutDisplayNote);
tvNoRecordsFound = (TextView) findViewById(R.id.tvNoRecordsFound);
recyclerView = (RecyclerView) findViewById(R.id.rvNotes);
}
public void Recycler_do ()
{
notes = note_database.getAllRecords();
LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
rvNoteAdapter adapter=new rvNoteAdapter(MainActivity.this,contacts);
recyclerView.setAdapter(adapter);
}
private void bindWidgetsWithEvent() {
btnAddNewRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onAddRecord();
}
});
}
private void onAddRecord() {
Intent intent = new Intent(MainActivity.this, Detail.class);
intent.putExtra(Constants.DML_TYPE, Constants.INSERT);
startActivityForResult(intent, Constants.ADD_RECORD);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_Detail:
public class Detail extends AppCompatActivity {
EditText note_type;
EditText note_text;
RecyclerView recyclerView;
NoteDatabaseAdapter noteDatabaseAdapter=new NoteDatabaseAdapter(Detail.this,6);
String id;
String request="";
TextView toolbarText;
ImageView btnDML;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Detail);
getAllWidgets();
bindWidgetsWithEvent();
checkForRequest();
}
private void checkForRequest() {
request = getIntent().getExtras().get(Constants.DML_TYPE).toString();
if (request.equals(Constants.UPDATE)) {
toolbarText.setText("update");
note_text.setText(getIntent().getExtras().get(Constants.COLUMN_NOTE_TEXT).toString());
note_type.setText(getIntent().getExtras().get(Constants.COLUMN_NOTE_TYPE).toString());
id=getIntent().getExtras().get(Constants.ID).toString();
} else {
toolbarText.setText("new note");
}
}
private void bindWidgetsWithEvent() {
btnDML.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onButtonClick();
}
});
final ImageView back_btn = (ImageView) findViewById(R.id.back_btn);
back_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Detail.this,"back btn",Toast.LENGTH_LONG).show();
finish(); //back to prev activity
}
});
}
private void getAllWidgets() {
note_type = (EditText) findViewById(R.id.note_type_EB);
note_text = (EditText) findViewById(R.id.note_text_EB);
toolbarText=(TextView) findViewById(R.id.toolbar_text);
btnDML = (ImageView) findViewById(R.id.save_btn);
recyclerView=(RecyclerView) findViewById(R.id.rvNotes);
setTitle(null);
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
//topToolBar.setLogo(R.mipmap.ic_launcher);
// topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
}
private void onButtonClick() {
if (note_type.getText().toString().equals("") || note_text.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Add Both Fields", Toast.LENGTH_LONG).show();
} else if (request.equals(Constants.INSERT))
{
Intent intent = new Intent();
intent.putExtra(Constants.COLUMN_NOTE_TYPE, note_type.getText().toString());
intent.putExtra(Constants.COLUMN_NOTE_TEXT, note_text.getText().toString());
intent.putExtra(Constants.ID, id);
setResult(RESULT_OK, intent);
finish();
}
else if (request.equals(Constants.UPDATE))
{
create_note note = new create_note();
note.setType(note_type.getText().toString());
note.setText(note_text.getText().toString());
note.setId(id);
try {
noteDatabaseAdapter.update(note);
ArrayList<create_note> noteArrayList = noteDatabaseAdapter.getAllRecords();
ArrayList<create_note> temp=noteDatabaseAdapter.getAllRecords();
rvNoteAdapter rvNoteAdaptermodel=new rvNoteAdapter(Detail.this,temp);
**//do not refresh RecyclerView
rvNoteAdaptermodel.updateItems(noteArrayList);**
}catch (Exception e)
{
Log.d("Database", "Exception:" + e.getMessage());
}
finish();
//
}
}
activity_CardView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="5dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv">
<RelativeLayout
android:id="#+id/inflateParentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<ImageView
android:contentDescription="#string/action_new"
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="#mipmap/ic_save"
/>
<TextView
android:layout_toRightOf="#id/back_btn"
android:id="#+id/note_type_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:text="Name"
android:textSize="15sp"
android:gravity="start"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
/>
<TextView
android:layout_toRightOf="#id/back_btn"
android:id="#+id/note_text_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:text="Name"
android:textSize="15sp"
android:gravity="start"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/note_type_inflate" />
<TextView
android:layout_toRightOf="#id/back_btn"
android:id="#+id/note_id_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:text="Name"
android:textSize="15sp"
android:gravity="start"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/note_text_inflate" />
</RelativeLayout>
rvNoteAdapter.java (Adapter for RecyclerView)
public class rvNoteAdapter extends RecyclerView.Adapter<rvNoteAdapter.ViewHolder> {
private List<create_note> noteList= Collections.emptyList();
Context noteContext;
private create_note note;
private String rowID = null;
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView noteText;
public TextView noteType;
public TextView noteId;
private CardView cardView;
public ViewHolder(View itemView){
super(itemView);
noteText=(TextView) itemView.findViewById(R.id.note_text_inflate);
noteType=(TextView) itemView.findViewById(R.id.note_type_inflate);
noteId=(TextView) itemView.findViewById(R.id.note_id_inflate);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
public rvNoteAdapter(Context context,List<create_note> Note_List){
this.noteList=Note_List;
this.noteContext=context;
}
private Context getNoteContext()
{
return noteContext;
}
#Override
public rvNoteAdapter.ViewHolder onCreateViewHolder (ViewGroup parent,int viewType)
{
// Context context=parent.getContext();
// LayoutInflater inflater=LayoutInflater.from(context);
//View NoteView=inflater.inflate(R.layout.activity_CardView,parent,false);
//ViewHolder viewHolder=new ViewHolder(NoteView);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_CardView, parent, false);
ViewHolder memberViewHolder = new ViewHolder(view);
return memberViewHolder;
}
#Override
public void onBindViewHolder(final rvNoteAdapter.ViewHolder viewHolder,int position)
{
//viewHolder.noteText.setText(noteList.get(position).getText());
create_note note=noteList.get(position);
viewHolder.noteText.setText(note.getText());
viewHolder.noteType.setText(note.getType());
viewHolder.noteId.setText(note.getId());
final int pos=position;
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
create_note note=(create_note)v.getTag();
String note_text=viewHolder.noteText.getText().toString();
String note_type=viewHolder.noteType.getText().toString();
String note_id=viewHolder.noteId.getText().toString();
Intent intent = new Intent(v.getContext(), Detail.class);
intent.putExtra(Constants.COLUMN_NOTE_TYPE, note_type);
intent.putExtra(Constants.COLUMN_NOTE_TEXT, note_text);
intent.putExtra(Constants.ID, note_id);
intent.putExtra(Constants.DML_TYPE, Constants.UPDATE);
v.getContext().startActivity(intent);
// mainActivity.onUpdateRecord(note_text,"jhkhkj",note_id);
Toast.makeText(noteContext,"clicked"+pos,Toast.LENGTH_LONG).show();
}
});
}
//my problem with this code that not refreshed RecyclerView
public void updateItems(ArrayList<create_note> notes)
{
noteList.clear();
noteList.addAll(notes);
notifyDataSetChanged();
}
public void addItem(int position, create_note note) {
try {
noteList.add(position, note);
notifyItemInserted(position);
}
catch (Exception e) {
}
}
public void removeItem(create_note note) {
int position=noteList.indexOf(note);
noteList.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return noteList.size();
}
NoteDatabaseAdapter.java (adapter for database) :
public class NoteDatabaseAdapter extends SQLiteOpenHelper {
public static final String TABLE_NAME = "tbl_note";
public static final String ID = "ID";
public static final String COLUMN_NOTE_TYPE = "type";
public static final String COLUMN_NOTE_TEXT = "text";
private SQLiteDatabase database;
public NoteDatabaseAdapter(Context context,int newVersion) {
super(context,"noteDatabase.db",null,newVersion);
}
#Override
public void onCreate(SQLiteDatabase db)
{
try {
String sql = "create table "+ TABLE_NAME + " ( id integer primary key autoincrement NOT NULL," + COLUMN_NOTE_TYPE + " NVARCHAR," + COLUMN_NOTE_TEXT + " NVARCHAR)";
db.execSQL(sql);
}catch (Exception e)
{
Log.d("Database", "Exception:" + e.getMessage());
}
}
#Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
try{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}catch (Exception e)
{
Log.d("Database", "Exception:" + e.getMessage());
}
}
public void insert(create_note note) {
try {
database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NOTE_TYPE, note.getType());
contentValues.put(COLUMN_NOTE_TEXT, note.getText());
database.insert(TABLE_NAME, null, contentValues);
database.close();
} catch (Exception e) {
Log.d("Database", "Exception:" + e.getMessage());
}
}
public void update(create_note note) {
try {
database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NOTE_TYPE, note.getType());
contentValues.put(COLUMN_NOTE_TEXT, note.getText());
database.update(TABLE_NAME,contentValues,"id="+note.getId(),null);
database.close();
} catch (Exception e) {
Log.d("Database", "Exception:" + e.getMessage());
}
}
public ArrayList<create_note> getAllRecords() {
database = this.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
ArrayList<create_note> notes = new ArrayList<create_note>();
create_note note_model;
cursor.moveToLast();
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
note_model = new create_note();
note_model.setId( cursor.getString(0));
note_model.setType(cursor.getString(1));
note_model.setText(cursor.getString(2));
notes.add(note_model);
cursor.moveToPrevious();
}
}
cursor.close();
database.close();
return notes;
}
}
create_note.java (object class)
public class create_note {
private String id;
private String type;
private String text;
public create_note(){}
public create_note(String type, String text) {
this.type = type;
this.text = text;
}
public create_note(String id, String type, String text) {
this.id = id;
this.type = type;
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setType(String type) {
this.type = type;
}
}
My problem is in rvNoteAdapter.java on UpdateItem method.
adapter.invalidate is not working,
Can anybody help me?
Use this to set and refresh apter of recyclerview.
if (null == rvNoteAdapter) {
rvNoteAdapter rvNoteAdaptermodel=new
rvNoteAdapter(Detail.this,temp);
rvNoteAdaptermodel.updateItems(noteArrayList);
recyclerView.setAdapter(adapter);
} else {
rvNoteAdaptermodel.updateItems(noteArrayList);
rvNoteAdapter.notifyDataSetChanged();
}
And your updateItemsmethod should be like,
public void updateItems(ArrayList<create_note> notes)
{
noteList.clear();
noteList.addAll(notes);
}
i use this method to update Recyclerview
in MainActivity
set notes ArreyList as static
set rvNotAdapter as static
then i use this variables in DetailActivity.
in DetailActivity use this code :
ArreyList<create_note> noteArreyList = noteDatabaseAdapter.getAllRecords();
MainActivity.notes.clear();
MainActivity.notes.addAll(noteArreyList);
MainActivity.rvNoteAdapter.notifyDataSetChanged();
i use this codes after i updete my data in database :
noteDatabaseAdapter.update(note);
I'm trying to change the icon of a button in my recycler view every time the activity starts based off a boolean value in my custom object. I assume this has to be done within the adapter since not every groups button will have the same background.
Below is the code for my recycler view adapter:
public class RecipeListAdapter extends RecyclerView.Adapter<RecipeListAdapter.ViewHolder>{
private List<Recipe> mRecipeSet;
private Button mAddToGroceriesButton;
public RecipeListAdapter(List<Recipe> recipes){
mRecipeSet = recipes;
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//This is what will handle what happens when you click a recipe in the recycler view
private TextView mRecipeName;
private TextView mPrepTime;
private TextView mCookTime;
private TextView mServingSize;
private RelativeLayout mRecipeTextSection;
public ViewHolder(View v) {
super(v);
mRecipeName = (TextView) v.findViewById(R.id.recipe_list_recycler_view_recipe_name);
mServingSize = (TextView) v.findViewById(R.id.recipe_list_recycler_view_serving_size);
mPrepTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_prep_time);
mCookTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_cook_time);
mRecipeTextSection = (RelativeLayout) v.findViewById(R.id.recycled_item_section_view);
mRecipeTextSection.setOnClickListener(this);
mAddToGroceriesButton = (Button) v.findViewById(R.id.add_to_grocery_list);
mAddToGroceriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Recipe recipeToGrocery = mRecipeSet.get(position);
//RecipeDB dbHelper = new RecipeDB(v.getContext());
//dbHelper.addGroceryItem(recipeToGrocery);
if(!recipeToGrocery.isInList()) {
RecipeDB dbHelper = new RecipeDB(v.getContext());
dbHelper.addGroceryItem(recipeToGrocery);
recipeToGrocery.setInList(true);
dbHelper.updateRecipe(recipeToGrocery);
mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
Toast.makeText(v.getContext(), recipeToGrocery.getRecipeName() + " added to grocery list.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(v.getContext(), "That recipe is already in the list.", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onClick(View v){
int position = getAdapterPosition();
Intent i = new Intent(v.getContext(), RecipeTextView.class);
Recipe selectedRecipe = mRecipeSet.get(position);
i.putExtra("view_recipe_key", selectedRecipe);
v.getContext().startActivity(i);
}
}
public void add(int position, Recipe item) {
mRecipeSet.add(position, item);
notifyItemInserted(position);
}
public void remove(Recipe item) {
int position = mRecipeSet.indexOf(item);
mRecipeSet.remove(position);
notifyItemRemoved(position);
}
public RecipeListAdapter(ArrayList<Recipe> myRecipeset) {
mRecipeSet = myRecipeset;
}
// Create new views (invoked by the layout manager)
#Override
public RecipeListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_recycled, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Recipe recipe = mRecipeSet.get(position);
String recipeName = recipe.getRecipeName();
String prepTime = "Prep Time: " + String.valueOf(recipe.getPrepTime()) + " minutes";
String cookTime = "Cook Time: " + String.valueOf(recipe.getCookTime()) + " minutes";
String servingSize = "Servings: " + String.valueOf(recipe.getServings());
holder.mRecipeName.setText(recipeName);
//Only display values if they are not null
if(recipe.getServings() != null) {
holder.mServingSize.setText(servingSize);
}
if (recipe.getPrepTime() != null) {
holder.mPrepTime.setText(prepTime);
}
if(recipe.getCookTime() != null) {
holder.mCookTime.setText(cookTime);
}
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
if(mRecipeSet != null) {
return mRecipeSet.size();
}
return 0;
}
}
I know how to change the background of the button when it's clicked with
mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
but it's obviously not going to save the state of that button when the activity restarts. I'm just not sure of how to check the boolean value for each group upon activity start up and change the button background accordingly.
I tried using
if(recipe.isInList()){
mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
}
in the onBindViewHolder method but it didn't do anything, and I'm pretty sure that wouldn't be the correct place for it anyways. I know the boolean is working properly since I use it in other places and it works fine.
Here's the relevant XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/apk/res/android"
android:layout_margin="7dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="#+id/recycled_item_section_view"
android:elevation="30dp"
android:background="#drawable/background_border"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Recipe name"
android:textSize="24dp"
android:textColor="#color/black"
android:id="#+id/recipe_list_recycler_view_recipe_name"
android:paddingBottom="3dp"
android:maxWidth="275dip"
android:singleLine="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#color/black"
android:layout_below="#id/recipe_list_recycler_view_recipe_name"
android:id="#+id/recipe_list_recycler_view_serving_size"
android:paddingBottom="3dp"/>
<Button
android:layout_width="35dp"
android:layout_height="35dp"
android:background="#mipmap/ic_playlist_add_black_24dp"
android:height="36dp"
android:padding="8dp"
android:layout_alignParentRight="true"
android:id="#+id/add_to_grocery_list"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recipe_list_recycler_view_serving_size"
android:layout_alignParentLeft="true"
android:textSize="18dp"
android:textColor="#color/black"
android:id="#+id/recipe_list_recycler_view_prep_time"
android:paddingBottom="3dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recipe_list_recycler_view_prep_time"
android:textSize="18dp"
android:textColor="#color/black"
android:layout_alignParentLeft="true"
android:id="#+id/recipe_list_recycler_view_cook_time"/>
</RelativeLayout>
Recipe class:
public class Recipe implements Parcelable {
//These are all of the qualities a recipe contains, we will create an arraylist of this in the activity
private String mRecipeName;
private int mID;
private String mServings;
private String mPrepTime;
private String mCookTime;
private boolean isInList;
private List<String> mIngredients;
private List<String> mDirections;
public Recipe(){
}
public Recipe(int id, String name, String serving, String prep, String cook, List<String>
ingredientsList, List<String> directionsList, boolean inList){
this.mID = id;
this.mRecipeName = name;
this.mServings = serving;
this.mPrepTime = prep;
this.mCookTime = cook;
this.mIngredients = ingredientsList;
this.mDirections = directionsList;
this.isInList = inList;
}
public Recipe(String name, String serving, String prep, String cook, List<String>
ingredientsList, List<String> directionsList, boolean inList){
this.mRecipeName = name;
this.mServings = serving;
this.mPrepTime = prep;
this.mCookTime = cook;
this.mIngredients = ingredientsList;
this.mDirections = directionsList;
this.isInList = inList;
}
public String getRecipeName() {
return mRecipeName;
}
public int getID() {
return mID;
}
public void setID(int id){
mID = id;
}
public String getServings() {
return mServings;
}
public String getPrepTime() {
return mPrepTime;
}
public void setRecipeName(String recipeName) {
mRecipeName = recipeName;
}
public void setServingSize(String servings) {
mServings = servings;
}
public void setPrepTime(String prepTime) {
mPrepTime = prepTime;
}
public void setServings(String servings) {
mServings = servings;
}
public List<String> getIngredients() {
return mIngredients;
}
public List<String> getDirections() {
return mDirections;
}
public String getCookTime() {
return mCookTime;
}
public void setCookTime(String cookTime) {
mCookTime = cookTime;
}
public void setIngredients(List<String> ingredients) {
mIngredients = ingredients;
}
public void setDirections(List<String> directions) {
mDirections = directions;
}
public boolean isInList() {
return isInList;
}
public void setInList(boolean inList) {
isInList = inList;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mRecipeName);
dest.writeInt(this.mID);
dest.writeString(this.mServings);
dest.writeString(this.mPrepTime);
dest.writeString(this.mCookTime);
dest.writeByte(this.isInList ? (byte) 1 : (byte) 0);
dest.writeStringList(this.mIngredients);
dest.writeStringList(this.mDirections);
}
protected Recipe(Parcel in) {
this.mRecipeName = in.readString();
this.mID = in.readInt();
this.mServings = in.readString();
this.mPrepTime = in.readString();
this.mCookTime = in.readString();
this.isInList = in.readByte() != 0;
this.mIngredients = in.createStringArrayList();
this.mDirections = in.createStringArrayList();
}
public static final Creator<Recipe> CREATOR = new Creator<Recipe>() {
#Override
public Recipe createFromParcel(Parcel source) {
return new Recipe(source);
}
#Override
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
}
And main activity class that uses the adapter:
public class RecipeList extends AppCompatActivity{
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private int REQUEST_CODE=1;
private Button mNavigateGroceryButton;
RecipeDB dbHelper = new RecipeDB(this);
List<Recipe> recipes;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipes = dbHelper.getAllRecipes();
setContentView(R.layout.activity_recipe_list);
mRecyclerView = (RecyclerView) findViewById(R.id.list_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecipeListAdapter(recipes);
mRecyclerView.setAdapter(mAdapter);
mNavigateGroceryButton = (Button) findViewById(R.id.navigate_to_groceries_button_list_view);
mNavigateGroceryButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(RecipeList.this, ExpandableListViewActivity.class);
//Log.d("Navigate", "navigate pressed" );
startActivity(i);
}
});
}
#Override
public void onBackPressed() {
}
public boolean onOptionsItemSelected(MenuItem item){
//Handles menu buttons
switch (item.getItemId()){
case R.id.recipe_list_add_recipe_actionbar_button:
//This button creates a new empty Recipe object and passes it to the EditRecipe class
//The Recipe object is passed as a parcelable
Recipe passedRecipe = new Recipe();
Intent i = new Intent(RecipeList.this, EditRecipe.class);
i.putExtra("passed_recipe_key", (Parcelable) passedRecipe);
startActivityForResult(i, REQUEST_CODE);
return true;
default:
Log.d("Name,", "default called");
return super.onOptionsItemSelected(item);
}
}
public void addNewReRecipe(Recipe recipe){
dbHelper.addRecipe(recipe);
recipes = dbHelper.getAllRecipes();
mAdapter = new RecipeListAdapter(recipes);
mRecyclerView.setAdapter(mAdapter);
}
//Makes the menu bar appear as it is in the action_bar_recipe_list_buttons menu layout file
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_recipe_list_buttons, menu);
return true;
}
//This code is called after creating a new recipe. This is only for creating, and not editing.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE){
if(resultCode == Activity.RESULT_OK) {
Recipe createdRecipe = data.getExtras().getParcelable("recipe_key");
addNewReRecipe(createdRecipe);
}
}
}
}
Looks like you need to declare your button at the top of your ViewHolder with your other views. So move the declaration from the top of your adapter:
private Button mAddToGroceriesButton;
Then in your onBindViewHolder method you can get a reference to your button through the holder and set the background:
if(recipe.isInList()) {
holder.mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
}
Try this,
public class RecipeListAdapter extends RecyclerView.Adapter<RecipeListAdapter.ViewHolder>{
private List<Recipe> mRecipeSet;
private Button mAddToGroceriesButton;
public RecipeListAdapter(List<Recipe> recipes){
mRecipeSet = recipes;
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//This is what will handle what happens when you click a recipe in the recycler view
private TextView mRecipeName;
private TextView mPrepTime;
private TextView mCookTime;
private TextView mServingSize;
private RelativeLayout mRecipeTextSection;
public ViewHolder(View v) {
super(v);
mRecipeName = (TextView) v.findViewById(R.id.recipe_list_recycler_view_recipe_name);
mServingSize = (TextView) v.findViewById(R.id.recipe_list_recycler_view_serving_size);
mPrepTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_prep_time);
mCookTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_cook_time);
mRecipeTextSection = (RelativeLayout) v.findViewById(R.id.recycled_item_section_view);
mAddToGroceriesButton = (Button) v.findViewById(R.id.add_to_grocery_list);
}
}
public void add(int position, Recipe item) {
mRecipeSet.add(position, item);
notifyItemInserted(position);
}
public void remove(Recipe item) {
int position = mRecipeSet.indexOf(item);
mRecipeSet.remove(position);
notifyItemRemoved(position);
}
public RecipeListAdapter(ArrayList<Recipe> myRecipeset) {
mRecipeSet = myRecipeset;
}
// Create new views (invoked by the layout manager)
#Override
public RecipeListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_recycled, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Recipe recipe = mRecipeSet.get(position);
String recipeName = recipe.getRecipeName();
String prepTime = "Prep Time: " + String.valueOf(recipe.getPrepTime()) + " minutes";
String cookTime = "Cook Time: " + String.valueOf(recipe.getCookTime()) + " minutes";
String servingSize = "Servings: " + String.valueOf(recipe.getServings());
holder.mRecipeName.setText(recipeName);
//Only display values if they are not null
if(recipe.getServings() != null) {
holder.mServingSize.setText(servingSize);
}
if (recipe.getPrepTime() != null) {
holder.mPrepTime.setText(prepTime);
}
if(recipe.getCookTime() != null) {
holder.mCookTime.setText(cookTime);
}
mRecipeTextSection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Intent i = new Intent(v.getContext(), RecipeTextView.class);
Recipe selectedRecipe = mRecipeSet.get(position);
i.putExtra("view_recipe_key", selectedRecipe);
v.getContext().startActivity(i);
});
Recipe recipeToGrocery = mRecipeSet.get(position);
if(!recipeToGrocery.isInList()) {
mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
}
else{
mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_26dp);//set another image
}
mAddToGroceriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Recipe recipeToGrocery = mRecipeSet.get(position);
//RecipeDB dbHelper = new RecipeDB(v.getContext());
//dbHelper.addGroceryItem(recipeToGrocery);
if(!recipeToGrocery.isInList()) {
RecipeDB dbHelper = new RecipeDB(v.getContext());
dbHelper.addGroceryItem(recipeToGrocery);
recipeToGrocery.setInList(true);
dbHelper.updateRecipe(recipeToGrocery);
notifyDataSetChanged();
}
else {
Toast.makeText(v.getContext(), "That recipe is already in the list.", Toast.LENGTH_SHORT).show();
}
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
if(mRecipeSet != null) {
return mRecipeSet.size();
}
return 0;
}
}