RecyclerView wont display data - android

I am using RecyclerView to show some data in form of list, but the data won't display.
Following is my code, I am using to implement RecyclerView
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
String[] c_names={"INDIA","US","UK","AUSTRALIA","CHINA","JAPAN"};
int[] c_flags={R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india};
private RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;
ArrayList<Country> arrayList=new ArrayList<>();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
int count=0;
for(String Name:c_names){
arrayList.add(new Country(Name,c_flags[count]));
Log.i("ggg", "onCreate: "+arrayList.get(count).getName()+" "+arrayList.get(count).getFlag_id());
count++;
}
recyclerAdapter=new RecyclerAdapter(arrayList);
recyclerView.setAdapter(recyclerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem=menu.findItem(R.id.ction_search);
SearchView searchView= (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
newText=newText.toLowerCase();
ArrayList<Country> newList=new ArrayList<>();
for(Country country:arrayList){
String name=country.getName().toLowerCase();
if(name.contains(newText)){
newList.add(country);
}
}
recyclerAdapter.setFilter(newList);
return true;
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
ArrayList<Country> arrayList=new ArrayList<>();
public RecyclerAdapter(ArrayList<Country> arrayList) {
this.arrayList = arrayList;
}
#Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,
parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
holder.c_flags.setImageResource(arrayList.get(position).getFlag_id());
holder.c_name.setText(arrayList.get(position).getName());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView c_flags;
TextView c_name;
public MyViewHolder(View itemView) {
super(itemView);
c_flags=itemView.findViewById(R.id.flag);
c_name=itemView.findViewById(R.id.name);
}
}
public void setFilter(ArrayList<Country> filter){
// filter=new ArrayList<>();
arrayList.addAll(filter);
notifyDataSetChanged();
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">
<include layout="#layout/toolbar_layout"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</LinearLayout>
row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="4dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:elevation="5dp"
app:cardCornerRadius="8dp"
android:background="#9E9E9E"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#9E9E9E">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:scaleType="fitXY"
android:id="#+id/flag"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/flag"
android:layout_marginLeft="20dp"
android:gravity="center"
android:layout_centerVertical="true"
android:textSize="15dp"
android:textStyle="bold"
android:id="#+id/name"
android:textColor="#000000"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
toolbar_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/colorPrimary"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
My RecyclerView is blank.
I am showing the arrays mentioned in MainActivity in my RecyclerView. I tried a lot but couldn't figure out what am I doing wrong? Any help would be greatly appreciated.

You are missing orientation in parent layout.
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">
<include layout="#layout/toolbar_layout"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</LinearLayout>

Related

App crashes when trying to dynamically change the textView's text in a Bottom App Bar

I'm trying to dynamically change the text of a textView placed within a BottomAppBar ,but this causes my app to crash. The number of items of the recyclerview which are selected are to be displayed in the textView. This is done in the updateViewCounter(). Everything works fine when the number of selected items are displayed with a Toast.
Here's my code:
public class PhotosFragment extends Fragment {
ArrayList<ImageModel > imageList;
ArrayList<ImageModel> selectedList;
TextView selected_text;
RecyclerView imageRecyclerView;
ImageAdapter imageAdapter;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_photos, container, false);
runtimePermission();
return view;
}
private void runtimePermission() {
Dexter.withContext(getContext()).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
#Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
init();
getAllImages();
setImageList();
}
#Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(getContext(), "Permission is Required!", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
public void init() {
imageRecyclerView = (RecyclerView) view.findViewById(R.id.photos_recycler);
selected_text = view.findViewById(R.id.selected_textView);
imageList = new ArrayList<>();
selectedList = new ArrayList<>();
}
public void setImageList() {
imageRecyclerView.setLayoutManager((new GridLayoutManager(getContext(), 4)));
imageAdapter = new ImageAdapter(getContext(), imageList);
imageRecyclerView.setAdapter(imageAdapter);
imageAdapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
#Override
public void onItemViewClick(int position, View v){
try {
if (!imageList.get(position).isSelected) {
selectImage(position);
} else {
unSelectImage(position);
}
} catch (ArrayIndexOutOfBoundsException ed) {
ed.printStackTrace();
}
}
});
};
public void selectImage(int position) {
imageList.get(position).setSelected(true);
imageAdapter.notifyDataSetChanged();
prepareSelection(position);
}
public void prepareSelection(int position) {
if (!selectedList.contains(imageList.get(position))) {
selectedList.add(imageList.get(position));
} else {
selectedList.remove(imageList.get(position));
}
updateViewCounter();
}
public void unSelectImage(int position) {
imageList.get(position).setSelected(false);
imageAdapter.notifyDataSetChanged();
prepareSelection(position);
}
private void updateViewCounter() {
int counter = selectedList.size();
selected_text.setText(valueOf(counter));
}
public void getAllImages(){// queries mediastore }
}
And here's the activity_main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
tools:ignore="MissingConstraints" >
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#color/colorPrimary"
app:tabTextColor="#color/white"
app:tabIndicatorColor="#color/white"
app:tabMode="scrollable"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:tintMode="#color/white"
android:src="#android:drawable/arrow_up_float"
app:fabSize="auto"
app:layout_anchor="#id/bottom_app_bar"
app:fabAlignmentMode="end" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
style="#style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="0dp"
app:fabCradleRoundedCornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_app_bar_menu">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/selected_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="selected"
android:textColor="#fff"
android:layout_centerInParent="false"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Title"/>
</RelativeLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_nav" />
</androidx.drawerlayout.widget.DrawerLayout>

