I have very popular question - why the data not showing in the RecycleView.
That is my Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
private static final String TAG = "CommentsAdapter";
public ImageView mThumbView;
private List<PostComment> postCommentList;
public CommentsAdapter(List<PostComment> postCommentList) {
this.postCommentList = postCommentList;
}
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(CommentsAdapter.ViewHolder holder, int position) {
PostComment record = postCommentList.get(position);
holder.mAuthorTextView.setText(record.getUser().getFull_name());
holder.mDateTextView.setText(record.getCreated_at());
holder.mTextTextView.setText(record.getText());
}
#Override
public int getItemCount() {
Log.i(TAG + " comments size", Integer.toString(postCommentList.size()));
return postCommentList.size();
}
public void addItems(List<PostComment> postCommentList) {
this.postCommentList.addAll(postCommentList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mAuthorTextView;
public TextView mDateTextView;
public TextView mTextTextView;
public ViewHolder(View v) {
super(v);
mAuthorTextView = (TextView) itemView.findViewById(R.id.comment_author);
mDateTextView = (TextView) itemView.findViewById(R.id.comment_date);
mTextTextView = (TextView) itemView.findViewById(R.id.comment_text);
}
}
}
That is my view:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/comment_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="#drawable/cat"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middleLayout"
android:layout_toRightOf="#+id/comment_image"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="Dark Plastic"
android:textColor="?attr/colorPrimary"
android:textSize="14dp" />
<TextView
android:id="#+id/comment_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/comment_author"
android:text="21 minutes"
android:textSize="14dp" />
</RelativeLayout>
<TextView
android:id="#+id/comment_text"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/comment_image"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_marginTop="8dp"
android:layout_below="#id/middleLayout"
android:text="sd"
android:textColor="#android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:paddingLeft="16dp"
android:paddingRight="16dp"></View>
That is how I set up the adapter:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
post_details_comments_list.setLayoutManager(mLayoutManager);
mCommentListAdapter = new CommentsAdapter(new ArrayList<PostComment>());
post_details_comments_list.setAdapter(mCommentListAdapter);
After I set up the adapter, I the getItemCount returns the quantity of the items - so that is not null.
RecycleView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/post_details_mapview"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_details_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextLabelStyle"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/post_details_comments_list"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_comment"
android:hint="#string/hint_type_comment"/>
</LinearLayout>
I make the request to the server and here is the callback, where I get the results and set up the adapter:
#Override
public void onAllPostCommentsCallback(AllComments allComments) {
Log.i(TAG + " I've the comments", allComments.getPostCommentList().toString());
mCommentListAdapter.addItems(allComments.getPostCommentList());
post_details_comments_list.setAdapter(mCommentListAdapter);
mCommentListAdapter.notifyDataSetChanged();
}
But the list is not displaying.
What can be the reason?
put null inplace of parent in View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
I've already solved the issue. The problem was that I had RecycleView inside the NestedScrollView
Answer in this post helped me a lot:
How to use RecyclerView inside NestedScrollView?
Related
Hi in the below code I am working with bottom navigation it contains different option in that one thing is account .If click on the account bottom tab want to display the cardview in recyclerviews
But data is not coming anything. It is displaying empty.
using this below xml to display the content in listview format
accounts.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Fragment_account.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="#color/White">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/slivergray"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/White">
<ImageView
android:id="#+id/appImage"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginLeft="16dp"
android:background="#drawable/ic_account_circle_black_24dp"
android:backgroundTint="#color/gray"
android:scaleType="centerCrop"
tools:ignore="ContentDescription"/>
<TextView
android:id="#+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/appImage"
android:paddingLeft="16sp"
android:paddingRight="16dp"
android:text="Apollo Hospital"
android:textColor="#color/black"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
<TextView
android:id="#+id/subHeaderText"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headingText"
android:layout_toRightOf="#+id/appImage"
android:paddingLeft="16dp"
android:text="Hospital"
android:paddingRight="16dp"
android:textColor="#color/gray"
android:textSize="15sp"/>
<TextView
android:id="#+id/subHeadingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/subHeaderText"
android:layout_toRightOf="#+id/appImage"
android:gravity="left"
android:lines="5"
android:maxLines="5"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:text="stories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detail"
android:textColor="#color/gray"
android:textSize="14sp"/>
<TextView
android:id="#+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="⋮"
android:textColor="#color/gray"
android:layout_marginRight="10dp"
android:textAppearance="?android:textAppearanceLarge"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/subHeadingText">
<LinearLayout
android:layout_width="209dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="30dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="#color/gray"
android:src="#drawable/ic_account_circle_black_24dp"></ImageView>
<Button
android:id="#+id/action1"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 1"
android:textColor="#color/gray"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="#drawable/ic_account_circle_black_24dp"
android:layout_marginLeft="50dp"></ImageView>
<Button
android:id="#+id/action2"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 2"
android:textColor="#color/gray"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
AccountFragment.java:
public class AccountFragement extends Fragment {
public AccountFragement(){
}
RecyclerView rv;
private List<List_Data> list_data;
private MyAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate (R.layout.account, container, false);
rv=(RecyclerView) rootView.findViewById(R.id.recyclerview);
//rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
list_data=new ArrayList<>();
adapter=new MyAdapter(list_data,getContext());
rv.setAdapter(adapter);
return rootView;
}
private void setupData(List<List_Data> list_data) {
adapter=new MyAdapter(list_data,getContext());
rv.setAdapter(adapter);
}
}
List_Data.java:
public class List_Data {
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<List_Data>list_data;
private Context context;
private String title1;
public MyAdapter(List<List_Data> list_data, Context context) {
this.list_data = list_data;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_account,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
List_Data listData=list_data.get(position);
// title1=list_data.get(0).getTitle();
holder.title.setText(listData.getTitle());
}
#Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView img;
TextView title,subtile;
public ViewHolder(View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.headingText);
subtile=(TextView)itemView.findViewById(R.id.subHeaderText);
title.setText(title1);
}
}
}
the code you have written is somehow incomplete, first you can not view any data because there are no data in the List list_data in the AccountFragment so the adapter is does not know where to fetch data from.
When you have data in the list then you can populate to the adapter.
I recommend you follow the best approach from the link below in case you have any problem please do let us know.
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
And try your best to start using AndroidX in your project
I have 2 layouts where first layout for Fragment and second layout for RecyclerView. I have created CardView in RecyclerView, every CardView has some data. And I have created button below RecyclerView where the function of button for sending Data on CardView. My problem is I don't know how to send data when i click button in Fragment.
Fragment Layout :
<?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:id="#+id/container_daftar_alamat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.menu.menu_signin.menu.MenuAddressFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMoveTambahAlamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#null"
android:drawableStart="#drawable/ic_tambah_alamat"
android:drawablePadding="10dp"
android:text="Tambah Alamat"
android:textAllCaps="false"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Daftar Alamat"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tambah_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/gunakanAlamat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="100dp"
android:background="#drawable/bg_button_red"
android:text="Gunakan Alamat"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
RecyclerView Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
app:cardBackgroundColor="#color/colorWhite"
app:cardCornerRadius="4dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/nameUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Muhammad Rafi Bahrur Rizki"
android:textColor="#color/colorBlack"
android:textSize="14sp"
android:textStyle="bold"
tools:ignore="UnusedAttribute" />
<TextView
android:id="#+id/addressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:maxLines="1"
android:text="(Alamat Kantor)"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/streetName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Mangga Dua Square Lantai 1 Jakarta,"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp"
tools:ignore="UnusedAttribute" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/blokAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="Blok C no. 148 - 150"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/cityAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/countryAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/phoneUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="0812951825"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<Button
android:id="#+id/buttonEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:drawableStart="#drawable/ic_edit"
android:text="Edit"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#id/buttonEdit"
android:drawableStart="#drawable/ic_delete"
android:text="Hapus"
android:textAllCaps="false"
android:textSize="12sp" />
</RelativeLayout>
</TableLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When i select RadioButton, only data in RadioButton will be obtained. And when i click button, the data will be send to another activity.
Adapter RecyclerView :
public class AdapterGetAddress extends RecyclerView.Adapter<AdapterGetAddress.ViewHolder> {
Context context;
private List<ModelGetAddress> modelGetAddressList;
private BaseApiService baseApiService;
private int previousSelected = -1;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_address, parent, false);
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final ModelGetAddress adapterAddress = modelGetAddressList.get(position);
}
#Override
public int getItemCount() {
return modelGetAddressList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameUser, addressUser, streetName, blokAddressUser, cityAddressUser, countryUser, phoneUser;
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameUser = itemView.findViewById(R.id.nameUser);
addressUser = itemView.findViewById(R.id.addressUser);
streetName = itemView.findViewById(R.id.streetName);
countryUser = itemView.findViewById(R.id.countryAddressUser);
blokAddressUser = itemView.findViewById(R.id.blokAddressUser);
cityAddressUser = itemView.findViewById(R.id.cityAddressUser);
phoneUser = itemView.findViewById(R.id.phoneUser);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
}
});
}
}
}
Code in Fragment :
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here's were i'm stuck
}
});
return view;
}
}
You can solve this via an interface.
public interface ItemClickedCallback{
void onItemClicked(String nameUser //, the other data you wanna pass );
}
pass it through the constructor of your adapter.
Use this adapter constructor
ItemClickedCallback callback;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList, ItemClickedCallback callback) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
this.callback= callback;
}
When checking a radio button of any item; call the interface method, like this.
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
callback.onItemClicked(getNameUser //, the rest..);
}
});
Now, when you initialize the adapter in the fragment, create the new interface via the constructor and assign the data to class variables like this.
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
private String nameUser_;
// the rest...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
AdapterGetAddress adapter = new AdapterGetAddress(getActivity, list, new ItemClickedCallback() {
#Override
public void onItemClicked(String userName //, the rest..) {
// assign to the class variable like this
nameUser_ = userName;
// the rest..
}
}););
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Use the data as you want..
}
});
return view;
}
}
I have something wrong with recyclerView. It doesn't show all items. For example, recyclerView has 12 items, but it shows like 10 with a half items. I can set paddingBottom for it, and it shows all items, but I don't think it is a good way. How can I fix it? I think maybe it becouse of my buttons above it.
acctivity_search_coin.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="50dp"
app:layout_constraintTop_toBottomOf="#+id/lianerForThreeBtn" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#FFFFFF"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textSize="15sp"
android:drawablePadding="6dp"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/lianerForThreeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/button2">
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
activity_recycler_view_adapter_coin_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/denominationTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="8dp"
android:layout_toEndOf="#+id/secondIV"
android:textSize="20sp" />
<TextView
android:id="#+id/yearTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/denominationTV"
android:layout_alignStart="#+id/denominationTV"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="8dp"
android:textSize="20sp" />
<ImageView
android:id="#+id/firstIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"/>
<ImageView
android:id="#+id/secondIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="4dp"
android:layout_toEndOf="#+id/firstIV"/>
</RelativeLayout>
RecyclerViewAdapterCoinList.java
public class RecyclerViewAdapterCoinList extends RecyclerView.Adapter<RecyclerViewAdapterCoinList.ViewHolder> {
private List<String> mYearList;
private List<String> mDenominationList;
private List<String> mImageList1;
private List<String> mImageList2;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterCoinList(Context context, List<String> denominationList,List<String> yearList, List<String> imageList1,List<String> imageList2) {
this.mInflater = LayoutInflater.from(context);
this.mYearList = yearList;
this.mDenominationList = denominationList;
this.mImageList1 = imageList1;
this.mImageList2 = imageList2;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_coin_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String year = mYearList.get(position);
String denomination = mDenominationList.get(position);
String imageString1 = mImageList1.get(position);
String imageString2 = mImageList2.get(position);
holder.denominationTV.setText(denomination);
holder.yearTV.setText(year);
try {
Glide.with(context).load(imageString1).into(holder.firstIV);
Glide.with(context).load(imageString2).into(holder.secondIV);
}catch (Exception e){e.printStackTrace();}
}
// total number of rows
#Override
public int getItemCount() {
return mDenominationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView denominationTV;
TextView yearTV;
ImageView firstIV;
ImageView secondIV;
ViewHolder(View itemView) {
super(itemView);
denominationTV = itemView.findViewById(R.id.denominationTV);
yearTV = itemView.findViewById(R.id.yearTV);
firstIV = itemView.findViewById(R.id.firstIV);
secondIV = itemView.findViewById(R.id.secondIV);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mDenominationList.get(id);
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
Try adding app:layout_constraintBottom_toBottomOf="parent" in the recycler view and also make the height of the recycler view to be 0dp (match_constraint).
Recycler View is only occupying 1st half of the screen no matter what I do.
Can anyone suggest how to make Recyclerview occupy full width of the screen?
This is what it displays me in Android Studio
But this is the actual output on a device. It's all wrapped to the left side of the device.
activity.java
public class RecordLogs extends AppCompatActivity {
private RecyclerView recyclerView;
private RecordsAdapter adapter;
private List<Record> recordList;
private ProgressBar progressBar;
private FirebaseFirestore db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_logs);
progressBar = findViewById(R.id.progressbar);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager((this)));
recordList = new ArrayList<>();
adapter = new RecordsAdapter(this, recordList);
recyclerView.setAdapter(adapter);
db = FirebaseFirestore.getInstance();
activity.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=".RecordLogs">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="26/7/2018" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Qty. L"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="25" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rate ₹"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="125" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total ₹"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
adapter.java
public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.RecordViewHolder> {
private Context mCtx;
private List<Record> recordList;
public RecordsAdapter(Context mCtx, List<Record> recordList) {
this.mCtx = mCtx;
this.recordList = recordList;
}
#NonNull
#Override
public RecordViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.record_logs_card, null);
return new RecordViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecordViewHolder holder, int position) {
Record record = recordList.get(position);
holder.date.setText(record.getDate());
holder.qty.setText(String.valueOf(record.getQty()));
holder.rate.setText(String.valueOf(record.getRate()));
holder.total.setText(String.valueOf(record.getTotal()));
}
#Override
public int getItemCount() {
return recordList.size();
}
class RecordViewHolder extends RecyclerView.ViewHolder {
TextView date, qty, rate, total;
public RecordViewHolder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date);
qty = itemView.findViewById(R.id.qty);
rate = itemView.findViewById(R.id.rate);
total = itemView.findViewById(R.id.total);
}
}
}
I found it:
All I had to change the following line of code in my custom adapter as follows:
View view = inflater.inflate(R.layout.record_logs_card, null);
to
View view = inflater.inflate(R.layout.record_logs_card, parent, false);`
I am using a TextView under android.support.v7.widget.CardView. However, whenever I try to set a long text, TextView is not giving the full result.
List<User> userList = new ArrayList<>();
userList.add(new User(R.mipmap.ic_launcher,
"Amit", "9988776655",
"amit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.com"));
This is my java code(as an example) and this shows in the app like this:
for this first one 'Amit'.
How to solve this? If the text is long then it will automatically set it to next line.
Here is my full Cardview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is the snapshot of the java code:Java Snap Shot
this is the java full code
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.UserViewHolder>{
private List<MainActivity.User> userList;
private Context context;
public AllUsersAdapter(List<MainActivity.User> userList, Context context) {
this.userList = userList;
this.context = context;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout, null);
UserViewHolder userViewHolder = new UserViewHolder(view);
return userViewHolder;
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
MainActivity.User user = userList.get(position);
String a=user.getEmailId().toString();
holder.tvProfileName.setText(user.getProfileName());
holder.tvPhoneNumber.setText(user.getPhoneNumber());
holder.tvEmailId.setText(a);
}
#Override
public int getItemCount() {
return userList.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
TextView tvProfileName;
TextView tvPhoneNumber;
TextView tvEmailId;
public UserViewHolder(View itemView) {
super(itemView);
tvProfileName = (TextView) itemView.findViewById(R.id.tvProfileName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
}
Thank you.
use this :
yourTextView.append("\n anotherTextPart")
First of all - if your string will not have white spaces TextView will not able to properly arange text in multi line.
Try to set following attributes :
<TextView
...
android:maxLines="4"/>
Update here is my test that works even without flag above:
MainActivity.java:
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView email;
public ViewHolder(View itemView) {
super(itemView);
email = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_card, parent, false);
return new ViewHolder(card);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.email.setText("test#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.com");
}
#Override
public int getItemCount() {
return 5;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);
view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
view.setAdapter(new Adapter());
}
user_card.xml (changed layout_height to wrap_content):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:src="#drawable/user"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test test test"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
main_activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Result:
Solved by using
TableLayout
and using specific width.full code:
<RelativeLayout 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.CardView
android:id="#+id/cvSingleUser"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="270dip"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="test test test"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="test"
android:layout_below="#id/tvProfileName" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:layout_below="#id/tvPhoneNumber" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>