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>
Related
I have some problem with my code. I cannot set Textview but ImageView is working. This is my xml textview. I using adapter and listener interface.
error : textview on a null object reference
<?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:descendantFocusability="blocksDescendants"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_middle" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/spacing_middle"
android:layout_marginRight="#dimen/spacing_middle"
android:visibility="visible"
app:cardCornerRadius="0dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginLeft="#dimen/spacing_large"
android:layout_marginRight="#dimen/spacing_large"
android:layout_marginTop="#dimen/spacing_large"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgGambar"
android:layout_width="#dimen/spacing_xxlarge"
android:layout_height="#dimen/spacing_xxlarge"
android:foreground="#color/overlay_light_20"
app:civ_shadow="true"
app:civ_shadow_radius="0"
android:src="#drawable/photo_female_2"
app:civ_border="false" />
<View
android:layout_width="#dimen/spacing_large"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Emma Richmond"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#color/grey_90" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_small"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="in "
android:textAppearance="#style/TextAppearance.AppCompat.Caption"
android:textColor="#color/grey_40" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Hwy, Carthage"
android:textAppearance="#style/TextAppearance.AppCompat.Caption"
android:textColor="#color/light_blue_400"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvKonten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
android:lineSpacingExtra="4dp"
android:text="#string/middle_lorem_ipsum"
android:textAppearance="#style/TextAppearance.AppCompat.Caption"
android:textColor="#color/grey_60" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/spacing_medium"
android:background="#color/grey_5" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_xxlarge"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgLike"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:layout_marginEnd="#dimen/spacing_middle"
android:layout_marginRight="#dimen/spacing_middle"
android:tint="#color/light_green_300"
android:src="#drawable/ic_thumb_up" />
<TextView
android:id="#+id/txtsuka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="12 likes"
android:textAppearance="#style/TextAppearance.AppCompat.Caption"
android:textColor="#color/grey_40" />
</LinearLayout>
<View
android:layout_width="#dimen/spacing_large"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:layout_marginEnd="#dimen/spacing_middle"
android:layout_marginRight="#dimen/spacing_middle"
android:tint="#color/light_blue_400"
android:src="#drawable/ic_textsms" />
<TextView
android:id="#+id/tvKomentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="4 Comments"
android:textAppearance="#style/TextAppearance.AppCompat.Caption"
android:textColor="#color/grey_40" />
</LinearLayout>
<TextView
android:id="#+id/tvLalu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end|right"
android:text="2h ago"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/grey_40" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
and this is my adapter : https://pastebin.com/csiW52h4
public class AdapterCardPostList extends RecyclerView.Adapter<AdapterCardPostList.ViewHolder> {
private Context context;
private List<PostItem> list;
private List<Check> checkList;
private OnItemClickListener mOnItemClickListener;
public AdapterCardPostList(Context context, List<PostItem> list, List<Check> checkList, OnItemClickListener monItemClickListener) {
this.context = context;
this.list = list;
this.checkList = checkList;
this.mOnItemClickListener = monItemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public ImageView image, imglike;
public TextView name;
public TextView konten;
public TextView like;
public TextView komentar;
public TextView lalu;
public ViewHolder(View v) {
super(v);
image = (ImageView) v.findViewById(R.id.imgGambar);
name = (TextView) v.findViewById(R.id.tvName);
konten = (TextView) v.findViewById(R.id.tvKonten);
like = (TextView) v.findViewById(R.id.txtsuka);
komentar = (TextView) v.findViewById(R.id.tvKomentar);
lalu = (TextView) v.findViewById(R.id.tvLalu);
imglike = (ImageView) v.findViewById(R.id.imgLike);
imglike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mOnItemClickListener.onItemClick(view, checkList.get(getAdapterPosition()), getAdapterPosition(), list.get(getAdapterPosition()));
}
});
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_post, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
PostItem p = list.get(position);
holder.konten.setText(p.getContent());
holder.like.setText(p.getLike());
holder.name.setText(p.getName());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
// long time = sdf.parse(p.getTanggal()).getTime();
Date date = sdf.parse(p.getTanggal());
long now = System.currentTimeMillis();
CharSequence ago =
DateUtils.getRelativeTimeSpanString(date.getTime(), now, DateUtils.MINUTE_IN_MILLIS);
holder.lalu.setText(ago);
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
holder.komentar.setText(p.getKomen() + " Comments");
Glide.with(context).load("https://graph.facebook.com/" + p.getUid() + "/picture?type=normal").into(holder.image);
if (p.getUid().equals(MainActivity.id)){
holder.imglike.setColorFilter(ContextCompat.getColor(context, R.color.red_200), android.graphics.PorterDuff.Mode.MULTIPLY);
checkList.add(new Check(true));
}else{
holder.imglike.setColorFilter(ContextCompat.getColor(context, R.color.light_green_300), android.graphics.PorterDuff.Mode.MULTIPLY);
checkList.add(new Check(false));
}
}
#Override
public int getItemCount() {
return list.size();
}
}
my error like this
why my imageview working but textview not working
thank you for attention
mOnItemClickListener.onItemClick(view, checkList.get(getAdapterPosition()), getAdapterPosition(), list.get(getAdapterPosition()));
change to
mOnItemClickListener.onItemClick(v, checkList.get(getAdapterPosition()), getAdapterPosition(), list.get(getAdapterPosition()));
This is because the id tvKonten does not exist. In your XML code, the id of the TextView is txtsuka, not tvKonten. Change tvKonten to txtsuka.
Try these options, it might work for you :
Invalidate Caches and Restart
Check if you have a different version of the same layout file in your res folder such as ...v26.xml or something like that ; if you find any then delete the unwanted version of the file.
I have recycler view and spinner in activity depending on spinner value post request has been sent to server after which recycler view populated in which every row have some cost. I want to add this cost of every row.
Screenshot is given below:
In screenshot we can see No. of quantities are 2 which cost 40 like this other row also have some value.SO I want to add all these costs from each row and showing it in lower left area where ToTAL is written.
Here is my code:
activity_select_pack.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectPack">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progress"
android:layout_centerInParent="true"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="3dp"
android:layout_margin="16dp"
android:id="#+id/marketCard"
app:cardBackgroundColor="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="15sp"
android:id="#+id/textMarket"
android:text="Select Market Name"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_below="#+id/textMarket"
android:id="#+id/marketSpinner"
android:layout_marginTop="25dp"
android:background="#drawable/spinner_back"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="#+id/products"
android:layout_below="#+id/marketCard"
android:visibility="invisible"
android:layout_above="#+id/totalLayout"
android:layout_marginBottom="3dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:id="#+id/totalLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="10dp"
android:background="#drawable/spinner_back"
android:text="Total:00.00"
android:layout_centerVertical="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="8dp"
android:background="#drawable/login_but"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="Generate bill"
android:textSize="12sp"
android:textColor="#fff"/>
</RelativeLayout>
</RelativeLayout>
selectpack_layout.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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Crown"
android:layout_margin="10dp"
android:id="#+id/marketName"
android:textSize="18sp"
android:textColor="#color/colorPrimary"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:id="#+id/view1"
android:layout_below="#+id/marketName"
android:background="#adadad"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product No."/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="#+id/productNo"
android:textColor="#color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/productNo"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="#+id/page"
android:textColor="#color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/page"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MRP"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="#+id/mrp"
android:textColor="#color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/mrp"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="#+id/innerPack"
android:textColor="#color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/innerPack"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Outer pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="#+id/outerPack"
android:textColor="#color/colorPrimary"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/innerPack"
android:background="#adadad"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Spinner
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="#+id/qtySpinner"
android:background="#drawable/qty_spinner"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:id="#+id/total"
android:layout_weight="1"/>
<Button
android:layout_width="50dp"
android:layout_height="30dp"
android:text="ORDER"
android:id="#+id/order"
android:textColor="#fff"
android:background="#drawable/login_but"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
SelectPack.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_pack);
fAuth = FirebaseAuth.getInstance();
ActionBar ab = getSupportActionBar();
assert ab!= null;
ab.setTitle("Select Pack");
ab.setDisplayHomeAsUpEnabled(true);
marketSpinner = findViewById(R.id.marketSpinner);
progress = findViewById(R.id.progress);
products = findViewById(R.id.products);
products.setHasFixedSize(true);
products.setLayoutManager(new LinearLayoutManager(this));
productList = new ArrayList<>();
List<String> categories = new ArrayList<String>();
categories.add("Select market");
categories.add("Crown");
categories.add("Long Book A4");
categories.add("Long Book");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
marketSpinner.setAdapter(dataAdapter);
marketSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
if(item.equals("Select market")){
progress.setVisibility(View.INVISIBLE);
}
else{
getData(item);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void getData(String item){
progress.setVisibility(View.VISIBLE);
products.setVisibility(View.INVISIBLE);
productList.clear();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.writeTimeout(20,TimeUnit.SECONDS)
.build();
RequestBody formBody = new FormBody.Builder()
.add("name",item)
.build();
Request request = new Request.Builder().post(formBody).url(URL).build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(#NotNull Call call, #NotNull final Response response) throws IOException {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONArray jsonArray = new JSONArray(response.body().string());
if(jsonArray.length() > 0){
products.setVisibility(View.VISIBLE);
progress.setVisibility(View.INVISIBLE);
}
for(int i=0;i<jsonArray.length();i++){
progress.setVisibility(View.INVISIBLE);
JSONObject object = jsonArray.getJSONObject(i);
String str1 = object.getString("market");
String str2 = object.getString("product_no");
String str3 = object.getString("page");
String str4 = object.getString("mrp");
String str5 = object.getString("inner_pack");
String str6 = object.getString("outer_pack");
Log.d("prod",str2);
ProductsModel model = new ProductsModel(str1,str2,str3,str4,str5,str6);
productList.add(model);
}
ProductAdapter adapter = new ProductAdapter(getApplicationContext(),productList);
products.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onFailure(#NotNull Call call, #NotNull final IOException e) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progress.setVisibility(View.INVISIBLE);
products.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
ProductAdapter.java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private Context context;
private List<ProductsModel> productList;
public ProductAdapter(Context context, List<ProductsModel> productList) {
this.context = context;
this.productList = productList;
}
#NonNull
#Override
public ProductAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.selectpack_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull final ProductAdapter.ViewHolder holder, int position) {
final ProductsModel model = productList.get(position);
holder.marketName.setText(model.getMarketName());
holder.productNo.setText(model.getProductNo());
holder.page.setText(model.getPage());
holder.mrp.setText(model.getMrp());
holder.innerPack.setText(model.getInnerPack());
holder.outerPack.setText(model.getOuterPack());
List<String> qty = new ArrayList<>();
qty.add("Select qty");
qty.add("1");
qty.add("2");
qty.add("3");
qty.add("4");
qty.add("5");
qty.add("6");
qty.add("7");
qty.add("8");
qty.add("9");
qty.add("10");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, qty);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.qtySpinner.setAdapter(dataAdapter);
holder.qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
if(!item.equals("Select qty")){
int qty = Integer.parseInt(item);
int cost = Integer.parseInt(model.getMrp());
int val = cost * qty;
holder.total.setText(String.valueOf(val));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
#Override
public int getItemCount() {
return productList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView marketName,productNo,page,mrp,innerPack,outerPack,total;
Spinner qtySpinner;
Button order;
public ViewHolder(#NonNull View itemView) {
super(itemView);
order = itemView.findViewById(R.id.order);
qtySpinner = itemView.findViewById(R.id.qtySpinner);
marketName = itemView.findViewById(R.id.marketName);
productNo = itemView.findViewById(R.id.productNo);
page = itemView.findViewById(R.id.page);
mrp = itemView.findViewById(R.id.mrp);
innerPack = itemView.findViewById(R.id.innerPack);
outerPack = itemView.findViewById(R.id.outerPack);
total = itemView.findViewById(R.id.total);
}
}
}
Someone please let me know how can I get and add total cost from each row and show it in lower left area. Any help would be appreciated.
THANKS
You can use array and append the value of each cell in it and never clear it, then create a function in your adapter that return with this array, here you cal add it's items.
So I am displaying a recylerview with an Image and the name of a person. Below that is a seekbar, which the user can move to rate that person.
Now I want to store the ratings (=progress) of each seekbar in a list. However right now it only stores the rating of the last seekbar in the list.
So it stores only the progress of the last seekbar right now. I would like that when the user clicks the button that every current rating value of every seekbar is stored in a list.
Thank you very much.
That is is my layout_listitem:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:id="#+id/parent_layoutREC"
android:orientation="horizontal"
android:layout_marginTop="13dp"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="2dp"
android:paddingLeft="2dp"
android:layout_width="73dp"
android:layout_height="73dp"
android:id="#+id/kunde_imageREC"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
android:id="#+id/kunde_nameREC"
android:textColor="#000"
android:textSize="19sp"
android:textStyle="bold"/>
</LinearLayout>
<SeekBar
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="8dp"
android:id="#+id/seek_Bar"
android:max="10"
android:progress="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 1 2 3 4 5"
android:textSize="20sp"/>
That is my RecyclerViewAdapter class:
public class RecyclerViewAdap extends
RecyclerView.Adapter<RecyclerViewAdap.ViewHolder> {
private static final String TAG = "RecyclerViewAdap";
private ArrayList<String> mImageUrl = new ArrayList<>();
private ArrayList<String> mKundeNamen = new ArrayList<>();
public ArrayList<Integer> mBewertungen = new ArrayList<>();
private Context mContext;
public String bewertung;
private TextView progressSeekbar;
public RecyclerViewAdap(ArrayList<String> imageUrl, ArrayList<String> kundeNamen, Context context) {
mImageUrl = imageUrl;
mKundeNamen = kundeNamen;
mContext = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
TextView kundeName;
LinearLayout parentLayout;
SeekBar seekBar1;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.kunde_imageREC);
kundeName = itemView.findViewById(R.id.kunde_nameREC);
parentLayout = itemView.findViewById(R.id.parent_layoutREC);
seekBar1 = itemView.findViewById(R.id.seek_Bar);
progressSeekbar = itemView.findViewById(R.id.progress_seekbar);
seekBar1.setOnSeekBarChangeListener(seekBarChangeListener);
//int progress = seekBar1.getProgress();
//String.valueOf(Math.abs((long)progress)).charAt(0);
//progressSeekbar.setText("Bewertung: " +
// String.valueOf(Math.abs((long)progress)).charAt(0) + "." + String.valueOf(Math.abs((long)progress)).charAt(1));
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
Log.d(TAG, "onCreateViewHolder: ");
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.load(mImageUrl.get(position))
.into(holder.imageView);
holder.kundeName.setText(mKundeNamen.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, mKundeNamen.get(position),
Toast.LENGTH_SHORT).show();
}
});
holder.seekBar1.setTag(position);
}
#Override
public int getItemCount() {
return mImageUrl.size();
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// updated continuously as the user slides the thumb
progressSeekbar.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// called when the user first touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// called after the user finishes moving the SeekBar
if (seekBar.getTag().toString().equals("1")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 2) {
mBewertungen.remove(0);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
if (seekBar.getTag().toString().equals("2")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 3) {
mBewertungen.remove(1);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
}
That is the activity where the Recyclerview is being displayed:
<?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"
tools:context=".BewertungEsserActvity"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
android:background="#drawable/gradient_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Bewerte deine Gäste"
android:textColor="#color/colorDarkGrey"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorBlack"
android:layout_marginBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:visibility="invisible"
android:id="#+id/progressbar_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp" />
<Button
android:layout_marginRight="66dp"
android:layout_marginLeft="66dp"
android:id="#+id/bewerten_Btn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:background="#drawable/btn_background_profil"
android:padding="10dp"
android:text="Bewerten"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="16sp"
android:textStyle="normal" />
<View
android:layout_marginBottom="10dp"
android:layout_marginTop="34dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBlack" />
</LinearLayout>
</ScrollView>
You can do it in multiple ways:
1- You can store seekbars value as a property in your model and update that property on OnSeekBarChangeListener and when the user hits the button get the data source from the adapter and then run a loop on it and call getter of that attribute.
2- You can store seekbars value as a List<> in your adapter and whenever the user hits the button get the list from the adapter.
I am experiencing a very weird issue where I am unable to view any text/data, but it clearly shows the number of items in the list.
I have confirmed that dataList has data, and I am able to get the exact same string that I set after debugging.
Parent Fragment:
ListView InboxListView = (ListView) view.findViewById(R.id.listViewInbox);
InboxAdapter adapter = new InboxAdapter(mActivity, R.layout.activity_inbox_item, dataList, NotificationInbox.this);
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SwipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:padding="15dp"
android:id="#+id/searchBarInbox"
android:hint="Search"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="49dp"
android:id="#+id/listViewInbox" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Adapter: http://pastebin.com/mSUrdLcW << CLICK
public class InboxAdapter extends ArrayAdapter<InboxBO> {
NotificationInbox fragment;
Context context;
List<InboxBO> inboxForSharedPref;
List<InboxBO> inboxList;
int layoutResID;
SharedPreferences prefs;
private ArrayList<InboxBO> inboxarraylist;
private SparseBooleanArray mSelectedItemIds;
public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
super(context, layoutResourceID, objs);
mSelectedItemIds = new SparseBooleanArray();
this.context = context;
this.inboxList = objs;
this.layoutResID = layoutResourceID;
this.inboxarraylist = new ArrayList<InboxBO>();
this.inboxarraylist.addAll(inboxList);
this.fragment = fragment;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inboxHolder inboxholder;
View view = convertView;
if (view == null) {
view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
//LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
//LayoutInflater inflater = LayoutInflater.from(getContext());
//inflater.inflate(R.layout.activity_inbox_item, null);
inboxholder = new inboxHolder();
//view = inflater.inflate(layoutResID, parent, false);
prefs = context.getSharedPreferences(
Constants.PREF_NAME, 0);
inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
inboxholder.delete = (TextView)view.findViewById(R.id.delete);
inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
view.setTag(inboxholder);
}
else {
inboxholder = (inboxHolder) view.getTag();
}
InboxBO mItem = inboxList.get(position);
if(mItem!=null){
inboxholder.senderNameText.setText((String)mItem.getTitle());
inboxholder.pushDateText.setText(new Date().toString());
inboxholder.pushTimeText.setText(new Date().toString());
inboxholder.messageText.setText((String)mItem.getMessage());
}
return view;
}
// For swipe action
private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
context);
alert.setTitle("Delete Message");
alert.setMessage("Are you sure you wish to delete this message?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
inboxList.remove(position);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
Toast.makeText(context, "Message Deleted!",
Toast.LENGTH_SHORT).show();
holder.swipeLayout.close();
fragment.retrieveMessage();
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
};
}
// Algorithm to filter out listview based on text changed in listview searchbox
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
inboxList.clear();
if (charText.length() == 0) {
inboxList.addAll(inboxarraylist);
}
else
{
for (InboxBO ilist : inboxarraylist)
{
/*if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
{
inboxList.add(ilist);
}*/
if(ilist.getTitle().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) ){
inboxList.add(ilist);
}
}
}
notifyDataSetChanged();
}
public static class inboxHolder {
TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
RelativeLayout itemLayout;
private SwipeLayout swipeLayout;
}
// Methods below are for multi-deletion
public void toggleSelection(int position) {
selectView(position, !mSelectedItemIds.get(position));
}
// Remove selection after unchecked
public void remove(InboxBO object) {
inboxList.remove(object);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
notifyDataSetChanged();
}
// Item checked on selection
public void selectView(int position, boolean value) {
if (value)
mSelectedItemIds.put(position, value);
else
mSelectedItemIds.delete(position);
notifyDataSetChanged();
}
// Get number of selected item
public int getSelectedCount() {
return mSelectedItemIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemIds;
}
public void removeSelection() {
mSelectedItemIds = new SparseBooleanArray();
notifyDataSetChanged();
}
}
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
EDIT: Screenshot added
EDIT2: Clarified parent fragment calling the adapter.
EDIT3: I did a temporary fix. Apparently the RelativeLayout and the Linear Layout in the AdapterXML were the cause of my misery. I'm not exactly sure why, though. I think it's the RelativeLayout and the LinearLayout somehow not displaying the text. Removing those two tags will display the texts in a linear fashion.
Try this
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>
This is my Activity:
public String id;
public String passPhrase;
public ArrayList<SongCell> songs;
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_view_layout);
Context context = this.getApplicationContext();
Bundle stateData = getIntent().getExtras();
try
{
id = stateData.getString("id");
passPhrase = stateData.getString("passPhrase");
ArrayList data;
recyclerView = (RecyclerView) findViewById(R.id.song_list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<SongCell>();
for (int i=0; i<5;i++)
{
data.add(new SongCell("Song "+i,"Artist "+i, null));
}
myAdapter = new MyAdapter(data);
recyclerView.setAdapter(myAdapter);
}
catch(Exception e)
{
Log.d("Error: ", e.toString());
}
}
card.xml
<android.support.v7.widget.CardView android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/song_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/song_name"
android:textSize="30sp"
android:text="Song Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/vote_button"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="#drawable/arrow" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Activity.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:background="#2d2d2d">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:layout_marginBottom="10dp"
android:background="#222222"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/featSongImage1"
android:contentDescription="#string/newsongimage"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/songname"
android:id="#+id/featSongName1" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/featSongButt1"
android:background="#drawable/arrow"
android:contentDescription="#string/votearrow" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="fill_horizontal"
android:gravity="fill_horizontal|center">
<android.support.v7.widget.RecyclerView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
And this is my custom adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<SongCell> data;
public MyAdapter(ArrayList<SongCell> dataI)
{
data = dataI;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_card,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int pos) {
viewholder.songName.setText(data.get(pos).getName());
viewholder.artist.setText(data.get(pos).getArtist());
viewholder.image.setImageBitmap(data.get(pos).getArtwork());
}
#Override
public int getItemCount() {
return 0;
}
#Override
public int getItemViewType(int position) {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView songName;
TextView artist;
ImageView image;
ImageButton voteButton;
public ViewHolder(View v)
{
super(v);
this.songName = (TextView) v.findViewById(R.id.song_name);
this.image = (ImageView) v.findViewById(R.id.song_photo);
this.voteButton = (ImageButton) v.findViewById(R.id.vote_button);
}
}
}
I have looked at a ton of guides on how to get this working but it still doesn't work. Anyone have any clue what's going on? I'm on version 25.0.1, and have all the modules imported. But it's not adding a single card into the layout.
I thought it would be difficult to debug such a big code you uploaded. But luckily my eye went on this line
#Override
public int getItemCount() {
return 0;
}
return 0; in adapter.
Your list size is always 0.
instead write this
#Override
public int getItemCount() {
return data.size();
}
EDIT-1:
You will also get null pointer exception here
viewholder.artist.setText(data.get(pos).getArtist());
Because you are not initializing the artist variable in ViewHolder class.
Edit-2
#Override
public int getItemCount() {
return 0;
}
you can remove this particular code from your adapter. Because overriding the methods of class that we don't use might sometimes result in errors that you can't even imagine.