FirebaseUI RecycleView only displays first item

I updated to firebase-ui 6.3.0 which doesn't have the populateViewHolder method anymore. I changed the code according to the examples I found online. RecycleView didn't show anything until I added adapter.startListening() to the onStart method of my home activity.
The problem is that only 1 item is displayed and can't figure it out why. Since I use linearLayoutManager.setStackFromEnd(true) the item displayed is the last item, but if I comment out that line the first item is displayed (and the context manu doesn't work either).
I checked some similiar StackOverflow questions but couldn't find the problem.
Firebase RecyclerView OnDataChanged only last element is Shown
Recycler View showing only very last item added to realtime database
Here are my classes and xmls:
HomeActivity:
private RecyclerView transactionsRecyclerView;
private TransactionRecordAdapter transactionRecordAdapter;
private FirebaseHelper firebaseHelper;
private Query query;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseHelper = new FirebaseHelper();
query = this.firebaseHelper.getTransactionRecordsOrderedByDateQuery();
// FirebaseDatabase.getInstance().getReference().child(ConstantStringValue.DB_CHILD_TRANSACTION_RECORDS).child(auth.getCurrentUser().getUid())
// .orderByChild("date");
transactionsRecyclerView = findViewById(R.id.transactionsRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
transactionsRecyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, VERTICAL);
transactionsRecyclerView.addItemDecoration(itemDecoration);
registerForContextMenu(transactionsRecyclerView);
}
#Override
protected void onStart() {
super.onStart();
transactionRecordAdapter = new TransactionRecordAdapter(TransactionRecord.class, query, this);
transactionsRecyclerView.setAdapter(transactionRecordAdapter);
transactionRecordAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
transactionRecordAdapter.stopListening();
}
TransactionRecordAdapter:
private int position;
private String selectedItemId;
private Context context;
public TransactionRecordAdapter(Class<TransactionRecord> modelClass, Query ref, Context context) {
super(new FirebaseRecyclerOptions.Builder<TransactionRecord>()
.setQuery(ref, modelClass)
.build());
this.context = context;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public String getSelectedItemId() {
return selectedItemId;
}
public void setSelectedItemId(String selectedItemId) {
this.selectedItemId = selectedItemId;
}
#Override
protected void onBindViewHolder(#NonNull final TransactionRecordViewHolder holder, int position,
#NonNull TransactionRecord model) {
holder.itemView.setOnLongClickListener(v -> {
setPosition(holder.getAdapterPosition());
setSelectedItemId(holder.getId());
return false;
});
holder.setAnnouncement(model.getAnnouncement());
...
holder.setId(this.getRef(position).getKey());
}
#NonNull
#Override
public TransactionRecordViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transrecord, parent, false);
return new TransactionRecordViewHolder(view);
}
#Override
public void onViewRecycled(TransactionRecordViewHolder holder) {
holder.itemView.setOnLongClickListener(null);
super.onViewRecycled(holder);
}
TransactionRecordViewHolder:
private View view;
private String id;
public TransactionRecordViewHolder(View itemView) {
super(itemView);
view = itemView;
view.setOnCreateContextMenuListener(this);
}
// ... setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
contextMenu.add(Menu.NONE, R.id.editTransHomeContext,
Menu.NONE, "Szerkesztés");
contextMenu.add(Menu.NONE, R.id.deleteTransHomeContext,
Menu.NONE, "Törlés");
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="hu.beczdev.cashbudget.activity.HomeActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/transactionsRecyclerView" />
</LinearLayout>
transrecord.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" >
<TextView
android:id="#+id/transAmountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:paddingStart="25dp"
android:paddingTop="10dp"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/transDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingTop="10dp"
android:paddingEnd="25dp"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingStart="25dp"
android:textStyle="bold"
android:id="#+id/transCatNameTextView"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:textSize="15sp"
android:paddingStart="25dp"
android:id="#+id/transCommentView"/>
</LinearLayout>
</LinearLayout>
I think your issue not in firebase as it already returned a row, you at least need to wrap_content of the height of your list item to not occupy the entire height of the RecyclerView.
so in transrecord.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="wrap_content" >

