I've made an adapter with a layout like the one I share below. it went well but, the appearance of each item became untidy.
Here is my code :
public class TableRecyclerAdapter extends RecyclerView.Adapter<TableRecyclerAdapter.ViewHolder> {
private ArrayList<TableModel> data;
private Context context;
private Fragment fragment;
private Dialog dialog;
public TableRecyclerAdapter(Context context, ArrayList<TableModel> data, Fragment fragment, Dialog dialog) {
this.context = context;
this.data = data;
this.fragment = fragment;
this.dialog = dialog;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_table, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.tvTable.setText(data.get(position).getName());
holder.tvState.setText(data.get(position).getStateString());
holder.bg.setBackgroundColor(Color.parseColor(data.get(position).getWarna()));
holder.tvHtml.setText(Html.fromHtml(data.get(position).getShortInfoHtml()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView paid;
private TextView tvTable, tvHtml, tvState;
private ConstraintLayout bg;
public ViewHolder(View itemView) {
super(itemView);
tvTable = itemView.findViewById(R.id.tvTable);
tvHtml = itemView.findViewById(R.id.tvHtml);
bg = itemView.findViewById(R.id.bg);
tvState = itemView.findViewById(R.id.tvState);
paid = itemView.findViewById(R.id.ivPaid);
}
}
}
This is for layout item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bg"
android:layout_width="match_parent"
android:background="#drawable/border_button_green"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:background="#drawable/border_transparent_table"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tvState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="This text"
android:textColor="#color/color_primary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/tvTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="25dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:text="7"
android:textColor="#color/color_primary"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvHtml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:paddingBottom="12dp"
android:text="Example Text"
android:textColor="#color/color_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvTable" />
<ImageView
android:id="#+id/ivPaid"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:src="#drawable/paid"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
It makes my recyclerview have different heights depending on the text.
I want the items to be the same height per row (maximum height in the row)
Sorry for my english. I hope someone can help me. Thank you
Related
I am trying to implement a nested recycle view i read several post online, but i am still having a difficult time. This is what i did so far, i created a main layout which contains my main Recycle view, i then created the main recycle view row item. And populated it by creating an adapter and implementing it in my main activity. Secondly, i crated a layout with my child recycle view as well as the child recycle view row item. The problem arises when i am trying to access this recycle view, i am unsure as to how to go about doing this? I was able to access my parent recycle and implement the adapter in my main activity, but i am not sure as to how to access my child recycle view. Could someone assist me?
**Parent Recycle View**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
**Parent Recycle View row**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_home"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:elevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view_movie"
android:layout_width="130dp"
android:layout_height="170dp"
android:scaleType="fitXY"
/>
<LinearLayout
android:id="#+id/layout_mov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image_view_movie"
android:layout_margin="1dp"
android:orientation="vertical">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Mission Impossible"
android:textSize="12sp" />
<TextView
android:id="#+id/tv_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:maxLines="1"
android:text="Action,Comedy..."
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
**Child RecycleView**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_movie_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Movie Category"
android:textSize="14sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_recycler_view_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_movie_category"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal" />
<View
android:id="#+id/activityMainDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/home_recycler_view_horizontal"
android:layout_marginBottom="5dp"
android:background="#31C7C7CC" />
</RelativeLayout>
**Child recycle view row**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/newsThumbNail"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/newsTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Dangerous fire is out of control at 24 and park"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="#+id/newsThumbNail"
app:layout_constraintStart_toStartOf="#+id/newsThumbNail"
app:layout_constraintTop_toBottomOf="#+id/newsThumbNail" />
<TextView
android:id="#+id/newsBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Some text is here"
app:layout_constraintEnd_toEndOf="#+id/newsTitle"
app:layout_constraintStart_toStartOf="#+id/newsTitle"
app:layout_constraintTop_toBottomOf="#+id/newsTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
**Parent Adapter**
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MessageViewHolder> {
private Context context;
private ArrayList<DataModel> userMessagesList;
public HomeAdapter(Context context, ArrayList<DataModel> userMessageList) {
this.context = context;
this.userMessagesList = userMessageList;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inner_recycleview_row, viewGroup, false);
return new MessageViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull MessageViewHolder viewHolder, int i) {
// viewHolder.senderTextView.setText(message.getMessage());
viewHolder.senderTextView.setText(userMessagesList.get(i).getName());
viewHolder.receiverTextView.setText(userMessagesList.get(i).getAge());
}
#Override
public int getItemCount() {
return userMessagesList.size();
}
//links up ui elements
static class MessageViewHolder extends RecyclerView.ViewHolder {
private TextView senderTextView, receiverTextView;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
senderTextView = itemView.findViewById(R.id.tv_genre);
receiverTextView = itemView.findViewById(R.id.tv_title);
}
}
}
**Main Activity**
public class MainActivity extends AppCompatActivity {
private RecyclerView messagesRecycleView;
private ArrayList<DataModel> messageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageList = new ArrayList<>();
initRecycleView();
populateArray();
}
public void populateArray(){
DataModel dataModel1= new DataModel("Bob","18");
messageList.add(dataModel1);
}
//set up RecycleView
public void initRecycleView() {
messagesRecycleView = findViewById(R.id.rv_main);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
//linearLayoutManager.setReverseLayout(true);
messagesRecycleView.setLayoutManager(linearLayoutManager);
HomeAdapter homeAdapter = new HomeAdapter(MainActivity.this, messageList);
messagesRecycleView.setAdapter(homeAdapter);
}
}
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MessageViewHolder> {
private Context context;
private ArrayList<DataModel> userMessagesList;
public NewsAdapter(Context context, ArrayList<DataModel> userMessageList) {
this.context = context;
this.userMessagesList = userMessageList;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_feed_row, viewGroup, false);
return new MessageViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull MessageViewHolder viewHolder, int i) {
// viewHolder.senderTextView.setText(message.getMessage());
viewHolder.senderTextView.setText(userMessagesList.get(i).getName());
viewHolder.receiverTextView.setText(userMessagesList.get(i).getAge());
}
#Override
public int getItemCount() {
return userMessagesList.size();
}
//links up ui elements
static class MessageViewHolder extends RecyclerView.ViewHolder {
private TextView senderTextView, receiverTextView;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
senderTextView = itemView.findViewById(R.id.newsBody);
receiverTextView = itemView.findViewById(R.id.newsTitle);
}
}
}
I have ListView and my own adapter. ListView item consists of TextView, icon image and image "trash can"(simply "trash"). The problem is when i click on any item it should be deleted but in fact the last item of ListView disappears. I checked ArrayList and the item I clicked on is deleted from there.
Also when i try to add new item the program just restore previous items and if all initial items are displayed and i try to add new the program crashes. Although in ArrayList items are added correctly.
Activity code
public class MyItemsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_items);
myItemsList = findViewById(R.id.ListOfMyItems);
myItemsAdapter.arrayList.add(new MyItems("Keys", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Wallet", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Physics", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("umovnyy vasyl", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Smartphone)", R.drawable.plus));
for (int i = 0; i<100; i++){
arrayList.add(new MyItems("Max)", R.drawable.plus));
}
myItemsList.addFooterView(new ImageView(this));
myItemsList.addHeaderView(new ImageView(this));
myItemsList.setAdapter(myItemsAdapter);
for (int i = 0; i<100; i++){
myItemsAdapter.arrayList.remove((5));
}
arrayList.add(new MyItems("Maxx)", R.drawable.plus));
myItemsAdapter.notifyDataSetChanged();
myItemsAdapter.notifyDataSetInvalidated();
myItemsList.setOnItemClickListener((parent, view, position, id) -> {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_SHORT)
.show();
myItemsAdapter.arrayList.remove((position-1));
myItemsAdapter.notifyDataSetChanged();
myItemsList.invalidateViews();
myItemsAdapter.notifyDataSetInvalidated();
});
}
ListView myItemsList;
ArrayList<MyItems> arrayList = new ArrayList<>();
MyItemsAdapter myItemsAdapter = new MyItemsAdapter(MyItemsActivity.this, arrayList);
public void addNewItem(View view) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MyItemsActivity.this);
dialog.setView(R.layout.add_new_item);
AlertDialog alertDialog = dialog.create();
ListView a = alertDialog.getListView();
alertDialog.show();
myItemsList.invalidateViews();
}
public void addNewItemButtonClicked(View view){
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.add_new_item, null);
EditText nameOfItem = v.findViewById(R.id.nameOfItem);
arrayList.add(new MyItems(nameOfItem.getText().toString(), R.drawable.forwardbutton));
myItemsAdapter.notifyDataSetChanged();
myItemsAdapter.notifyDataSetInvalidated();
}
public void notificationClicked(View view){
Toast.makeText(getApplicationContext(),
"Notification clicked", Toast.LENGTH_SHORT)
.show();
}
public void openMenu(View view) {
Intent intent = new Intent(getBaseContext(), MenuActivity.class);
startActivity(intent);
}
}
Adapter
class MyItemsAdapter extends BaseAdapter {
ArrayList<MyItems> arrayList;
Context context;
public MyItemsAdapter(Context context, ArrayList<MyItems> arrayList) {
this.arrayList=arrayList;
this.context=context;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
observer.onChanged();
super.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
super.unregisterDataSetObserver(observer);
observer.onInvalidated();
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public MyItems getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyItems myItems = arrayList.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.example_of_list_of_items, parent, false);
TextView nameOfItem = convertView.findViewById(R.id.exampleTextView);
ImageView itemCircle = convertView.findViewById(R.id.exampleImageViewIcon);
ImageView trash = convertView.findViewById(R.id.exampleImageViewTrash);
nameOfItem.setText(myItems.itemName);
itemCircle.setImageResource(myItems.itemImage);
trash.setImageResource(R.drawable.trash);
trash.setOnClickListener(v -> {
Toast.makeText(context, "lol "+position, Toast.LENGTH_SHORT).show();
arrayList.remove(position);
notifyDataSetChanged();
notifyDataSetInvalidated();
});
}
return convertView;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return arrayList.size();
}
#Override
public boolean isEmpty() {
return false;
}
}
MyItems code
class MyItems {
String itemName;
int itemImage;
public MyItems(String itemName, int image) {
this.itemName = itemName;
this.itemImage = image;
}
}
Example of item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/exampleTextView"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="85dp"
android:fadingEdge="horizontal|vertical"
android:layoutDirection="inherit"
android:lineSpacingExtra="14sp"
android:text="TextView"
android:textAlignment="textStart"
android:textColor="#000000"
android:textSize="20sp"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="8sp"
app:layout_constraintEnd_toStartOf="#+id/exampleImageViewTrash"
app:layout_constraintStart_toEndOf="#+id/exampleImageViewLeft"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/exampleImageViewLeft"
android:layout_width="25dp"
android:layout_height="23dp"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher_round" />
<ImageView
android:id="#+id/exampleImageViewTrash"
android:layout_width="49dp"
android:layout_height="47dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:cropToPadding="true"
android:fitsSystemWindows="true"
android:focusableInTouchMode="false"
android:focusable="false"
android:clickable="false"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="70dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code of main screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="99dp">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="127dp"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/myItemsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_items"
android:textColor="#000000"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/menuImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:clickable="true"
android:focusable="true"
android:onClick="openMenu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu" />
<ImageView
android:id="#+id/notificationImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="true"
android:focusable="true"
android:onClick="notificationClicked"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/notification" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListOfMyItems"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="100dp"
android:alwaysDrawnWithCache="true"
android:animateLayoutChanges="true"
android:choiceMode="singleChoice"
android:clickable="false"
android:clipToPadding="false"
android:descendantFocusability="beforeDescendants"
android:dividerHeight="1dp"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:isScrollContainer="true"
android:layoutDirection="ltr"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:listfooter="#android:layout/simple_list_item_1">
</ListView>
<LinearLayout
android:layout_width="409dp"
android:layout_height="70dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:focusable="auto"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ListOfMyItems">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<TextView
android:id="#+id/addNewItemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-1dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="70dp"
android:clickable="true"
android:focusable="true"
android:onClick="addNewItem"
android:text="#string/add_new_item"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/plusImageView"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/plusImageView"
android:layout_width="67dp"
android:layout_height="74dp"
android:layout_marginStart="56dp"
android:clickable="false"
android:focusable="true"
android:onClick="addNewItem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/plus" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code for AlertDialog adding new item
<?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="300dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#FFFFFF">
<EditText
android:id="#+id/nameOfItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:ems="10"
android:hint="Enter name"
android:inputType="textPersonName"
android:singleLine="true"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="9dp"
android:background="#android:drawable/btn_default_small"
android:onClick="addNewItemButtonClicked"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/nameOfItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Looks like your data is not properly updated inside the Adapter's getView method.
You should try to update the item inside the Adapter when contentView is not null.
For performance reason, there is a pattern called ViewHolder used inside the adapter. Refer to Android listview using ViewHolder for more info. Just create Holder when contentView is null and set the data when it isn't. Hope it helps.
P.S. ListView is considered deprecated. Use RecyclerView instead. It has already built-in ViewHolder pattern and is more flexible
I am searching for a long time on net. But no use. Please help or try to give some ideas how to solve this problem.
1. Bug Description
image link:https://i.stack.imgur.com/ixvdB.jpg
"left":Run the apk
"Right":When I swipe down, the size of the items is changing.
2. Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
List<Member> memberList = new ArrayList<>();
memberList.add(new Member(1, R.drawable.baishatunbeach1, "白沙屯海灘1"));
//add Members
recyclerView.setAdapter(new MemberAdapter(this, memberList));
}
private class MemberAdapter extends RecyclerView.Adapter<MemberAdapter.ViewHolder> {
private Context context;
private List<Member> memberList;
MemberAdapter(Context context, List<Member> memberList) {
this.context = context;
this.memberList = memberList;
}
#Override
public MemberAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_cardview_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(MemberAdapter.ViewHolder holder, int position) {
final Member member = memberList.get(position);
holder.imageId.setImageResource(member.getImage());
holder.textId.setText(String.valueOf(member.getId()));
holder.textName.setText(member.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(member.getImage());
Toast toast = new Toast(context);
toast.setView(imageView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
});
}
#Override
public int getItemCount() {
return memberList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageId;
TextView textId, textName;
ViewHolder(View itemView) {
super(itemView);
imageId = (ImageView) itemView.findViewById(R.id.imageId);
textId = (TextView) itemView.findViewById(R.id.textId);
textName = (TextView) itemView.findViewById(R.id.textName);
}
}
}
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
recyclerview_cardview_item.xml
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:padding="6dp"
app:cardBackgroundColor="#ffdddddd"
app:cardCornerRadius="28dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageId"
android:layout_width="120dp"
android:layout_height="160dp"
android:layout_marginStart="16dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginStart="20dp" />
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
You need to change the height of cardview and linearlayout to wrap_content
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
app:cardBackgroundColor="#ffdddddd"
app:cardCornerRadius="28dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
I've created a custom ListView with a BaseAdapter. When I start the activity, I see an empty ListView with no entries.
Here's the Adapter class:
public class SpeiseplanListAdapter extends BaseAdapter {
private ArrayList<SpeiseplanEntry> data;
private Context context;
public SpeiseplanListAdapter(ArrayList<SpeiseplanEntry> data, Context context) {
this.data = data;
this.context = context;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
TextView day = (TextView) entry.findViewById(R.id.text_day);
TextView date = (TextView) entry.findViewById(R.id.text_date);
TextView firstDish = (TextView) entry.findViewById(R.id.text_first_dish);
TextView secondDish = (TextView) entry.findViewById(R.id.text_second_dish);
SpeiseplanEntry current = data.get(position);
day.setText(current.getDay());
date.setText(current.getDate());
firstDish.setText(current.getFirstDish());
secondDish.setText(current.getSecondDish());
return entry;
}
}
The OnCreate() method :
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, getApplicationContext());
listView.setAdapter(adapter);
...
}
The Layout of a single row:
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp">
<TextView
android:text="MO"
android:layout_width="wrap_content"
android:layout_height="35dp" android:id="#+id/text_day" android:fontFamily="monospace"
android:typeface="monospace" android:textSize="30sp"
android:textColor="#color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp" android:layout_marginLeft="28dp"
app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="28dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="#+id/text_first_dish"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp"
app:layout_constraintLeft_toRightOf="#+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:textAlignment="textStart"
android:text="firstDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="#+id/text_second_dish"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
app:layout_constraintLeft_toRightOf="#+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:text="secondDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:text="21.09."
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/text_date"
android:layout_marginLeft="16dp" app:layout_constraintLeft_toLeftOf="parent" android:textSize="18sp"
android:fontFamily="monospace" android:typeface="monospace" android:textColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="32dp"
android:layout_marginStart="16dp"/>
The layout of the Activity:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.gymoth.goapp.SpeiseplanActivity" tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/list_speiseplan"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" android:divider="#color/divider" android:dividerHeight="1dp"/>
Is there a problem with the ConstraintLayout I used for the single row? Or is the Adapter the problem? Any help is appreciated.
first put a condition at inflate time...
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
}
then use of holder in adapter..
Use like this
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, YourActivityName.this);
listView.setAdapter(adapter);
...
}
May sound like a duplicate issue but have checked the issues posted which is either related to the getItemCount() not set or layout height/width issues.
I'm using a CardView as RecyclerView Item . The adapter fetches 60 records (checked the count )but the CardView is not even shown neither the data .
Using ConstraintLayout in which I'm still a newbie, the design view it shows perfectly. The data is being populated from external APIs and using retrofit library which is working fine.
Activity xml.
<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:scrollbars="vertical"
tools:context="com.techn.rails.Activity.TrainStatus">
/>
<android.support.v7.widget.CardView
android:id="#+id/search_cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/trainno_search_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Train Number"
app:layout_constraintEnd_toStartOf="#+id/trainno_search_et"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
<android.support.v7.widget.AppCompatEditText
android:id="#+id/trainno_search_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="#id/trainno_search_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBaseline_toBaselineOf="#+id/trainno_search_tv"
android:inputType="number"
android:maxLength="6"
android:hint="#string/train_number"
app:layout_constraintHorizontal_weight="1.5"
/>
<android.support.v7.widget.AppCompatEditText
android:id="#+id/doj_et"
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="#id/trainno_search_tv"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/trainno_search_tv"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="12dp"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/doj_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="#+id/doj_et"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/trainno_search_et"
app:layout_constraintBaseline_toBaselineOf="#+id/doj_et"
android:layout_marginTop="12dp"
android:text="#string/select_date"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/doj_btn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="#string/submit_btn"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/train_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result_cardview"
app:layout_constraintBottom_toBottomOf="parent"
android:scrollbars="vertical"
android:padding="10dp"
android:layout_margin="10dp"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Cardview xml
<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.CardView
android:id="#+id/train_status_card_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="center"
android:layout_margin="5dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
android:background="?android:attr/selectableItemBackground">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_stn_code_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_stn_code"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/train_stn_code_lbl"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_sch_arr_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#id/train_stn_code_lbl"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_sch_arr"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#id/train_stn_code"
app:layout_constraintLeft_toRightOf="#id/train_sch_arr_lbl"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_act_dep_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#id/train_sch_arr_lbl"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/train_act_dep"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#id/train_sch_arr_lbl"
app:layout_constraintLeft_toRightOf="#id/train_act_dep_lbl"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Activity
public class TrainStatus extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
#BindView(R.id.doj_btn)
AppCompatButton mDojBtn;
#BindView(R.id.trainno_search_et)
AppCompatEditText mTrainNumber;
#BindView(R.id.submit)
AppCompatButton mSubmit;
#BindView(R.id.train_list)
RecyclerView recyclerView;
private Calendar calendar;
private DatePickerDialog datePickerDialog;
int Year, Month, Day ;
String dateSelected, dateToDisplay;
private TrainStatusRoute mAdapter;
private List<Route> mRoutes;
private static final String TAG = TrainStatus.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_train_status);
ButterKnife.bind(this);
mRoutes = new ArrayList<>();
mAdapter = new TrainStatusRoute(this,mRoutes);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
calendar = Calendar.getInstance();
Year = calendar.get(Calendar.YEAR) ;
Month = calendar.get(Calendar.MONTH);
Day = calendar.get(Calendar.DAY_OF_MONTH);
mDojBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
datePickerDialog = DatePickerDialog.newInstance(TrainStatus.this, Year, Month, Day);
datePickerDialog.setThemeDark(false);
datePickerDialog.showYearPickerFirst(false);
}
});
mSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
requestAPIQuery(mTrainNumber.getText().toString(),dateSelected);
}
});
}
#Override
public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {
dateSelected = String.valueOf(Year)+String.valueOf(Month + 1)+String.valueOf(Day);
}
private void requestAPIQuery (String trainNumber, String doj){
IApi apiService = APIClient.getClient().create(IApi.class);
Call<TrainStatus> call = apiService.getTrainStatus(trainNumber,doj,API_KEY);
call.enqueue(new Callback<TrainStatus>() {
#Override
public void onResponse(Call<TrainStatus> call, Response<TrainStatus> response) {
List<Route> trainStatuses = response.body().getRoute();
mAdapter.notifyDataSetChanged();
Log.d(TAG,"Number of trains"+trainStatuses.size());//getting count 60 here
}
#Override
public void onFailure(Call<com.technovibe.railstat.Model.TrainStatus> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
}
Adapter code
public class TrainStatusRoute extends RecyclerView.Adapter<TrainStatusRoute.RouteViewHolder> {
private Context mContext;
private List<Route> mRoutes;
public static class RouteViewHolder extends RecyclerView.ViewHolder{
#BindView(R.id.train_act_arr)
TextView mActArr;
#BindView(R.id.train_act_dep)
TextView mActDep;
#BindView(R.id.train_sch_arr)
TextView mSchArr;
#BindView(R.id.train_sch_dep)
TextView mSchDep;
public RouteViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public TrainStatusRoute(Context context, List<Route> routeList) {
this.mContext = context;
this.mRoutes = routeList;
}
#Override
public TrainStatusRoute.RouteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.train_status_card, parent, false);
return new RouteViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrainStatusRoute.RouteViewHolder holder, int position) {
Route route = mRoutes.get(position);
holder.mActArr.setText(route.getActarr());
holder.mActDep.setText(route.getActdep());
holder.mSchDep.setText(route.getSchdep());
holder.mSchArr.setText(route.getScharr());
}
#Override
public int getItemCount() {
return mRoutes.size();
}
}
Please bear with me posting lengthy code.
Appreciate any help , thanks in advance.
I guess the problem could be in the card view's width and heights:
<android.support.v7.widget.CardView
android:id="#+id/search_cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
Try changing the width and height values to let's say width match_parent and height to 60 dp and see if the items will appear.
public void onResponse(Call<TrainStatus> call, Response<TrainStatus> response) {
List<Route> trainStatuses = response.body().getRoute();
//Add this line here
mAdapter.setRoutes(trainStatuses);
mAdapter.notifyDataSetChanged();
Log.d(TAG,"Number of trains"+trainStatuses.size());//getting count 60 here
}
And define a setter for your list in your adapter
public void setRoutes(List<Route> routes){
mRoutes = routes;
}