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"`.
Related
I am making an app that let users create meetings and I would like the details of the meeting (eg name, type, location) in a recycler view.I ran the php code by itself and was able to get the values from the database.
Currently, I am getting an error of
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at in recyclerviewAdapter
holder.Name.setText(users.getName());
holder.Type.setText(users.getType());
holder.Location.setText(users.getLocation());
which is linked to
public class Users {
private String name,type,location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
recyclerviewAdapter
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
Context context;
List<Users> usersList;
public UserAdapter(Context context, List<Users> usersList) {
this.context = context;
this.usersList = usersList;
}
#NonNull
#Override
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
return new UserHolder(userLayout);
}
#Override
public void onBindViewHolder(#NonNull UserHolder holder, int position) {
Users users=usersList.get(position);
holder.Name.setText(users.getName());
holder.Type.setText(users.getType());
holder.Location.setText(users.getLocation());
}
#Override
public int getItemCount() {
return usersList.size();
}
public class UserHolder extends RecyclerView.ViewHolder{
TextView Name,Type,Location;
public UserHolder(#NonNull View itemView){
super(itemView);
Name=itemView.findViewById(R.id.textTitle);
Type=itemView.findViewById(R.id.textType);
Location=itemView.findViewById(R.id.textLocation);
}
}
}
main activity code
public class MainActivity2 extends AppCompatActivity {
private static final String URL = "http://10.0.2.2/mad/activitydisplay.php";
RecyclerView recyclerView;
UserAdapter userAdapter;
List<Users> usersList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView = findViewById(R.id.recylcerList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
usersList=new ArrayList<>();
LoadAllUser();
}
private void LoadAllUser() {
JsonArrayRequest request =new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray array) {
for (int i=0;i<array.length();i++){
try {
JSONObject object=array.getJSONObject(i);
String name=object.getString("eventname").trim();
String type=object.getString("type").trim();
String location=object.getString("location").trim();
Users user= new Users();
user.setName(name);
user.setType(type);
user.setLocation(location);
usersList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
userAdapter=new UserAdapter(MainActivity2.this,usersList);
recyclerView.setAdapter(userAdapter);
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue=Volley.newRequestQueue(MainActivity2.this);
requestQueue.add((request));
}
}
php code to get data from table
$sql="SELECT * FROM `activitytable` ";
$result=mysqli_query($conn,$sql);
$user=array();
while($row = mysqli_fetch_assoc($result)){
$index['eventname']=$row['eventname'];
$index['type']=$row['type'];
$index['location']=$row['location'];
array_push($user, $index);
}
echo json_encode($user);
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity2">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recylcerList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_list resource file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textTitle"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textType"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textLocation"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
instead of
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
return new UserHolder(userLayout);
}
use this
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list ,parent,false);
return new UserHolder(userLayout);
}
because activity_main2 doesn't contain R.id.textTitle,R.id.textType,R.id.textLocation
check your xml(R.layout.activity_main2) looks like id of your view are from another layout
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.
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
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>