RecyclerView strange background color

As the title says, I have a problem with RecyclerView and CardView.
During the development process, dark frames appeared over RecyclerView that isn't defined anywhere. Any advice how can I get rid of it?
CardView has two textViews and MapView.
SingleRun is a simple object with 2 Strings.
MainActivity
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Main Activity onCreate");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new SingleRunsAdapter(new SingleRunProvider().readData(), getApplicationContext()));
CardViewRecyclerViewItem
CardViewRecyclerViewItem
public class CardViewRecyclerViewItem extends CardView {
protected MapView mapView;
protected TextView distance;
protected TextView time;
public CardViewRecyclerViewItem(Context context) {
this(context, null);
}
public CardViewRecyclerViewItem(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(getContext()).inflate(R.layout.card_view_single_run, this);
mapView = (MapView) view.findViewById(R.id.single_run_map_mapview);
distance = (TextView) view.findViewById(R.id.details_distance_textview);
time = (TextView) view.findViewById(R.id.details_time_textview);
}
public void cardViewOnCreate(Bundle savedInstanceState) {
if (mapView != null) {
mapView.onCreate(savedInstanceState);
}
}
public void cardViewOnResume() {
if (mapView != null) {
mapView.onResume();
}
}}
CardViewHolder
public class CardViewHolder extends RecyclerView.ViewHolder {
private CardViewRecyclerViewItem mCardViewRecyclerViewItem;
public CardViewHolder(CardViewRecyclerViewItem cardViewRecyclerViewItem) {
super(cardViewRecyclerViewItem);
mCardViewRecyclerViewItem = cardViewRecyclerViewItem;
}
public void cardViewRecyclerViewItemOnResume() {
if (mCardViewRecyclerViewItem != null) {
mCardViewRecyclerViewItem.cardViewOnResume();
}
}}
SingleRunAdapter
public class SingleRunsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
private final List<SingleRun> singleRuns;
public SingleRunsAdapter(List<SingleRun> singleRuns, Context context) {
this.singleRuns = singleRuns;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardViewRecyclerViewItem cardViewRecyclerViewItem = new CardViewRecyclerViewItem(context);
cardViewRecyclerViewItem.cardViewOnCreate(null);
return new CardViewHolder(cardViewRecyclerViewItem);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
cardViewHolder.cardViewRecyclerViewItemOnResume();
}
#Override
public int getItemCount() {
return singleRuns.size();
}}
activity_main.xml
<android.support.design.widget.CoordinatorLayout 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="com.coderspeak.lightweightrunningtracker.single_run.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.coderspeak.lightweightrunningtracker.single_run.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
card_view_single_run.xml
<android.support.v7.widget.CardView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.maps.MapView xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/single_run_map_mapview"
android:layout_width="match_parent"
android:layout_height="144dp"
map:liteMode="true"
map:mapType="normal"
tools:context=".MapsActivity" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:id="#+id/single_run_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="00:12:34"
android:textAlignment="center" />
<TextView
android:id="#+id/single_run_distance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3.14km"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
How it looks:
RecyclerView with strange background color
If you will comment both LinearLayouts with content in card_view_single_run.xml and mapView line in CardViewRecyclerViewItem you will see this:
THIS
This is more strange in my opinion, because even if empty cards, recycler have some background.
Thank you for any help. If necessary, I can provide more code.
The best way to diagnose this kind of errors IMO is using Layout Inspector tool in Android Studio:
Try the following
in your MainActivity
change context from getApplicationContext() to MainActivity.this
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new SingleRunsAdapter(new SingleRunProvider().readData(), MainAcitivity.this));

