I have a RecyclerView which has items to display from REST API response when user choosed to load more data. However the data which binded to onBindViewHolder collapsed on the item.
The collapsed data is shown in the image below:
The orange and black text are come from different data list
The code to set data list to adapter when user choosed load more data:
mAdapter = new MaterialsRecyclerView(username, datePost, contentPost,
userPicturePost, contentType, titlePost, contentLabel, activityLabel, numberFavorite,
numberUpvote, numberDownvote, numberComments, upvoteStatus, downvoteStatus, favoriteStatus, getActivity(),
newsType, newsMedia, contentId, activityType
);
mRecyclerView.setAdapter(mAdapter);
I tried
mAdapter.notifyDataSetChanged();
after add data to data list in adapter but the data still collapsed.
My question is why collapsed data happened? And how to make the data not collapsed? I mean the data just loaded from data list suitable with the index.
Edit
Adapter class code(Constructor) method:
public MaterialsRecyclerView(List<String> usernameListParam, List<String> datePostListParam,
List<String> contentPostListParam, List<String> userPictureProfileListParam,
List<String> contentTypeListParam, List<String> titlePostListParam,
List<String> contentLabelListParam, List<String> activityLabelListParam,
List<Integer> textNumberFavoriteListParam, List<Integer> textNumberUpvoteListParam,
List<Integer> textNumberDownvoteListParam, List<Integer> textNumberCommentListParam,
List<Boolean> statusUpvotesParam, List<Boolean> statusDownvotesParam,
List<Boolean> statusFavoritesParam, Activity activity, List<Integer> newsTypeListParam,
List<String> newsMediaListParam, List<String> contentIdListParam,
List<Integer> activityCodeListParam) {
this.activity = activity;
usernameList = usernameListParam;
datePostList = datePostListParam;
contentPostList = contentPostListParam;
userPictureProfileList = userPictureProfileListParam;
contentTypeList = contentTypeListParam;
titlePostList = titlePostListParam;
contentTextList = contentLabelListParam;
activityTextList = activityLabelListParam;
textNumberDownvoteList = textNumberDownvoteListParam;
textNumberUpvoteList = textNumberUpvoteListParam;
textNumberFavoriteList = textNumberFavoriteListParam;
textNumberCommentList = textNumberCommentListParam;
statusUpvoteList = statusUpvotesParam;
statusDownvoteList = statusDownvotesParam;
statusFavoriteList = statusFavoritesParam;
newsTypeList = newsTypeListParam;
newsMediaList = newsMediaListParam;
contentIdList = contentIdListParam;
activityCodeList = activityCodeListParam;
username = new String[usernameList.size()];
datePost = new String[datePostList.size()];
titlePost = new String[titlePostList.size()];
contentPost = new String[contentPostList.size()];
userPictureProfile = new String[userPictureProfileList.size()];
contentType = new String[contentTypeList.size()];
contentText = new String[contentTextList.size()];
activityText = new String[activityTextList.size()];
textNumberFavorite = new Integer[textNumberFavoriteList.size()];
textNumberUpvote = new Integer[textNumberUpvoteList.size()];
textNumberDownvote = new Integer[textNumberDownvoteList.size()];
textNumberComment = new Integer[textNumberCommentList.size()];
statusUpvote = new Boolean[statusUpvoteList.size()];
statusDownvote = new Boolean[statusDownvoteList.size()];
statusFavorite = new Boolean[statusFavoriteList.size()];
newsType = new Integer[newsTypeList.size()];
newsMedia = new String[newsMediaList.size()];
contentId = new String[contentIdList.size()];
activityCode = new Integer[activityCodeList.size()];
username = usernameList.toArray(username);
datePost = datePostList.toArray(datePost);
titlePost = titlePostList.toArray(titlePost);
contentPost = contentPostList.toArray(contentPost);
userPictureProfile = userPictureProfileList.toArray(userPictureProfile);
contentType = contentTypeList.toArray(contentType);
contentText = contentTextList.toArray(contentText);
activityText = activityTextList.toArray(activityText);
textNumberFavorite = textNumberFavoriteList.toArray(textNumberFavorite);
textNumberUpvote = textNumberUpvoteList.toArray(textNumberUpvote);
textNumberDownvote = textNumberDownvoteList.toArray(textNumberDownvote);
textNumberComment = textNumberCommentList.toArray(textNumberComment);
statusUpvote = statusUpvoteList.toArray(statusUpvote);
statusDownvote = statusDownvoteList.toArray(statusDownvote);
statusFavorite = statusFavoriteList.toArray(statusFavorite);
newsType = newsTypeList.toArray(newsType);
newsMedia = newsMediaList.toArray(newsMedia);
contentId = contentIdList.toArray(contentId);
activityCode = activityCodeList.toArray(activityCode);
//notifyDataSetChanged();
}
Adapter xml code:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="#color/putih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:grid="http://schemas.android.com/tools"
android:gravity="center_vertical"
android:layout_margin="8dp">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<View
android:id="#+id/label_color_news"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/news_dashboard_mark"
android:visibility="invisible"
/>
<View
android:id="#+id/label_color_material"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/material_dashboard_mark"
android:visibility="invisible"
/>
<ImageView
android:id="#+id/user_picture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:foregroundGravity="right"
android:layout_below="#+id/label_color_news"
android:src="#drawable/recycleview_user" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:text="#string/username_label"
android:layout_alignTop="#id/user_picture"
android:layout_toRightOf="#id/user_picture"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:textColor="#color/abu"/>
<TextView
android:id="#+id/activity_username"
android:layout_width="wrap_content"
android:text="#string/username_activity_label"
android:layout_alignTop="#id/user_picture"
android:layout_toRightOf="#id/username"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:textColor="#color/abu"/>
<ImageView
android:id="#+id/info_button_content_activity"
android:layout_width="wrap_content"
android:layout_alignTop="#id/username"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:src="#drawable/ic_info_outline_black_18dp"
android:layout_marginLeft="5dp"
android:visibility="invisible"
/>
<TextView
android:id="#+id/date_post"
android:layout_width="120dp"
android:text="#string/date_label"
android:layout_below="#+id/username"
android:layout_toRightOf="#+id/user_picture"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_marginLeft="5dp"
android:textColor="#color/abu"/>
<TextView
android:id="#+id/post_label"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:foregroundGravity="center_horizontal"
android:layout_below="#+id/user_picture"
android:layout_marginLeft="10dp"
android:layout_alignTop="#+id/relativelayout_not_news_content"
android:layout_alignParentRight="true"
android:text="#string/material_author_label"
android:visibility="invisible"
/>
<TextView
android:id="#+id/post_activity_desc"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_picture"
android:layout_alignTop="#+id/relativelayout_not_news_content"
android:layout_alignParentRight="true"
android:text="#string/date_label"
android:textStyle="bold"
/>
<TextView
android:id="#+id/post_content_desc"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/post_activity_desc"
android:layout_alignParentRight="true"
android:text="#string/material_author_label"
android:textStyle="bold"
/>
<RelativeLayout
android:id="#+id/relativelayout_not_news_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user_picture"
android:visibility="gone">
<TextView
android:id="#+id/title_post"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="20sp"
android:text="#string/post_title_label"
android:layout_height="wrap_content"
android:textColor="#color/oranyeTerang"/>
<TextView
android:id="#+id/content_post"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textSize="15sp"
android:text="#string/post_content_label"
android:layout_below="#+id/title_post"
android:textColor="#color/abu"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativelayout_news_admin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user_picture"
android:visibility="gone">
<TextView
android:id="#+id/title_post_news_admin"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textColor="#color/oranyeTerang"
android:textSize="20sp"
android:text="#string/post_title_label"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/content_post_news_admin"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textSize="15sp"
android:text="#string/post_content_label"
android:layout_below="#+id/title_post_news_admin"
android:textColor="#color/abu"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativelayout_news_nonadmin_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user_picture"
android:visibility="gone">
<TextView
android:id="#+id/title_post_news_nonadmin_text"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textStyle="bold"
android:textSize="30sp"
android:text="#string/post_title_label"
android:layout_height="wrap_content"
android:textColor="#color/oranyeTerang"
android:visibility="gone"/>
<TextView
android:id="#+id/content_post_nonadmin_text"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textSize="20sp"
android:text="#string/post_content_label"
android:layout_below="#+id/title_post_news_nonadmin_text"
android:textColor="#color/abu"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativelayout_news_nonadmin_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user_picture"
android:visibility="gone">
<ImageView
android:id="#+id/media_post_news_nonadmin_media"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="400dp"
android:textColor="#color/abu"
/>
<TextView
android:id="#+id/caption_post_news_nonadmin_media"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:textSize="15sp"
android:text="#string/post_title_label"
android:layout_below="#+id/media_post_news_nonadmin_media"
android:layout_height="wrap_content"
android:textColor="#color/abu"/>
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="15dp">
<ImageView
android:id="#+id/icon_numbers_favorite"
android:src="#drawable/ic_favorite_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text_number_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/icon_numbers_upvote"
android:src="#drawable/ic_keyboard_arrow_up_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text_numbers_upvote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/icon_numbers_downvote"
android:src="#drawable/ic_keyboard_arrow_down_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text_numbers_downvote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<TextView
android:id="#+id/text_comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 Komentar"
android:gravity="right"
android:layout_gravity="right"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/awan"></View>
<LinearLayout
android:id="#+id/secondary_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/button_favorite"
android:src="#drawable/ic_favorite_border_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:id="#+id/button_comment"
android:src="#drawable/ic_chat_bubble_outline_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
<ImageView
android:id="#+id/button_upvote"
android:src="#drawable/ic_keyboard_arrow_up_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:id="#+id/button_downvote"
android:src="#drawable/ic_keyboard_arrow_down_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
POJO class which used as a data object model:
public class DashboardPOJO {
#SerializedName("rc")
public String rc;
#SerializedName("rm")
public String rm;
#SerializedName("skip")
public int skip;
#SerializedName("access_token")
public String access_token;
public DashboardPOJO(int skip, String access_token) {
this.skip = skip;
this.access_token = access_token;
}
public String getRm() {
return rm;
}
#SerializedName("results")
#Expose
private List<DashboardPOJO.Results> results = null;
public List<DashboardPOJO.Results> getResults() {
return results;
}
public class Results{
#SerializedName("dashboard")
#Expose
private Dashboard dashboard;
public class Dashboard {
#SerializedName("content_id")
#Expose
private String id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("desc")
#Expose
private String desc;
#SerializedName("synopsis")
#Expose
private String synopsis;
#SerializedName("content_code")
#Expose
private Integer content_code;
#SerializedName("activity_code")
#Expose
private Integer activityCode;
#SerializedName("post_by")
#Expose
private PostBy postBy;
#SerializedName("user_detail")
#Expose
private UserDetail userDetail;
#SerializedName("files")
#Expose
private List<DashboardPOJO.Results.Dashboard.Files> files = null;
#SerializedName("activity_text")
#Expose
private String activityText;
#SerializedName("content_text")
#Expose
private String contentText;
#SerializedName("news_type")
#Expose
private int newsType;
#SerializedName("upvote")
#Expose
private int upvote;
#SerializedName("downvote")
#Expose
private int downvote;
#SerializedName("favorite")
#Expose
private int favorite;
#SerializedName("upvoted")
#Expose
private boolean upvoted;
#SerializedName("downvoted")
#Expose
private boolean downvoted;
#SerializedName("favorited")
#Expose
private boolean favorited;
#SerializedName("comment")
#Expose
private int comment;
#SerializedName("created_at_from_now")
#Expose
private String createdAt;
public String getId(){
return id;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public String getSynopsis() {
return synopsis;
}
public Integer getContent_code() {
return content_code;
}
public Integer getActivityCode() {
return activityCode;
}
public String getActivityText() {
return activityText;
}
public String getContentText() {
return contentText;
}
public int getNewsType() {
return newsType;
}
public int getUpvote() {
return upvote;
}
public int getDownvote() {
return downvote;
}
public int getFavorite() {
return favorite;
}
//
public boolean getUpvoted() {
return upvoted;
}
public boolean getDownvoted() {
return downvoted;
}
public boolean getFavorited() {
return favorited;
}
public int getComment() {
return comment;
}
public String getCreatedAt() {
return createdAt;
}
public PostBy getPostBy() {
return postBy;
}
public UserDetail getUserDetail() {
return userDetail;
}
public void setPostBy(PostBy postBy) {
this.postBy = postBy;
}
/*#SerializedName("results")
#Expose
private List<DashboardPOJO.Results> results = null;
public List<DashboardPOJO.Results> getResults() {
return results;
}*/
public List<DashboardPOJO.Results.Dashboard.Files> getFiles() {
return files;
}
public class Files {
#SerializedName("originalname")
#Expose
private String originalName;
#SerializedName("filename")
#Expose
private String filename;
#SerializedName("http_path")
#Expose
private String httpPath;
public String getHttpPath() {
return httpPath;
}
public String getOriginalName() {
return originalName;
}
public String getFilename() {
return filename;
}
}
public class UserDetail {
#SerializedName("display_picture")
#Expose
private String displayPicture;
public String getDisplayPicture() {
return displayPicture;
}
}
public class PostBy {
#SerializedName("user_id")
#Expose
private String userId;
#SerializedName("username")
#Expose
private String username;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
}
public Dashboard getDashboard() {
return dashboard;
}
}}
In your layout you have several views with
android:layout_below="#+id/user_picture"
so they are drawn on top of each other
Related
I wanna ask how can I sum all patty price and display in the total ? The total is in activity_main.xml layout while all the product price is in view_product.xml .
Screenshot of app
As you can see the grey color text is per patty price , and the right side black text is price after multiply the quantity of patty. I want all the black text to sum up and show at below total.
MainActivity.java
package com.tankarjian.it212n.a2910assingment;
import android.app.Activity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private Products products;
private BaseAdapter productsAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = createInitialProductList();
productsAdapter = new ProductsAdapter(products, productClickListener, getLayoutInflater());
ListView productsListView = (ListView) findViewById(R.id.products_list);
productsListView.setAdapter(productsAdapter);
}
private Products createInitialProductList() {
return new Products(new ArrayList<>(Arrays.asList(
new Product("Chicken Patty", 0, 4.00),
new Product("Chicken Special Patty", 0, 5.00),
new Product("Imported Lamb Patty", 0, 8.00),
new Product("Imported Lamb Special Patty", 0, 10.00)
)));
}
private final ProductClickListener productClickListener = new ProductClickListener() {
#Override
public void onMinusClick(Product product) {
products.removeOneFrom(product);
productsAdapter.notifyDataSetChanged();
}
#Override
public void onPlusClick(Product product) {
products.addOneTo(product);
productsAdapter.notifyDataSetChanged();
}
};
private static class Products implements ProductDataSet {
private final List<Product> productList;
Products(List<Product> productList) {
this.productList = productList;
}
#Override
public int size() {
return productList.size();
}
#Override
public Product get(int position) {
return productList.get(position);
}
#Override
public long getId(int position) {
return position;
}
public void removeOneFrom(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity - 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
public void addOneTo(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity + 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
}
}
ProductsAdapter.java
package com.tankarjian.it212n.a2910assingment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Locale;
class ProductsAdapter extends BaseAdapter {
private final ProductDataSet productDataSet;
private final ProductClickListener productClickListener;
private final LayoutInflater layoutInflater;
ProductsAdapter(ProductDataSet productDataSet, ProductClickListener productClickListener, LayoutInflater layoutInflater) {
this.productDataSet = productDataSet;
this.productClickListener = productClickListener;
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return productDataSet.size();
}
#Override
public Product getItem(int position) {
return productDataSet.get(position);
}
#Override
public long getItemId(int position) {
return productDataSet.getId(position);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = createView(parent);
view.setTag(ViewHolder.from(view));
}
Product Products =(Product) getItem(position);
ImageView image = (ImageView) view.findViewById(R.id.imageBurgerView);
image.setImageResource(R.drawable.burger);
TextView text = (TextView) view.findViewById(R.id.singlePrice);
String price = String.format(Locale.ENGLISH, "%.2f", Products.getPrice());
text.setText(price);
TextView text1 = (TextView) view.findViewById(R.id.totalPrice);
String totalPrice = String.format(Locale.ENGLISH, "%.2f", (Products.getPrice() * Products.quantity));
text1.setText(totalPrice);
Product product = productDataSet.get(position);
ViewHolder viewHolder = (ViewHolder) view.getTag();
update(viewHolder, product);
return view;
}
private View createView(ViewGroup parent) {
return layoutInflater.inflate(R.layout.view_product, parent, false);
}
private void update(ViewHolder viewHolder, final Product product) {
viewHolder.name.setText(product.name);
viewHolder.quantity.setText(String.valueOf(product.quantity));
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onMinusClick(product);
}
});
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onPlusClick(product);
}
});
}
private static final class ViewHolder {
final TextView name;
final TextView quantity;
final View minus;
final View plus;
static ViewHolder from(View view) {
return new ViewHolder(
((TextView) view.findViewById(R.id.product_name)),
((TextView) view.findViewById(R.id.product_quantity)),
view.findViewById(R.id.product_minus),
view.findViewById(R.id.product_plus)
);
}
private ViewHolder(TextView name, TextView quantity, View minus, View plus) {
this.name = name;
this.quantity = quantity;
this.minus = minus;
this.plus = plus;
}
}
}
Product.java
package com.tankarjian.it212n.a2910assingment;
class Product {
final String name;
final int quantity;
private double price;
Product(String name, int quantity, Double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public double getPrice() {return price;}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_marginStart="15dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="16dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/products_list"
android:layout_width="0dp"
android:layout_height="512dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Switch
android:id="#+id/switchMember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Member"
app:layout_constraintEnd_toEndOf="#+id/products_list"
app:layout_constraintTop_toBottomOf="#+id/products_list" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:text="Tax "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/products_list"
app:layout_constraintVertical_bias="0.92" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginBottom="68dp"
android:text="Total "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/totalTax"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/finalTotalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/textView7"
android:layout_width="210dp"
android:layout_height="25dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView8"
android:layout_width="210dp"
android:layout_height="26dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
view_product.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF5722"
android:textSize="20sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBurgerView"
android:layout_width="70dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/burger" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/singlePrice"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_weight="1"
android:text="#string/textview"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/totalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/textview"
android:textAlignment="textStart"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="116dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/product_minus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="-"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="#+id/product_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="20"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/product_plus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="+"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>
</LinearLayout>
In view_product.xml you are only displaying the price, but that's not where the actual data is located so your question shouldn't be that the prices are in view_product.xml and you want to show in activity_main.xml, but your question should be -
Where is the data located
How to make the necessary calculations
Where to show it. .
Your data regarding the prices are in products object. So, in order to calculate the total you just need to add one more method which calculates the total
private double calculateTotal(ProductDataSet productDataSet) {
double totalPrice = 0.0;
for(int i=0; i<productDataSet.size(); i++) {
totalPrice += productDataSet.get(i).getPrice();
}
return totalPrice;
}
The result of this you can store in a new value or show directly in the textView itself.
((TextView)findViewById(R.id.finalTotalPrice)).setText(calculateTotal(products).toString());
This you can call anywhere after createInitialProductList(). I hope this helps and makes it clear for you.
The app quit when I change the line I am facing a problem when I try to develop an Android App that display the data from Firestore.
.
Can I know why it is not displaying the data on my RecyclerView and EventPage? I have rechecked everything, but I still can't find out why data doesn't show on EventPage.
EventPage.java
public class EventPage extends AppCompatActivity {
private static final String TAG = "DashboardFragment";
private FirestoreRecyclerAdapter<EventModel, EventViewHolder> adapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_page);
//Set recyclerView
recyclerView = findViewById(R.id.recyclerEventCard);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference cRef = db.collection("event");
Query query = db.collection("event").orderBy("ASC").limit(10);
FirestoreRecyclerOptions<EventModel> options = new FirestoreRecyclerOptions.Builder<EventModel>()
.setQuery(query, EventModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<EventModel, EventViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull EventViewHolder holder, int position, #NonNull EventModel model) {
holder.textViewEName.setText(model.getEName());
holder.textViewEDate.setText(model.getEDate());
holder.textViewEVenue.setText(model.getEVenue());
holder.textViewEDesc.setText(model.getEDesc());
}
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Changes on XML file. Try it.
View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.event_card_list, parent, false);
return new EventViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
EventModel.java
package com.example.myvolunteer;
public class EventModel {
private String EName;
private String EDate;
private String EVenue;
private String EDesc;
//Constructor
public EventModel(){}
public EventModel(String EName, String EDate, String EVenue, String EDesc){
this.EName = EName;
this.EDate = EDate;
this.EVenue = EVenue;
this.EDesc = EDesc;
}
public String getEName() {
return EName;
}
public void setEName(String EName) {
this.EName = EName;
}
public String getEDate() {
return EDate;
}
public void setEDate(String EDate) {
this.EDate = EDate;
}
public String getEVenue() {
return EVenue;
}
public void setEVenue(String EVenue) {
this.EVenue = EVenue;
}
public String getEDesc() {
return EDesc;
}
public void setEDesc(String EDesc) {
this.EDesc = EDesc;
}
}
EventViewHolder.java
package com.example.myvolunteer;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;
class EventViewHolder extends RecyclerView.ViewHolder{
TextView textViewEName, textViewEVenue, textViewEDate, textViewEDesc;
public EventViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
textViewEName = itemView.findViewById(R.id.textEventName);
textViewEDate = itemView.findViewById(R.id.textEventDate);
textViewEVenue = itemView.findViewById(R.id.textEventVenue);
textViewEDesc = itemView.findViewById(R.id.textEventDesc);
}
}
activity_event_page.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".EventPage">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerEventCard"
android:layout_width="351dp"
android:layout_height="663dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
event_card_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="250dp"
app:cardBackgroundColor="#B7F1FE"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="432dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textEventName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Event Name"
android:textColor="#color/black"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textEventDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Date"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/textEventVenue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Venue"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/textEventDesc"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Description"
android:textColor="#color/black"
android:textSize="20sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/createText"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Created By: "
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textEventCreatedBy"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Leader Name"
android:textColor="#color/black"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="#+id/createText"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="#adcae6"
android:text="More Info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
make Sure your model class String variables are equal to your firestore data feild because it is very important to retrieve data from firestore
private String eventName;
private String eventDate;
private String eventVenue;
private String eventDescription;
//Constructor
public EventModel(){}
public EventModel(String eventName, String eventDate, String eventVenue, String eventDescription){
this.eventName = eventName;
this.eventDate = eventDate;
this.eventVenue = eventVenue;
this.eventDescription = eventDescription;
}
public String geteventName() {
return eventName;
}
public void seteventName(String eventName) {
this.eventName = eventName;
}
public String geteventDate() {
return eventDate;
}
public void seteventDate(String eventDate) {
this.eventDate = eventDate;
}
public String geteventVenue() {
return eventVenue;
}
public void seteventVenue(String eventVenue) {
this.eventVenue = eventVenue;
}
public String geteventDescription() {
return eventDescription;
}
public void seteventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
This looks wrong:
Query query = db.collection("event").orderBy("ASC").limit(10);
You are telling Firestore to sort by a field called ASC, which doesn't exist in your screenshot, so no documents are returned.
You probably want to sort on the ascending date, which you can do with:
Query query = db.collection("event").orderBy("eventDate", Direction.ASCENDING).limit(10);
Note that while the above will fix the problem of the documents not showing, they will still show up in the wrong order. This is because your eventDate field is not useful for sorting dates.
Strings in Firestore are sorted lexicographically, and in that order, "12/7/2021" becomes before "13/6/2021".
If you want to fix this problem, you can either store the dates as Firestore's built-in Timestamp type, or use a string format that allows sorting, like "2021-07-12" (which is *after* "2021-07-13"`.
I am trying to incorporate a user model into my application while using data binding. And I am trying to get the user to sign up using firebase authentication email and password. But when my sign up button is clicked nothing happens.
I have a way for my login activity working using my own custom observable string class but I am just trying out a different method for this.
my user model:
public class User extends BaseObservable {
private String email;
private String password;
private String confPassword;
private String username;
#Bindable
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
notifyPropertyChanged(BR.email);
}
#Bindable
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.password);
}
#Bindable
public String getConfPassword() {
return confPassword;
}
public void setConfPassword(String confPassword) {
this.confPassword = confPassword;
notifyPropertyChanged(BR.confPassword);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
signUpViewModel:
public class SignUpViewModel extends ViewModel {
public void firebaseSignUp(String email, String password, String confPassword){
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confPassword)) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
if (password.length() > 6) {
if (password.equals(confPassword)) {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
} else {
}
}
});
}
}
}
}
}
}
signUpActivity:
public class SignUpActivity extends AppCompatActivity {
private SignUpViewModel signUpViewModel;
private FirebaseAuth.AuthStateListener authStateListener;
private User user = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivitySignUpBinding signUpBinding = DataBindingUtil.setContentView(this, R.layout.activity_sign_up);
signUpViewModel = new SignUpViewModel();
signUpBinding.setVModel(signUpViewModel);
signUpBinding.setActivity(this);
// signUpBinding.setUser(user);
signUpBinding.executePendingBindings();
}
public void signUp(){
signUpViewModel.firebaseSignUp(user.getEmail(), user.getPassword(), user.getConfPassword());
authStateListener = auth -> {
FirebaseUser firebaseUser = auth.getCurrentUser();
if (firebaseUser != null){
Intent sendToMain = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(sendToMain);
finish();
}
};
FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}
#Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
}
public void toSignIn(){
Intent toSignIn = new Intent(this, LoginActivity.class);
startActivity(toSignIn);
finish();
}
}
xml file with databindings:
<layout
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">
<data>
<variable name="vModel"
type="com.jthomann.cff_mvvm1.viewModel.SignUpViewModel"/>
<variable name="activity"
type="com.jthomann.cff_mvvm1.view.SignUpActivity"/>
<variable name="userModel"
type="com.jthomann.cff_mvvm1.model.User"/>
<import type="android.view.View"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/cff_bg_new"
tools:context="com.jthomann.cff_mvvm1.view.SignUpActivity"
tools:layout_editor_absoluteY="25dp">
<Button
android:id="#+id/signUpBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/sign_up_btn_new"
android:textAlignment="center"
android:textColor="#android:color/white"
android:onClick="#{() -> activity.signUp()}"
app:layout_constraintEnd_toEndOf="#+id/signUpConfPass"
app:layout_constraintStart_toStartOf="#+id/signUpConfPass"
app:layout_constraintTop_toBottomOf="#+id/signUpConfPass" />
<EditText
android:id="#+id/signUpEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="33dp"
android:ems="10"
android:inputType="textEmailAddress"
android:text="#={userModel.email}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/tvEmail"
app:layout_constraintTop_toBottomOf="#+id/tvEmail"
tools:ignore="Autofill,LabelFor" />
<TextView
android:id="#+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/email"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo" />
<TextView
android:id="#+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/password"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="#+id/signUpEmail"
app:layout_constraintTop_toBottomOf="#+id/signUpEmail" />
<TextView
android:id="#+id/tvConfPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/confPass"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="#+id/signUpPassword"
app:layout_constraintTop_toBottomOf="#+id/signUpPassword" />
<TextView
android:id="#+id/tvToSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/sign_in"
android:textColor="#color/colorOffWhite"
android:textSize="14sp"
android:onClick="#{() -> activity.toSignIn()}"
app:layout_constraintEnd_toEndOf="#+id/signUpBtn"
app:layout_constraintStart_toStartOf="#+id/signUpBtn"
app:layout_constraintTop_toBottomOf="#+id/signUpBtn" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:contentDescription="#null"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cff_logo_new" />
<EditText
android:id="#+id/signUpPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:labelFor="#id/signUpPassword"
android:text="#={userModel.password}"
app:layout_constraintEnd_toEndOf="#+id/signUpEmail"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/tvPassword"
app:layout_constraintTop_toBottomOf="#+id/tvPassword"
tools:ignore="Autofill" />
<EditText
android:id="#+id/signUpConfPass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:labelFor="#id/signUpConfPass"
android:inputType="textPassword"
android:text="#={userModel.confPassword}"
app:layout_constraintEnd_toEndOf="#+id/signUpPassword"
app:layout_constraintStart_toStartOf="#+id/tvConfPassword"
app:layout_constraintTop_toBottomOf="#+id/tvConfPassword"
tools:ignore="Autofill" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
well found the answer, I was just missing a line of code:
signUpBinding.setUser(user);
works after I added this in.
I created a Recyclerview in my main_activity and I wanted to use this cardview in a partial activity... I put extras variables and data to the new activity but any thing not showing in new activity...
this is my partial xml code (hotel_page.xml) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_view1"
android:layout_width="match_parent"
android:layout_height="288dp"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
card_view:cardCornerRadius="0dp"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card"
android:focusable="true"
android:contextClickable="true"
android:gravity="top"
android:layout_alignParentBottom="true">
</RelativeLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:paddingTop="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15dp"
android:layout_above="#+id/count1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/thumbnail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"
android:contextClickable="true"
android:layout_alignTop="#+id/title1" />
<TextView
android:id="#+id/count1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:textSize="12dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
This is My HotelPageList.java :
package ir.homa;
public class HotelPageList {
private String name;
private int numOfRooms;
private int thumbnail;
public HotelPageList() {
}
public HotelPageList(String name, int numOfRooms, int thumbnail) {
this.name = name;
this.numOfRooms = numOfRooms;
this.thumbnail = thumbnail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumOfRooms() {
return numOfRooms;
}
public void setNumOfRooms(int numOfRooms) {
this.numOfRooms = numOfRooms;
}
public int getThumbnail() {
return thumbnail;
}
public void setThumbnail(int thumbnail) {
this.thumbnail = thumbnail;
}
}
This is HotelPageAdapter.java :
package ir.homa;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* Created by SMQ on 7/20/2016.
*/
public class HotelPageAdapter extends RecyclerView.Adapter<HotelPageAdapter.MyViewHolder> {
private Context mContext;
private List<HotelPageList> hotelPage;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title1, count1;
public ImageView thumbnail1, overflow1;
public MyViewHolder(View view) {
super(view);
title1 = (TextView) view.findViewById(R.id.title1);
count1 = (TextView) view.findViewById(R.id.count1);
thumbnail1 = (ImageView) view.findViewById(R.id.thumbnail1);
overflow1 = (ImageView) view.findViewById(R.id.overflow);
}
}
public HotelPageAdapter(Context mContext, List<HotelPageList> hotelPage) {
this.mContext = mContext;
this.hotelPage = hotelPage;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hotel_page, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final HotelPageList hotel = hotelPage.get(position);
holder.title1.setText(hotel.getName());
holder.count1.setText(hotel.getNumOfRooms() + " اتاق");
// loading hotel cover using Glide library
Glide.with(mContext).load(hotel.getThumbnail()).into(holder.thumbnail1);
}
#Override
public int getItemCount() {return hotelPage.size();}
}
and this is result:
Where is problem ?
Try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="288dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view1"
android:layout_width="match_parent"
android:layout_height="288dp"
android:elevation="3dp"
card_view:cardCornerRadius="0dp"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card"
android:focusable="true"
android:contextClickable="true"
android:gravity="top"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:paddingTop="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15dp"
android:text="Title"
android:layout_above="#+id/count1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/thumbnail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#drawable/ic_launcher"
android:contextClickable="true"
android:layout_alignTop="#+id/title1" />
<TextView
android:id="#+id/count1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="25dp"
android:textSize="12dp"
android:text="sub title"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
I am new in android app developement. I tried to insert values to SQLite database through the code below ; and it is not working. I'm desperate!
Can anyone pleaaaaase help me???
this is DatabaseHelper.JAVA
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "agents.db";
private static final String TABLE_NAME = "agents";
private static final String COLUMN_ID = "id";
private static final String COLUMN_MATRICULE = "matricule";
private static final String COLUMN_NOM = "nom";
private static final String COLUMN_PRENOM = "prenom";
private static final String COLUMN_FONCTION = "fonction";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_CIN = "cin";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table agents (id integer primary key not null , " +
", matricule integer not null , nom text not null , prenom text not null , fonction text not null , email text not null , pass text not null , address text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertAgent(Agent a) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from agents";
Cursor cursor = db.rawQuery(query , null);
int count = cursor.getCount();
values.put(COLUMN_ID , count);
values.put(COLUMN_MATRICULE, a.getMatricule());
values.put(COLUMN_NOM, a.getNom());
values.put(COLUMN_PRENOM, a.getPrenom());
values.put(COLUMN_FONCTION, a.getFonction());
values.put(COLUMN_EMAIL, a.getEmail());
values.put(COLUMN_PASS, a.getPass());
values.put(COLUMN_ADDRESS, a.getAddress());
values.put(COLUMN_CIN, a.getCin());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String matricule)
{
db = this.getReadableDatabase();
String query = "select matricule, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do{
a = cursor.getString(0);
if(a.equals(matricule))
{
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
Agent.JAVA
public class Agent {
String matricule,nom,prenom,fonction,email,pass,address,cin;
public String getMatricule() {
return matricule;
}
public void setMatricule(String matricule) {
this.matricule = matricule;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getFonction() {
return fonction;
}
public void setFonction(String fonction) {
this.fonction = fonction;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
}
InscRire.JAVA
public class InscRire extends Activity {
DatabaseHelper helper = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sinscrire);
}
public void onSignUpClick(View v)
{
if(v.getId()== R.id.Bsinscrire)
{
EditText matricule = (EditText)findViewById(R.id.TFmatricule);
EditText nom = (EditText)findViewById(R.id.TFnom);
EditText prenom = (EditText)findViewById(R.id.TFprenom);
EditText fonction = (EditText)findViewById(R.id.TFfonction);
EditText email = (EditText)findViewById(R.id.TFemail);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
EditText pass2 = (EditText)findViewById(R.id.TFpass2);
EditText address = (EditText)findViewById(R.id.TFaddress);
EditText cin = (EditText)findViewById(R.id.TFcin);
String matriculestr = matricule.getText().toString();
String nomstr = nom.getText().toString();
String prenomstr = prenom.getText().toString();
String fonctionstr = fonction.getText().toString();
String emailstr = email.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
String addressstr = address.getText().toString();
String cinstr = cin.getText().toString();
if(!pass1str.equals(pass2str))
{
//popup msg
Toast pass = Toast.makeText(InscRire.this , "Mot de passe incorrect!" , Toast.LENGTH_SHORT);
pass.show();
}
else
{
//insert the detailes in database
Agent a = new Agent();
a.setMatricule(matriculestr);
a.setNom(nomstr);
a.setPrenom(prenomstr);
a.setFonction(fonctionstr);
a.setEmail(emailstr);
a.setPass(pass1str);
a.setAddress(addressstr);
a.setCin(cinstr);
helper.insertAgent(a);
}
}
}
}
Main.JAVA
public class MainActivity extends ActionBarActivity {
DatabaseHelper helper = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void onButtonClick(View v)
{
if(v.getId() == R.id.Bconnexion)
{
EditText a = (EditText)findViewById(R.id.TFmatricule);
String mat = a.getText().toString();
EditText b = (EditText)findViewById(R.id.TFpass);
String pass = b.getText().toString();
String mot_de_pass = helper.searchPass(mat);
if(pass.equals(mot_de_pass))
{
Intent i = new Intent(MainActivity.this, Display.class);
i.putExtra("Matricule",mat);
startActivity(i);
}
else
{
Toast temp = Toast.makeText(MainActivity.this , "Matricule et mot de passe incorrect!" , Toast.LENGTH_SHORT);
temp.show();
}
}
if(v.getId() == R.id.Binscrire)
{
Intent i = new Intent(MainActivity.this, InscRire.class);
startActivity(i);
}
}
#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);
}
}
Display.JAVA
public class Display extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
String matricule = getIntent().getStringExtra("Matricule");
TextView tv = (TextView)findViewById(R.id.TVmatricule);
tv.setText(matricule);
}
}
sinscrire.XML
<?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="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule "
android:id="#+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFmatricule"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nom"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFnom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Prenom"
android:id="#+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFprenom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fonction"
android:id="#+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="#+id/TFfonction" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Email"
android:id="#+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/TFemail" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="#+id/textView6" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Confirmer mot de passe"
android:id="#+id/textView7" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Address"
android:id="#+id/textView8" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="#+id/TFaddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cin"
android:id="#+id/textView9" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:ems="10"
android:id="#+id/TFcin" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sinscrire"
android:id="#+id/Bsinscrire"
android:onClick="onSignUpClick" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Main.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connexion"
android:id="#+id/Bconnexion"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TFmatricule"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="#+id/textView3"
android:layout_below="#+id/TFmatricule"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S'inscrire ici"
android:id="#+id/Binscrire"
android:layout_below="#+id/Bconnexion"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:onClick="onButtonClick" />
</RelativeLayout>
Display.XML
<?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="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome,"
android:id="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptyString"
android:id="#+id/TVmatricule" />
</LinearLayout>