This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 3 years ago.
RecyclerView isn't showed without setLayoutManager, but setLayoutManager makes my program crash. This method was demonstrated in all of the tutorials I have watched. Yeah, I know, there is too much code with a lot of garbage, but the error can be anywhere, so I hope you can help me(
StoreInfoPage.java (get it just like the MainActivity)
public class StoreInfoPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.store_info_page);
RecyclerView lastMonthSellsList = findViewById(R.id.last_month_sells_list);
ArrayList<Product> products = new ArrayList<>();
products.add(new Product(4, 5, true, true, 16000, 12000));
products.add(new Product(4, 5, true, true, 16000, 12000));
StoreSellingAdapter adapter = new StoreSellingAdapter(this, products);
lastMonthSellsList.setAdapter(adapter);
lastMonthSellsList.setLayoutManager(new LinearLayoutManager(this));
}
}
store_info_page.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StoreInfoPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView41"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold|italic" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:scaleType="fitEnd"
app:srcCompat="#android:drawable/ic_menu_info_details" />
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginBottom="10dp"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="Кофе 3в1"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editText5"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="#font/roboto_slab_bold"
android:inputType="number"
android:textAlignment="center" />
<TextView
android:id="#+id/textView17"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="Чёрное Кофе"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editText6"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="#font/roboto_slab_bold"
android:inputType="number"
android:textAlignment="center" />
<TextView
android:id="#+id/textView18"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<Switch
android:id="#+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showText="true"
android:text="ДОЛГ"
android:textAlignment="center"
android:textSize="24sp" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="Добавить"
android:textSize="24sp" />
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="#+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="ПРОШЛЫЙ МЕСЯЦ"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/last_month_sells_list"
/>
</LinearLayout>
store_sell_list_table_row.xml (Adapter xml for RecyclerView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:id="#+id/sells_list_adapter_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/whiteCoffeeAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="Кофе 3в1"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/whiteCoffeeQuantityAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="#+id/whiteCoffeePriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="#+id/whiteCoffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/blackCoffeeAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:text="Чёрное Кофе"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/blackCoffeeQuantityAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="#+id/blackCoffeePriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="#+id/blackCoffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="#+id/textView39"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/coffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/status_text_view_adapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ОПЛАЧЕНО"
android:textAlignment="center"
android:textStyle="bold" />
</LinearLayout>
StoreSellingAdapter (Adapter class)
public class StoreSellingAdapter extends RecyclerView.Adapter<StoreSellingAdapter.ViewHolder>{
private ArrayList<Product> products;
private Context context;
StoreSellingAdapter(Context context, ArrayList<Product> products) {
this.products = products;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.store_sell_list_table_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Product product = products.get(position);
holder.whiteCoffeeQuantity.setText(product.getWhiteCoffeeQuantity());
holder.blackCoffeeQuantity.setText(product.getBlackCoffeeQuantity());
holder.whiteCoffeePrice.setText(product.getWhiteCoffeePrice());
holder.blackCoffeePrice.setText(product.getBlackCoffeePrice());
holder.whiteCoffeeTotalPrice.setText(product.getWhiteCoffeeTotalPrice());
holder.blackCoffeeTotalPrice.setText(product.getBlackCoffeeTotalPrice());
holder.coffeeTotalPrice.setText(product.getCoffeeTotalPrice());
if(product.isDebt() && product.isPaid()) {
holder.statusTextViewAdapter.setText("ДОЛГ(ОПЛАЧЕНО)");
holder.parentLayout.setBackgroundColor(Color.parseColor("#ef5350"));
}
}
#Override
public int getItemCount() {
return products.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView whiteCoffeeQuantity;
TextView blackCoffeeQuantity;
TextView whiteCoffeePrice;
TextView blackCoffeePrice;
TextView whiteCoffeeTotalPrice;
TextView blackCoffeeTotalPrice;
TextView coffeeTotalPrice;
TextView statusTextViewAdapter;
LinearLayout parentLayout;
ViewHolder(#NonNull View itemView) {
super(itemView);
whiteCoffeeQuantity = itemView.findViewById(R.id.whiteCoffeeQuantityAdapter);
blackCoffeeQuantity = itemView.findViewById(R.id.blackCoffeeQuantityAdapter);
whiteCoffeePrice = itemView.findViewById(R.id.whiteCoffeePriceAdapter);
blackCoffeePrice = itemView.findViewById(R.id.blackCoffeePriceAdapter);
whiteCoffeeTotalPrice = itemView.findViewById(R.id.whiteCoffeeTotalPriceAdapter);
blackCoffeeTotalPrice = itemView.findViewById(R.id.blackCoffeeTotalPriceAdapter);
coffeeTotalPrice = itemView.findViewById(R.id.coffeeTotalPriceAdapter);
parentLayout = itemView.findViewById(R.id.sells_list_adapter_layout);
statusTextViewAdapter = itemView.findViewById(R.id.status_text_view_adapter);
}
}
}
Product.java
public class Product {
private int whiteCoffeeQuantity;
private int blackCoffeeQuantity;
private boolean isDebt;
private boolean isPaid;
private int whiteCoffeePrice;
private int blackCoffeePrice;
private int whiteCoffeeTotalPrice;
private int blackCoffeeTotalPrice;
private int coffeeTotalPrice;
public Product(int whiteCoffeeQuantity, int blackCoffeeQuantity, boolean isDebt, boolean isPaid, int whiteCoffeePrice, int blackCoffeePrice) {
this.whiteCoffeeQuantity = whiteCoffeeQuantity;
this.blackCoffeeQuantity = blackCoffeeQuantity;
this.isDebt = isDebt;
this.isPaid = isPaid;
this.whiteCoffeePrice = whiteCoffeePrice;
this.blackCoffeePrice = blackCoffeePrice;
whiteCoffeeTotalPrice = whiteCoffeeQuantity*whiteCoffeePrice;
blackCoffeeTotalPrice = blackCoffeeQuantity*blackCoffeePrice;
coffeeTotalPrice = whiteCoffeeTotalPrice+blackCoffeeTotalPrice;
}
public int getWhiteCoffeeQuantity() {
return whiteCoffeeQuantity;
}
public void setWhiteCoffeeQuantity(int whiteCoffeeQuantity) {
this.whiteCoffeeQuantity = whiteCoffeeQuantity;
}
public int getBlackCoffeeQuantity() {
return blackCoffeeQuantity;
}
public void setBlackCoffeeQuantity(int blackCoffeeQuantity) {
this.blackCoffeeQuantity = blackCoffeeQuantity;
}
public boolean isDebt() {
return isDebt;
}
public void setDebt(boolean debt) {
isDebt = debt;
}
public boolean isPaid() {
return isPaid;
}
public void setPaid(boolean paid) {
isPaid = paid;
}
public int getWhiteCoffeePrice() {
return whiteCoffeePrice;
}
public void setWhiteCoffeePrice(int whiteCoffeePrice) {
this.whiteCoffeePrice = whiteCoffeePrice;
}
public int getBlackCoffeePrice() {
return blackCoffeePrice;
}
public void setBlackCoffeePrice(int blackCoffeePrice) {
this.blackCoffeePrice = blackCoffeePrice;
}
public int getWhiteCoffeeTotalPrice() {
return whiteCoffeeTotalPrice;
}
public void setWhiteCoffeeTotalPrice(int whiteCoffeeTotalPrice) {
this.whiteCoffeeTotalPrice = whiteCoffeeTotalPrice;
}
public int getBlackCoffeeTotalPrice() {
return blackCoffeeTotalPrice;
}
public void setBlackCoffeeTotalPrice(int blackCoffeeTotalPrice) {
this.blackCoffeeTotalPrice = blackCoffeeTotalPrice;
}
public int getCoffeeTotalPrice() {
return coffeeTotalPrice;
}
public void setCoffeeTotalPrice(int coffeeTotalPrice) {
this.coffeeTotalPrice = coffeeTotalPrice;
}
}
I really don't know you!
Everything is because of TextView.setText(Int).
Just replace Int with String.valueOf(Int).
Related
View (or even child elements of view) doesn't updates but copies previous item in RecyclerView.
I have layout with tree transaction_status_tree.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dotConfirmed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/non_filled_progress_dot"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvConfirmed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BflSansTextViewStyle"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/confirmed"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/progress_line"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvConfirmedInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BflSansTextViewStyle"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/not_yet"
android:textSize="#dimen/txt_small_size"
android:textStyle="italic"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dotPickedCashier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/non_filled_progress_dot"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvPickedCashier"
style="#style/BflSansTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/picked_by_cashier"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/progress_line"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvPickedCashierInfo"
style="#style/BflSansTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/not_yet"
android:textSize="#dimen/txt_small_size"
android:textStyle="italic"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:id="#+id/trTransferred"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dotTransferred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/non_filled_progress_dot"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvTransferred"
style="#style/BflSansTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/transferred"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:id="#+id/trTransferredInfo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/progress_line"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvTransferredInfo"
style="#style/BflSansTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/not_yet"
android:textSize="#dimen/txt_small_size"
android:textStyle="italic"
android:visibility="visible"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dotSubmitted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/filled_progress_dot"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvSubmitted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BflSansTextViewStyle"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/submitted"
android:textStyle="bold"
tools:ignore="RtlHardcoded" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_gravity="center"
app:srcCompat="#drawable/filled_progress_dot"
tools:ignore="ContentDescription" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvSubmittedInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BflSansTextViewStyle"
android:layout_marginLeft="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="#string/not_yet"
android:textSize="#dimen/txt_small_size"
android:textStyle="italic"
tools:ignore="RtlHardcoded" />
</TableRow>
</TableLayout>
</FrameLayout>
And I am using it inside my card (item of list) card_transaction_item.xml with <include> tag:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- View to resolve problem with GONE first item in SwipeRefreshLayout -->
<View
android:layout_width="match_parent"
android:layout_height="1dp" />
<android.support.v7.widget.CardView
android:id="#+id/cvTh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/default_min_divider"
app:cardCornerRadius="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="UselessParent">
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvIdTh"
style="#style/BflTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:layout_marginTop="#dimen/default_material_margin"
android:text="Transaction number"
android:textSize="#dimen/txt_card_title_size"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvCodeTh"
style="#style/BflSansTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:text="BFL[xxxxxxxxxx]"
android:textSize="#dimen/txt_tiny_size"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:layout_marginTop="#dimen/default_min_divider"
android:orientation="horizontal">
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvThTimeStamp"
style="#style/BflSansTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/default_material_margin"
android:layout_marginRight="#dimen/default_material_margin"
android:layout_weight="1"
android:text="00-00-0000 00:00"
tools:ignore="HardcodedText,RtlHardcoded" />
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvThMoney"
style="#style/BflSansTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="$0000"
android:textAlignment="viewEnd"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:id="#+id/llExpandTree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/default_min_divider"
android:layout_marginTop="#dimen/default_min_divider"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingTop="20dp">
<com.bfl.delivery.customviews.BflTextView
android:id="#+id/tvCntStatusTh"
style="#style/BflSansSemiBoldTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="#dimen/default_material_margin"
android:layout_weight="1"
android:ellipsize="end"
android:text="Current status"
tools:ignore="HardcodedText,InefficientWeight" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<ImageView
android:id="#+id/ivExpandTh"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="#dimen/default_material_margin"
app:srcCompat="#drawable/expand"
tools:ignore="ContentDescription" />
</FrameLayout>
</LinearLayout>
<include
android:id="#+id/statusTreeTh"
layout="#layout/transaction_status_tree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/default_material_margin"
android:layout_marginEnd="#dimen/default_material_margin"
android:layout_marginStart="#dimen/default_material_margin"
android:visibility="gone" />
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
So, when I try to refresh list with new items, all views (e.g. all TextView) updates successfully except this tree. The tree becomes a copy of tree in previous card (item) in RecyclerView.
I have such adapter:
public class TransactionsCardAdapter extends RecyclerView.Adapter<TransactionsCardAdapter.TcaViewHolder> {
private Context mContext;
private List<TransactionCardData> cardsData;
public TransactionsCardAdapter (Context mContext, List<TransactionCardData> cardsData) {
this.mContext = mContext;
this.cardsData = cardsData;
}
class TcaViewHolder extends RecyclerView.ViewHolder {
TextView tvNumber;
TextView tvCode;
TextView tvTime;
TextView tvMoney;
TextView tvCntStatus;
ImageView ivExpand;
CardView cv;
View tree;
ImageView ivSubmitted;
TextView tvSubmitted;
TextView tvSubmittedDate;
ImageView ivTransferred;
TextView tvTransferred;
TextView tvTransferredDate;
ImageView ivPickedCashier;
TextView tvPickedCashier;
TextView tvPickedCashierDate;
ImageView ivConfirmed;
TextView tvConfirmed;
TextView tvConfirmedDate;
View expandView;
View trTransferred;
View trTransferredInfo;
TcaViewHolder(View v) {
super(v);
tvNumber = v.findViewById(R.id.tvIdTh);
tvCode = v.findViewById(R.id.tvCodeTh);
tvTime = v.findViewById(R.id.tvThTimeStamp);
tvMoney = v.findViewById(R.id.tvThMoney);
tvCntStatus = v.findViewById(R.id.tvCntStatusTh);
ivExpand = v.findViewById(R.id.ivExpandTh);
cv = v.findViewById(R.id.cvTh);
tree = v.findViewById(R.id.statusTreeTh);
tvSubmitted = tree.findViewById(R.id.tvSubmitted);
tvSubmittedDate = tree.findViewById(R.id.tvSubmittedInfo);
ivSubmitted = tree.findViewById(R.id.dotSubmitted);
tvTransferred = tree.findViewById(R.id.tvTransferred);
tvTransferredDate = tree.findViewById(R.id.tvTransferredInfo);
ivTransferred = tree.findViewById(R.id.dotTransferred);
ivPickedCashier = tree.findViewById(R.id.dotPickedCashier);
tvPickedCashier = tree.findViewById(R.id.tvPickedCashier);
tvPickedCashierDate = tree.findViewById(R.id.tvPickedCashierInfo);
tvConfirmed = tree.findViewById(R.id.tvConfirmed);
tvConfirmedDate = tree.findViewById(R.id.tvConfirmedInfo);
ivConfirmed = tree.findViewById(R.id.dotConfirmed);
expandView = v.findViewById(R.id.llExpandTree);
trTransferred = v.findViewById(R.id.trTransferred);
trTransferredInfo = v.findViewById(R.id.trTransferredInfo);
}
}
#NonNull
#Override
public TcaViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_transaction_item, parent, false);
return new TransactionsCardAdapter.TcaViewHolder(itemView);
}
#SuppressLint("DefaultLocale")
#Override
public void onBindViewHolder(#NonNull final TcaViewHolder holder, int position) {
final TransactionCardData item = cardsData.get(position);
String formattedId = String.format("%012d", item.getId());
holder.tvNumber.setText(formattedId);
holder.tvCode.setText(item.getPackageId());
String amount = String.valueOf(item.getMoney()) + " " + mContext.getString(R.string.uah);
holder.tvMoney.setText(amount);
String cntStatus;
/* Setting current status in title */
if (item.getStatusConfirmed() != null) {
cntStatus = mContext.getResources().getString(R.string.confirmed);
} else if (item.getStatusPickedCashier() != null) {
cntStatus = mContext.getResources().getString(R.string.picked_by_cashier);
} else if (item.getStatusTransferred() != null) {
cntStatus = mContext.getResources().getString(R.string.transferred);
} else {
cntStatus = mContext.getResources().getString(R.string.submitted);
}
/* Making dots */
if (item.getStatusConfirmed() != null) {
makeConfirmed(item.getStatusConfirmed(), holder);
}
if (item.getStatusTransferred() != null) {
makeTransferred(item.getStatusTransferred(), holder);
}
if (item.getStatusPickedCashier() != null) {
makePickedCashier(item.getStatusPickedCashier(), holder);
}
if (item.getStatusSubmitted() != null) {
makeSubmitted(item.getStatusSubmitted(), holder);
}
/* Hiding dot if necessary */
if (item.getStatusTransferred() == null && item.getStatusPickedCashier() != null) {
holder.trTransferred.setVisibility(View.GONE);
holder.trTransferredInfo.setVisibility(View.GONE);
}
holder.tvCntStatus.setText(cntStatus);
holder.tvTime.setText(item.getTimeSubmitted());
holder.expandView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandOrCollapse(holder);
}
});
}
private void makeSubmitted (String dateTime, TcaViewHolder holder) {
holder.tvSubmittedDate.setText(dateTime);
holder.tvSubmitted.setTypeface(null, Typeface.BOLD);
holder.ivSubmitted.setImageDrawable(
ContextCompat.getDrawable(mContext, R.drawable.filled_progress_dot)
);
}
private void makePickedCashier (String dateTime, TcaViewHolder holder) {
holder.tvPickedCashierDate.setText(dateTime);
holder.tvPickedCashier.setTypeface(null, Typeface.BOLD);
holder.ivPickedCashier.setImageDrawable(
ContextCompat.getDrawable(mContext, R.drawable.filled_progress_dot)
);
}
private void makeTransferred (String dateTime, TcaViewHolder holder) {
holder.tvTransferredDate.setText(dateTime);
holder.tvTransferred.setTypeface(null, Typeface.BOLD);
holder.ivTransferred.setImageDrawable(
ContextCompat.getDrawable(mContext, R.drawable.filled_progress_dot)
);
}
private void makeConfirmed (String dateTime, TcaViewHolder holder) {
holder.tvConfirmedDate.setText(dateTime);
holder.tvConfirmed.setTypeface(null, Typeface.BOLD);
holder.ivConfirmed.setImageDrawable(
ContextCompat.getDrawable(mContext, R.drawable.filled_progress_dot)
);
}
private void expandOrCollapse (#NonNull final TcaViewHolder holder) {
long animDuration = 100;
if (holder.tree.getVisibility() == View.GONE) {
holder.tree.setVisibility(View.VISIBLE);
holder.ivExpand.animate().rotation(180).setDuration(animDuration);
} else {
holder.tree.setVisibility(View.GONE);
holder.ivExpand.animate().rotation(0).setDuration(animDuration);
}
}
#Override
public int getItemCount() {
return cardsData.size();
}
public void updateData (List<TransactionCardData> nData) {
cardsData.clear();
cardsData.addAll(nData);
notifyDataSetChanged();
}
}
And I am using updateData (List<TransactionCardData> nData); to update my list.
Please, explain me, what I am doing wrong and why this problem happens.
RecyclerView reuses existing views instead of inflating them again. All the fields which are not explicitly updated will hence be copied from some other view. If the child is not being explicitly hidden or updated (like when they are null) old values will be used. So, you will have to add an else case for every child view. Like if some value is null hide it in the else case.
if (item.getStatusConfirmed() != null) {
makeConfirmed(item.getStatusConfirmed(), holder);
} else {
view.setVisibilty(View.GONE);
}
hi I am trying to display data in a recyclerview in android by parsing a json from mysql database. But it keeps displaying just some of the data and I can't seem to find the problem.Any help will be greatly appreciated.
This is the adapter:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
List<Course> data;
private Context context;
public Adapter(Context context,List<Course> data){
this.data=data;
this.context=context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
DecimalFormat form = new DecimalFormat("0.00");
Course current=data.get(position);
holder.tCoursecode.setText(current.getmCoursecode());
holder.tCoursetitle.setText(current.getmCoursetitle());
holder.tCredit.setText(String.valueOf(current.getmCredit()));
holder.tMarks.setText(form.format (current.getmMarks()));
holder.tGrade.setText(current.getmGrade());
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tCoursecode;
TextView tCoursetitle;
TextView tCredit;
TextView tMarks;
TextView tGrade;
public MyViewHolder(View itemView) {
super(itemView);
tCoursecode=itemView.findViewById(R.id.tcc);
tCoursetitle=itemView.findViewById(R.id.tct);
tCredit=itemView.findViewById(R.id.tc);
tMarks=itemView.findViewById(R.id.tm);
tGrade=itemView.findViewById(R.id.tg);
}
}
this the layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/right">
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="CSC 460" />
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text= "Information Security"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:padding="25dp"
android:textSize="18sp"
tools:text="1"/>
<TextView
android:id="#+id/tm"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="78.4"/>
<TextView
android:id="#+id/tg"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="A"/>
</LinearLayout>
Screenshot:the grade "A" is not displaying
Screenshot
try this way
<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="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="16dp"
>
<LinearLayout
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="CSC 460"
/>
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Information Security"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp"
tools:text="1"
/>
<TextView
android:id="#+id/tm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="78.4"
/>
<TextView
android:id="#+id/tg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="A"
/>
</LinearLayout>
</LinearLayout>
hope this helps
I am trying to implement table layout in android.I have table Header and row designed in one xml file and am passing data using adapter.So What is happening is I got scrolling horizontally but with multiple table header repeating on each row.
What I want to achieve is table header should be seen only once and all the rows below it should scroll along with table header.
Here's my xml file
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
And here's the image what it looks likescrolling table
UPDATE (2) HERE's Adapter Code
public class Flight_cancel_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Flight_cancel_Details> rowItems;
public static int available_seats;
public static PolicyAdapter adapter;
int TOTAL;
public static ArrayList<String> arraySeat=new ArrayList<String>();
Flight_cancel_Adapter(Context context, ArrayList<Flight_cancel_Details> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView type;
TextView fare;
TextView tax;
TextView air_pnr;
TextView ticket;
TextView namee;
CheckBox box;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.test, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.namee = (TextView) convertView.findViewById(R.id.name);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.fare = (TextView) convertView.findViewById(R.id.fare);
holder.tax = (TextView) convertView.findViewById(R.id.tax);
holder.air_pnr = (TextView) convertView.findViewById(R.id.air_pnr);
holder.ticket = (TextView) convertView.findViewById(R.id.ticket);
holder.box = (CheckBox) convertView.findViewById(R.id.check_bx);
holder.box.setTag(position);
try
{
final Flight_cancel_Details row_pos = rowItems.get(position);
String name=String.valueOf(row_pos.getName());
String typ=String.valueOf(row_pos.gettypes());
String far=String.valueOf(row_pos.getfares());
String tax=String.valueOf(row_pos.gettax());
String pnr=String.valueOf(row_pos.getair_pnr());
String ticket=String.valueOf(row_pos.getticket());
System.out.print("ROW POS-"+row_pos.getStatus());
if(row_pos.getStatus().equals("CANCELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(false);
holder.box.setText("CANCELLED");
}else {
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(true);
holder.box.setText("CANCEL");
}
if(row_pos.getTarvel_Status().equals("TRAVELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
}
}
catch (Exception e)
{
Log.e("BUS_CANCEL_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
int position = (int) buttonView.getTag();
System.out.println("--CLICKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--VALUE--" + contactId);
arraySeat.add(contactId);
}
else
{
int position = (int) buttonView.getTag();
System.out.println("--UNCHEKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--UNCHEKED VALUE--" + contactId);
arraySeat.remove(contactId);
}
}
});
return convertView;
}
}
Use Recyclerview for horizontal scrollview for a better performance
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
</LinearLayout>
recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:listitem="#layout/layout">
</android.support.v7.widget.RecyclerView>
Here my XML file, you should put title for all row TableView (TextView) on LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Title for all row, once for all row -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="My Name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:text="My Type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
I am totally familiar with ExpandableRecyclerview Or ExpandableLayout But I have a design that is a little bit unique and I am seeking help to achieve this implementation.
I Am Attaching the design
When the user clicks the arrow shown in the image the CardView Recyclerview should expand or inflate a recyclerview in between comment button and response section. How to do that. I have tried the ExpandableRecyclerview from ThoughtBot and ExpandableListView all this components add a child to parent.
I am using ExpandableRecyclerview from Thoughtbot,
com.thoughtbot.expandablerecyclerview
My Adapter Looks like as below.
public class ReviewRecyclerAdapter extends ExpandableRecyclerViewAdapter<ReviewViewHolder, CommentsViewHolder> {
private Context context;
public ReviewRecyclerAdapter(Context context,List<? extends ExpandableGroup> groups) {
super(groups);
this.context = context;
}
#Override
public ReviewViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_review, parent, false);
return new ReviewViewHolder(view);
}
#Override
public CommentsViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_comments, parent, false);
return new CommentsViewHolder(view);
}
#Override
public void onBindChildViewHolder(CommentsViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
final CommentsModel comments = ((ReviewsModel) group).getItems().get(childIndex);
holder.setSubTitletName(comments.getComment());
}
#Override
public void onBindGroupViewHolder(ReviewViewHolder holder, int flatPosition, ExpandableGroup group) {
holder.setGenreTitle(context, group);
}
}
// View Holders
public class ReviewViewHolder extends GroupViewHolder {
private TextView titleName;
private ImageView arrow;
private ImageView icon;
public ReviewViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.list_item_genre_name);
arrow = (ImageView) itemView.findViewById(R.id.list_item_genre_arrow);
}
public void setGenreTitle(Context context, ExpandableGroup title) {
if (title instanceof ReviewsModel) {
titleName.setText(title.getTitle());
if (((ReviewsModel) title).getReview()!= null && !((ReviewsModel) title).getReview().isEmpty()){
}
}
}
#Override
public void expand() {
animateExpand();
}
#Override
public void collapse() {
animateCollapse();
}
private void animateExpand() {
RotateAnimation rotate =
new RotateAnimation(360, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
private void animateCollapse() {
RotateAnimation rotate =
new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
}
public class CommentsViewHolder extends ChildViewHolder {
private TextView subTitleTextView;
public CommentsViewHolder(View itemView) {
super(itemView);
subTitleTextView = (TextView) itemView.findViewById(R.id.subtitle);
}
public void setSubTitletName(String name) {
subTitleTextView.setText(name);
}
}
//Review Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/review_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_avatar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:src="#drawable/profile_placeholder" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/list_avatar">
<TextView
android:id="#+id/list_item_genre_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" Guru natha"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="15"
android:text=" From Mg Road"
android:layout_alignStart="#+id/list_item_genre_name"
android:layout_marginStart="10dp"
android:layout_below="#+id/list_item_genre_name" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" 3 Days ago"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/days"
android:layout_below="#+id/days"
android:maxEms="15"
android:text=" 448 Views"
android:textSize="8sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/review_details"
android:layout_margin="20dp">
<TextView
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Review"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Rating"
android:textStyle="bold" />
<View
android:id="#+id/lineytf"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/review"
android:background="#color/black" />
<TextView
android:layout_marginTop="10dp"
android:id="#+id/review_conetent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/lineytf"
android:layout_below="#+id/lineytf"
android:maxLines="3"
android:maxEms="15"
android:text="#string/lorem_ipsum" />
</RelativeLayout>
<RelativeLayout
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/content">
<TextView
android:id="#+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:maxEms="15"
android:text="Response (2)"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:layout_above="#+id/line2"
android:src="#drawable/white_r_arrow"
android:text="arrow"
android:textStyle="bold"
android:id="#+id/list_item_genre_arrow" />
<View
android:id="#+id/line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/response"
android:background="#color/black" />
<!--Add expandable views-->
<Button
android:id="#+id/commentsbutton"
android:textSize="12sp"
android:textAllCaps="false"
android:text="Comments"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/line2"
android:layout_toStartOf="#+id/button2" />
<Button
android:textSize="12sp"
android:textAllCaps="false"
android:text="56"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/list_item_genre_arrow"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
//Comment Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="#+id/list_item_genre_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="70dp"
android:layout_alignParentRight="true"
android:src="#drawable/android" />
<TextView
android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:gravity="center_vertical"
tools:text="SubTitle" />
</FrameLayout>
Current Screenshot :
Please do shoot the closest solution if you have.
when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.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:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);