onCreateViewHolder not called RecyclerView

I am using Recyclerview and my onCreateViewHolder and onBindViewHolder are not called. I am getting the data but it is not displaying.
My adapter class
public class DishesAdapter extends RecyclerView.Adapter<DishesAdapter.MyViewHolder> {
private Context mContext;
List<List> DialogList = new ArrayList<List>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView dishnames;
public RatingBar dishratings;
public ImageView dishimages;
private ImageLoader mLoader;
public MyViewHolder(View view) {
super(view);
Log.d("follower2","hi");
dishnames = (TextView)view.findViewById(R.id.dishname);
dishimages = (ImageView)view.findViewById(R.id.dishimage);
dishratings=(RatingBar)view.findViewById(R.id.dishrating);
}
}
public DishesAdapter(Context mContext, List objects) {
super();
Log.d("follower1","hi");
this.mContext = mContext;
this.DialogList = objects;
Log.d("follower1",objects.toString());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.recipe_list, parent, false);
Log.d("follower","hi");
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final int i=position;
Log.d("follower","hi");
List dialog = DialogList.get(i);
Log.d("follower",dialog.toString());
String dishid = dialog.get(0).toString();
final String dishname = dialog.get(1).toString();
//byte[] dishimage = Base64.decode(dialog.get(2).toString(), Base64.DEFAULT);
String dishimage=dialog.get(2).toString();
String rating=dialog.get(3).toString();
holder.dishnames.setText(dishname);
holder.dishratings.setRating(Float.parseFloat(rating));
holder.mLoader.DisplayImage(dishimage.replaceAll(" ", "%20"),holder.dishimages);
}
#Override
public int getItemCount() {
return DialogList.size();
}}
Recyclerview
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(mLayoutManager);
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mealplanner.com.main.mealplanner.MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
Row List
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp"
android:id="#+id/cv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/dishimage"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginRight="6dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_margin="10dp"
android:src="#drawable/portraitlanding"
/>
<TextView
android:id="#+id/dishname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_toRightOf="#+id/dishimage"
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
android:text="Andrew" />
<RatingBar
android:id="#+id/dishrating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_below="#+id/dishname"
android:layout_toRightOf="#+id/dishimage"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
DishesAdapter constructor is being called. I am getting follower1 log along with the list of items.

Nested RecyclerView throws NPE

When I make Nested RecyclerView in android application, It is throws NPE.
activity_main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/groupList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
group_row.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="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/shortcutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</LinearLayout>
Here is the exception
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:16538)
I need help. Why It is NullPointerException? Can I Fix this problem?
UPDATE
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView groupList = (RecyclerView) findViewById(R.id.groupList);
groupList.setAdapter(new GroupAdapter());
}
GroupAdapter.java
public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.group_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
Add this:
LinearLayoutManager llm1 = new LinearLayoutManager(this);
groupList.setLayoutManager(llm1);

Categories

Resources