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.
Related
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 currently running an activity that works fine on an emulator (Nexus 5X API 27), however when executing it on a device (Samsung S8 Plus); the linear layout grid does not appear. I've added the proper constraints but they still don't show.
Tried shifting the textviews to make more space for the Linear Layout grid but still no use.
A screenshot of how the activity should look like:
https://imgur.com/a/I2wEgZW
Here is the Activity Code:
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class Badges extends AppCompatActivity {
private static String TAG = "Badges.Class";
private static ImageView badge1,badge2, badge3, badge4, badge5, badge6, badge7, badge8, badge9, questionBadge;
private Button summaryButton;
public static int b1, b2, b3, b4, b5, b6, b7, b8, b9;
public static String currentUser;
public static int badgePoints;
public static TextView textPointsValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_badges);
//Creating Badge Icons and greying them out
badge1 = findViewById(R.id.imageBadge1);
badge2 = findViewById(R.id.imageBadge2);
badge3 = findViewById(R.id.imageBadge3);
badge4 = findViewById(R.id.imageBadge4);
badge5 = findViewById(R.id.imageBadge5);
badge6 = findViewById(R.id.imageBadge6);
badge7 = findViewById(R.id.imageBadge7);
badge8 = findViewById(R.id.imageBadge8);
badge9 = findViewById(R.id.imageBadge9);
badge1.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge2.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge3.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge4.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge5.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge6.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge7.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge8.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
badge9.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
//Creating Summary Button
summaryButton = findViewById(R.id.summaryButtonBadge);
summaryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Badges.this,BadgesSummary.class);
startActivity(myIntent);
}
});
//Create Back Button
ImageView backButton = findViewById(R.id.badgeBackButton);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Badges.this,Home.class);
startActivity(myIntent);
}
});
//Question Mark Button
//Have yet to set up logic for this
questionBadge = findViewById(R.id.questionButBadge);
questionBadge.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//******Add Logic in here*****
}
});
//Points text
textPointsValue = findViewById(R.id.textPointsValue);
Log.d(TAG, "onCreate: the value of current user is " + currentUser);
//Staged Scenarios
if(currentUser.equals("test")){
//badge1.setColorFilter(this.getResources().getColor(R.color.transparentDarken));
Log.d(TAG, "onCreate: Ran the currentUser test method, and the value of current " +
"user " + currentUser);
badgePoints = 0;
badge1.setColorFilter(null);
addPoints(50);
b1=1;
badge2.setColorFilter(null);
addPoints(200);
b2=1;
badge3.setColorFilter(null);
addPoints(300);
b3=1;
badge4.setColorFilter(null);
addPoints(50);
b4=1;
}
else if(currentUser == "gg"){
}
else if(currentUser == "jason"){
}
//Scenario one
}
//Public method to reset the values
public static void resetBadge (){
//This resets all badges.
//Should only be ran when a new user is created.
b1 = 0;
b2 = 0;
b3 = 0;
b4 = 0;
b5 = 0;
b6 = 0;
b7 = 0;
b8 = 0;
b9 = 0;
badgePoints=0;
textPointsValue.setText(String.valueOf(badgePoints));
Log.d(TAG, "resetBadge: Badges have been reset due to new user login");
}
public static void setCurrentUser(String user){
Badges.currentUser = user;
Log.d(TAG, "setCurrentUser: User has been updated " + user);
}
public static void addPoints(int value){
badgePoints = badgePoints + value;
textPointsValue.setText(String.valueOf(badgePoints));
}
public static void achieveBadge1(){
//Create Avatar and profile
if(b1 == 0){
badge1.setColorFilter(null);
addPoints(50);
b1=1;
}
}
public static void achieveBadge2(){
//Choose major
if(b2 == 0){
badge2.setColorFilter(null);
addPoints(200);
b2=1;
}
}
public static void achieveBadge3(){
//Finish Degree
if(b3 == 0){
badge3.setColorFilter(null);
addPoints(300);
b3=1;
}
}
public static void achieveBadge4(){
//Redeem a Reward
if(b4 == 0){
badge4.setColorFilter(null);
addPoints(50);
b4=1;
}
}
public static void achieveBadge5(){
//Log in 7 days
if(b5 == 0){
badge5.setColorFilter(null);
addPoints(100);
b5=1;
}
}
public static void achieveBadge6(){
//Earn 500 points
if(b6 == 0){
badge6.setColorFilter(null);
addPoints(100);
b6=1;
}
}
public static void achieveBadge7(){
//Reviewed a Course
if(b7 == 0){
badge7.setColorFilter(null);
addPoints(200);
b7=1;
}
}
public static void achieveBadge8(){
//Saved a course
if(b8 == 0){
badge8.setColorFilter(null);
addPoints(50);
b1=1;
}
}
public static void achieveBadge9(){
//Update a wam
if(b9 == 0){
badge9.setColorFilter(null);
addPoints(100);
b9=1;
}
}
}
Here is the XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".Badges">
<ImageView
android:id="#+id/badgeBackButton"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginStart="3dp"
android:layout_marginTop="29dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/back_arrow" />
<ImageView
android:id="#+id/questionButBadge"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/question" />
<TextView
android:id="#+id/textPointsBadge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginEnd="176dp"
android:text="Points"
android:textColor="#color/colorLightPurple"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textPointsValue" />
<TextView
android:id="#+id/textPointsValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginEnd="177dp"
android:text="650"
android:textColor="#color/colorLightPurple"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="#+id/textPointsBadge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView13" />
<TextView
android:id="#+id/badgesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="139dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="139dp"
android:gravity="center"
android:text="Badges"
android:textAlignment="center"
android:textColor="#color/colorLightPurple"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/summaryButtonBadge"
android:layout_width="161dp"
android:layout_height="48dp"
android:layout_marginStart="69dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="20dp"
android:background="#drawable/purple_square_button"
android:text="See Summary"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="17dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linIconGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="10dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textPointsBadge">
<LinearLayout
android:id="#+id/linTopRow"
android:layout_width="match_parent"
android:layout_height="114dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBadge1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge1" />
<ImageView
android:id="#+id/imageBadge2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge2" />
<ImageView
android:id="#+id/imageBadge3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge3" />
</LinearLayout>
<LinearLayout
android:id="#+id/linMidRow"
android:layout_width="match_parent"
android:layout_height="114dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBadge4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge4" />
<ImageView
android:id="#+id/imageBadge5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge5" />
<ImageView
android:id="#+id/imageBadge6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge6" />
</LinearLayout>
<LinearLayout
android:id="#+id/linBotRow"
android:layout_width="match_parent"
android:layout_height="114dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBadge7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge7" />
<ImageView
android:id="#+id/imageBadge8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge8" />
<ImageView
android:id="#+id/imageBadge9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="#drawable/badge9" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageView13"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="178dp"
android:layout_marginEnd="178dp"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="#+id/textPointsValue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/badgesText"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/onboarding2_logo2" />
</android.support.constraint.ConstraintLayout>
I think your Layout is exceeding the screen size but not scrolling. You have assigned fixed size for some the layout elements. Try enclosing the constraintLayout using a nestedScrollView.
But also note one thing:
<LinearLayout
android:id="#+id/linIconGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="10dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textPointsBadge">
why are using match_parent for a direct child of constraintLayout? If you want to use the full width set the width as 0dp and use these
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
I found that images being used were too large (in terms of capacity e.g. 7mb each). I made the images smaller in size and everything began working fine.
To summarize, I'm building a social media app that displays:
A Newsfeed that shows posts from all user profiles in a given account
A Timeline that shows posts from a specific user profile in the account
I've built a custom BaseAdapter to populate a custom cell within each ListView. The Newsfeed ListView (that populates posts from all users on the account) is populating correctly.
The Timeline ListView (that populates posts from one profile on the account) is not showing. I've set breakpoints to ensure that my ArrayList is not null or empty when populating the Timeline ListView. In addition, breakpoints verify that my custom adapter is, in fact, pulling data from the ArrayList and inflating cells. However, the ListView is simply not visible.
Here is the layout file for my Newsfeed fragment (that is working correctly):
<android.support.constraint.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:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/input_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:backgroundTint="#color/colorSkyPrimary"
android:ems="10"
android:gravity="start|top"
android:hint="#string/what_s_going_on"
android:importantForAutofill="no"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="o" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="#+id/input_post"
app:layout_constraintEnd_toEndOf="#+id/input_post"
app:srcCompat="#drawable/camera_icon" />
<Button
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="8dp"
android:text="#android:string/cancel"
android:textColor="#color/colorGrassPrimary"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="48dp"
android:backgroundTint="#color/colorGrassPrimary"
android:text="#string/update"
android:textColor="#color/colorButtonText"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="300dp"
android:contentDescription="#string/post_image"
android:scaleType="fitCenter"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input_post"
tools:srcCompat="#tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/picker_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.ConstraintLayout
android:id="#+id/picker_image"
android:layout_width="0dp"
android:layout_height="75dp"
android:background="#android:color/white"
android:elevation="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageButton
android:id="#+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/camera_icon_large" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.375"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
Here is the layout file for my Profile fragment (in which the ListView is not showing at runtime)
<android.support.constraint.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:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:src="#drawable/male_icon_large"
app:civ_border_color="#android:color/transparent"
app:civ_border_width="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/display_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/brelynn_mack"
android:textAlignment="viewStart"
android:textColor="#color/colorTextDark"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/button_delete_profile"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toTopOf="#+id/profile_photo" />
<TextView
android:id="#+id/display_timestamp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/may_14_2019_9_59_pm"
android:textAlignment="viewStart"
android:textColor="#color/colorTextLight"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_name" />
<TextView
android:id="#+id/display_last_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="#string/_8741_grouse_run_lane_28314"
android:textAlignment="viewStart"
android:textColor="#color/colorTextPrimary"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_timestamp" />
<ImageButton
android:id="#+id/button_delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#android:color/white"
android:contentDescription="#string/trash_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash_icon" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/camera_icon" />
<ImageButton
android:id="#+id/button_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="#android:color/white"
android:contentDescription="#string/family_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/family_icon_small" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_posts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
Here is my custom adapter:
public class NewsfeedAdapter extends BaseAdapter {
// Class properties
private static final String TAG = "NewsfeedAdapter";
public static final String EXTRA_POSTS = "extra_posts";
public static final String EXTRA_POSITION = "extra_position";
private final Context context;
ArrayList<Post> posts;
Account account;
// Constructor
public NewsfeedAdapter(Context context, ArrayList<Post> posts, Account account) {
this.context = context;
this.posts = posts;
this.account = account;
}
// System generated methods
#Override
public int getCount() {
return posts.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return posts.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Post post = posts.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.cell_newsfeed, null);
}
TextView profileNameDisplay = convertView.findViewById(R.id.display_profile_name);
String name = post.getPosterName() + " " + account.getFamilyName();
profileNameDisplay.setText(name);
TextView timestampDisplay = convertView.findViewById(R.id.display_timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy # hh:mm a", Locale.getDefault());
String timestamp = dateFormat.format(post.getTimeStamp());
timestampDisplay.setText(timestamp);
TextView postMessageDisplay = convertView.findViewById(R.id.display_post_message);
postMessageDisplay.setText(post.getPostMessage());
if (post.getHasImage()) {
AccountUtils.loadProfilePhoto(context, convertView, post.getPosterId());
}
else {
ImageView postImage = convertView.findViewById(R.id.display_post_image);
postImage.setVisibility(View.GONE);
}
PostUtils.loadPostImage(convertView, post.getPostId());
ImageButton deleteButton = convertView.findViewById(R.id.button_delete_post);
ImageButton editButton = convertView.findViewById(R.id.button_edit_post);
toggleButtons(post, editButton, deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(context.getString(R.string.delete_post));
alertBuilder.setMessage(context.getString(R.string.delete_post_message));
alertBuilder.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
posts.remove(position);
PostUtils.deletePost(context, post.getPostId(), post.getPosterId());
notifyDataSetChanged();
PostUtils.listenForNews(context);
}
});
alertBuilder.setNegativeButton(context.getString(R.string.cancel), null);
AlertDialog alert = alertBuilder.create();
alert.show();
}
});
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent editIntent = new Intent(context, EditPostActivity.class);
editIntent.putExtra(EXTRA_POSTS, posts);
editIntent.putExtra(EXTRA_POSITION, position);
context.startActivity(editIntent);
}
});
return convertView;
}
// Custom methods
private void toggleButtons(Post post, ImageButton editButton, ImageButton deleteButton) {
long twoMinutes = System.currentTimeMillis() - (2 * 60 * 1000);
long fiveMinutes = System.currentTimeMillis() - (5 * 60 * 1000);
if (post.getTimeStamp().getTime() < fiveMinutes) {
editButton.setVisibility(View.GONE);
}
else {
editButton.setVisibility(View.VISIBLE);
}
if (post.getTimeStamp().getTime() < twoMinutes) {
deleteButton.setVisibility(View.GONE);
}
else {
deleteButton.setVisibility(View.VISIBLE);
}
}
Here are the lifecycle methods from the Newsfeed fragment that load my data and set the adapter:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newsfeed, container, false);
PostUtils.listenForNews(getActivity());
mPosts = PostUtils.loadNewsfeed(getActivity());
mNewsfeed = view.findViewById(R.id.list_newsfeed);
mImagePicker = view.findViewById(R.id.picker_image);
mPhotoView = view.findViewById(R.id.post_image);
AccountUtils.listenForUpdates(getActivity());
setClickListener(view);
setFocusListener(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadNewsfeed(getActivity());
Account account = AccountUtils.loadAccount(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, account);
mNewsfeed.setAdapter(mNewsfeedAdapter);
}
Here are the lifecycle methods from my Profile fragment where the same functionality should work for a slightly different set of data:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
try {
if(getActivity().getIntent() != null && getActivity().getIntent().getAction().equals(FamilyProfileFragment.ACTION_EDIT_PROFILE)) {
mEditingSelf = false;
mIsParent = true;
mProfile = (Profile) getActivity().getIntent().getSerializableExtra(FamilyProfileFragment.EXTRA_PROFILE);
String selectedName = mProfile.getFirstName();
String loadedName = AccountUtils.loadProfile(getActivity()).getFirstName();
if(selectedName.equals(loadedName)) {
mEditingSelf = true;
}
}
else {
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
}
catch (Exception e) {
e.printStackTrace();
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
mAccount = AccountUtils.loadAccount(getActivity());
AccountUtils.loadProfilePhoto(getActivity(), view, mProfile.getProfileId());
mPhotoView = view.findViewById(R.id.profile_photo);
PostUtils.listenForTimeline(getActivity(), mProfile);
mPosts = PostUtils.loadTimeline(getActivity());
mTimeline = view.findViewById(R.id.list_posts);
setClickListener(view);
setTextDisplay(view);
populateProfile(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadTimeline(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, mAccount);
mTimeline.setAdapter(mNewsfeedAdapter);
}
My constraints were set up incorrectly in the Profile fragment. Updated the XML to the following:
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
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 trying to implement a list of Card views, and the list is fine, but the layout inside the card is nothing like what i have defined in the XML. Any ideas why this is not showing properly? Here is my card XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cvAdvertFavourite"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
card_view:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
>
<ImageView
android:id="#+id/ivCardFavouriteAnimalPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp"
/>
<TextView
android:id="#+id/tvCardFavouriteTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/ivCardFavouriteAnimalPhoto"
android:textSize="30sp"
/>
<TextView
android:id="#+id/tvCardFavouriteRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCardFavouriteTitle"
android:layout_toRightOf="#+id/ivCardFavouriteAnimalPhoto"
/>
<TextView
android:id="#+id/tvCardFavouritePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCardFavouriteRace"
android:layout_toRightOf="#+id/ivCardFavouriteAnimalPhoto"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I use this RecyclerView 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="wrap_content"
android:padding="16dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvCardFavourite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is my CardItem class:
public class FavouriteCardItem {
Bitmap animal;
int price;
String title, race;
public FavouriteCardItem(Bitmap animal, int price, String title, String race){
this.animal = animal;
this.price = price;
this.title = title;
this.race = race;
}
public Bitmap getAnimal() {
return animal;
}
public void setAnimal(Bitmap animal) {
this.animal = animal;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
}
Here is how I call my adapter and set the data for the adapter:
private void loadSetData() {
List<FavouriteCardItem> items = new ArrayList<FavouriteCardItem>();
Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.bird);
FavouriteCardItem item1 = new FavouriteCardItem(bitmap, 100, "birds", "birds");
items.add(item1);
Bitmap bitmap2 = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.dog);
FavouriteCardItem item2 = new FavouriteCardItem(bitmap2, 200, "dogs", "dogs");
items.add(item2);
Bitmap bitmap3 = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.cat);
FavouriteCardItem item3 = new FavouriteCardItem(bitmap3, 1000, "cats", "cats");
items.add(item3);
FavouriteCardAdapter adapter = new FavouriteCardAdapter(getActivity(), R.layout.card_view, items);
recyclerView.setAdapter(adapter);
}
And this is my Adapter class:
public class FavouriteCardAdapter extends RecyclerView.Adapter<FavouriteCardAdapter.CardViewHolder> {
private Context mContext;
private int resourceId;
List<FavouriteCardItem> cardItems;
public FavouriteCardAdapter(Context context, int resource, List<FavouriteCardItem> cardItems){
mContext = context;
resourceId = resource;
this.cardItems = cardItems;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
CardViewHolder cvh = new CardViewHolder(v);
return cvh;
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
int screenHeight = metrics.heightPixels;
holder.cardView.getLayoutParams().height = screenHeight / 4;
int price = cardItems.get(position).getPrice();
String price1 = Integer.toString(price);
holder.tvPrice.setText(price1);
holder.tvRace.setText(cardItems.get(position).getRace());
holder.tvTitle.setText(cardItems.get(position).getTitle());
holder.ivAnimal.setImageBitmap(cardItems.get(position).getAnimal());
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return cardItems.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
ImageView ivAnimal;
TextView tvPrice, tvTitle, tvRace;
public CardViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cvAdvertFavourite);
ivAnimal = (ImageView) itemView.findViewById(R.id.ivCardFavouriteAnimalPhoto);
tvPrice = (TextView) itemView.findViewById(R.id.tvCardFavouritePrice);
tvTitle = (TextView) itemView.findViewById(R.id.tvCardFavouriteTitle);
tvRace = (TextView) itemView.findViewById(R.id.tvCardFavouriteRace);
}
}
}
BTW I am aware that I am not supposed to load images like this and so on, but I just wanted to create some test data to get the layout right before i start loading the data from the online data storage.
Here is how the whole Card view looks like. It should have 3 textViews and 1 imageView, but it only shows one imageView.....
You can try this as one of the solution if you do not want to specify the dimension for width and height of Image
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cvAdvertFavourite"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
>
<ImageView
android:id="#+id/ivCardFavouriteAnimalPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:weight = "2"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weight = "1" />
<TextView
android:id="#+id/tvCardFavouriteTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="20sp"
/>
<TextView
android:id="#+id/tvCardFavouriteRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCardFavouriteTitle"
/>
<TextView
android:id="#+id/tvCardFavouritePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCardFavouriteRace"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
In this I have used LinearLayout which will distribute the screen width in the ratio 2:1 between ImageView and RelativeLayout with text.
Also I have reduced the size of text from 30sp to 20sp
try to make your card xml look like this:
<android.support.v7.widget.CardView
android:id="#+id/cvAdvertFavourite"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
card_view:cardUseCompatPadding="true"
>
<RelativeLayout
android:id="#+id/something"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
>
<ImageView
android:id="#+id/ivCardFavouriteAnimalPhoto"
android:layout_width="#dimen/your_size_of_image" // the important part
android:layout_height="#dimen/your_size_of_image" // the important part
android:layout_centerVertical="true"
android:scaleType="centerCrop"/>
<LinearLayout // make sure you are using a linearlyout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/ivCardFavouriteAnimalPhoto"
android:layout_toRightOf="#id/ivCardFavouriteAnimalPhoto"
android:orientation="vertical">
<TextView
android:id="#+id/tvCardFavouriteTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<TextView
android:id="#+id/tvCardFavouriteRace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<TextView
android:id="#+id/tvCardFavouritePrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>