How to change the font in recycler view? - java

I have a list of Button type TextViews like set in a horizontal RecyclerView and I'm trying to change the font of the text of these buttons and it's not working.
I've changed the font in the XML file of my item to exo, and tried to use many tips of view instead of the TextView but i can't change the font anyway.
is there any limitation about customization of the view when using RecyclerView or I'm doing it wrong?
p.s. I can't change the padding of the TextView either.
My RecyclerView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="32dp">
<TextView
android:id="#+id/rpgListBtn"
android:layout_width="130dp"
android:layout_height="32dp"
android:layout_weight="1"
android:background="#drawable/ic_rpg_list"
android:fontFamily="#font/exo_teste"
android:gravity="center"
android:includeFontPadding="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="D&D"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
My RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rpgListView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:clipToPadding="false"
android:paddingLeft="22dp"
android:paddingRight="8dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/escolha_um_" />
my adapter:
package com.example.rpglink.Adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.rpglink.R;
import java.util.List;
public class RpgListAdapter extends RecyclerView.Adapter<RpgListAdapter.MyViewHolder> {
private List<String> titulos;
public RpgListAdapter(List<String> titulos) {
this.titulos = titulos;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.rps_list_view_holder,parent,false);
return new MyViewHolder(itemLista);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
String titulo = titulos.get(position);
holder.titulo.setText(titulo);
}
#Override
public int getItemCount() {
return titulos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titulo;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.rpgListBtn);
}
}
}
the implementation of recycler view in the activity:
package com.example.rpglink.Activitys;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.rpglink.Adapters.RpgListAdapter;
import com.example.rpglink.Helpers.HorizontalSpaceItemDecoration;
import com.example.rpglink.R;
import java.util.ArrayList;
import java.util.List;
public class HomeC extends AppCompatActivity {
private RecyclerView recyclerView;
private RpgListAdapter adapter;
private List<String> lista = new ArrayList<>();
public HomeC() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_c);
criarTitulos();
recyclerView = findViewById(R.id.rpgListView);
recyclerView.setHasFixedSize(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
adapter = new RpgListAdapter(lista);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new HorizontalSpaceItemDecoration(32));
}
public void criarTitulos(){
this.lista.add("D&D");
this.lista.add("CyberPunk");
this.lista.add("Call of Cthulhu");
this.lista.add("Tormenta");
}
}

You can set it in the ViewHolder constructor using Typeface.createFromAsset
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titulo;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.rpgListBtn);
titulo.setTypeface(Typeface.createFromAsset(itemView.getContext().getAssets(),
"font/exo_teste"));
}
}

Related

Fit 2 textViews in an adapter , java , android

I am creating a note-taking app for myself, I created an adapter to show notes on the main screen with a title view and text view but the app crashes when I launch it
here is RecyclerViewAdapter.java.
I am using basic java, I don't have much experience so please try to explain simply so could I understand,
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.myViewHolder> {
String[] arr;
String[] arr2;
public RecyclerViewAdapter(String[] arr, String[] arr2) {
this.arr = arr;
this.arr2 = arr2;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent,
false);
return new myViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull myViewHolder holder, int position) {
holder.titleView.setText(position);
holder.textView.setText(position);
}
#Override
public int getItemCount() {
return arr.length;
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView titleView;
TextView textView;
public myViewHolder(#NonNull View itemView) {
super(itemView);
titleView = itemView.findViewById(R.id.text_title_view);
textView = itemView.findViewById(R.id.text_text_view);
}
}
}
Here is the XML code,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="start"
android:text="Title" />
<TextView
android:id="#+id/text_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="start"
android:text="Body will go here" />
</LinearLayout>
and here is my main activity
package com.example.keepnotes;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
RecyclerView.LayoutManager layoutManager;
private RecyclerView recyclerView;
private Toolbar toolbar1;
RecyclerViewAdapter recyclerViewAdapter;
String []arr = {"First Heading ","Second Heading"};
String []arr2 = {"First body ","Second body"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting up recycler view
recyclerView = findViewById(R.id.recycler_view);
layoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter= new RecyclerViewAdapter(arr, arr2);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
please guide me that where I am mistaken.
update: Thanks to #stealthoust, App is running but can't find any view on the screen, what can I do about that.
Try to change this on onBindViewHolder
holder.titleView.setText(arr[position]); holder.textView.setText(arr2[position]);

How to pass edit text value from adapter to activity?

I have a recycler view adapter which takes input from user in edittext. I want to pass those values as a list back to the activity in which the recycler view is present.
I need this list in activity to send as a parameter to api through retrofit.
Create an interface like this:
interface IList {
void addToList(String editTextValue);
}
Implement this interface in your Activity:
class MainActivity extends AppCompatActivity implements IList {
#Override
public void addToList(String editTextValue) {
//TODO("logic for adding to list and sending")
}
}
Add to Adapter's constructor Activity, that implementing IList interface, as paramert:
public Adapter(IList listener){
this.listener = listener;
}
Execute addToList method in your adapter:
#Override
public void onBindViewHolder(NewViewHolder holder, int position) {
holder.sendButton.setOnClickListener .setOnClickListener(v -> {
String newText = holder.editText.text.toString()
listener.addToList(newText)
});
}
There's a working sample.
MainActivity.java
```
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Adapter adapter = new Adapter();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(adapter);
EditText editText = findViewById(R.id.editText);
editText.setOnEditorActionListener((textView, actionId, keyEvent) -> {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
// add item
adapter.addItem(editText.getText().toString());
// clear text input
textView.setText("");
handled = true;
}
return handled;
});
}
}
```
activity_main.xml
```
<?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="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Insert text.."
android:imeOptions="actionDone"
android:inputType="text"
app:layout_constraintBottom_toTopOf="#+id/recyclerView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
Adapter.java
```
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<String> items = new ArrayList<>();
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(android.R.layout.test_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder holder, int position) {
String currentItem = items.get(position);
holder.textView.setText(currentItem);
}
public void addItem(String item) {
// add at first position - replace with your desired index
int index = 0;
this.items.add(index, item);
notifyItemInserted(index);
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
}
}
You can use the "done" key on the keyboard to notify the adapter that an item has been added.
To display the "done" button in the xml it is necessary to use these two instructions in the edittext
android:imeOptions="actionDone"
android:inputType="text"

How to fix InflateException:Error inflating class com.cepheuen.elegantnumberbutton.view.ElegantNumberButton

Elegant Class:
public class MainHome extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
FirebaseDatabase firebaseDatabase;
DatabaseReference reference;
ProgressDialog progressDialog;
private List<Vegitable> vegitablesList;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
//Buttons
Button addButton;
ElegantNumberButton countButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_home);
progressDialog = new ProgressDialog(MainHome.this);
progressDialog.setMessage("Please waiting...!");
progressDialog.show();
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Vegitables");
setSupportActionBar(toolbar);
recyclerView =(RecyclerView)findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//get firebase
firebaseDatabase=FirebaseDatabase.getInstance();
reference=firebaseDatabase.getReference("vegetables");
vegitablesList=new ArrayList<>();
loadVegitables();
progressDialog.dismiss();
//Buttons for quntity and add to cart
countButton=(ElegantNumberButton)findViewById(R.id.number_counter);
if(countButton != null) {
countButton.setOnClickListener(new ElegantNumberButton.OnClickListener() {
#Override
public void onClick(View view) {
String num = countButton.getNumber();
Toast.makeText(MainHome.this, "Quantity:" + num, Toast.LENGTH_SHORT).show();
}
});
}
}
I am setting up Elegant number Button in my application. It will show an exception like com.cepheuen.elegantnumberbutton.view.ElegantNumberButton cannot be cast to android.widget.TextView. I want to display each type vegetable when i was clicked add button Elegant button was visible to user. I dont know how to rectify my problem. Thank u.
I am new to android.
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.layout_listitems, parent, false);
return new MyViewHolder(view);
}
XML
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="2dp"
android:elevation="#dimen/cell"
android:id="#+id/parent_layout">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_foreground" />
<TextView
android:id="#+id/nameVegitable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="10dp"
android:textColor="#5396FA"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:id="#+id/inrlogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:textColor="#B87461"/>
<TextView
android:id="#+id/price"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="53dp"
android:textColor="#F31F6C"
android:textSize="20sp" />
<Button
android:id="#+id/add_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="265dp"
android:layout_marginTop="25dp"
android:textSize="15sp"
android:onClick="addCart"
android:text="ADD"
android:background="#drawable/design_button"/>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="#+id/number_counter"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginStart="265dp"
android:layout_marginTop="25dp"
android:background="#drawable/design_button"
android:textSize="8sp"
android:visibility="invisible"
app:initialNumber="0"
app:finalNumber="20"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Java
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.greenfresh.model.Vegitable;
import com.squareup.picasso.Picasso;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Vegitable> listVegitables;
private Context context;
public MyAdapter(List<Vegitable> listVegitables, Context context) {
this.listVegitables = listVegitables;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.layout_listitems, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
int resourceId = context.getResources().getIdentifier(listVegitables.get(position).getImage(), "drawable",
context.getPackageName());
holder.image.setImageResource(resourceId);
holder.vegitableName.setText(listVegitables.get(position).getName());
holder.price.setText(listVegitables.get(position).getPrice());
holder.qunType.setText(listVegitables.get(position).getQunType());
}
#Override
public int getItemCount() {
return listVegitables.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public TextView vegitableName;
public TextView price;
public TextView qunType;
public ElegantNumberButton countButton;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
image=(ImageView)itemView.findViewById(R.id.image);
vegitableName=(TextView)itemView.findViewById(R.id.nameVegitable);
price=(TextView)itemView.findViewById(R.id.price);
qunType=(TextView)itemView.findViewById(R.id.inrlogo);
countButton=(ElegantNumberButton)itemView.findViewById(R.id.number_counter);
}
}
}
Elegant Number Class:
countButton=(ElegantNumberButton)findViewById(R.id.number_counter);
if(countButton != null) {
countButton.setOnClickListener(new ElegantNumberButton.OnClickListener() {
#Override
public void onClick(View view) {
String num = countButton.getNumber();
Toast.makeText(MainHome.this, "Quantity:" + num, Toast.LENGTH_SHORT).show();
}
});
}
I expect the output Elegant Number button on each vegetable type.

Recycle view in Android java

Recycler View is not iterating to the length of array.
Here is the code, what am i missing. i followed a guide describing the entire process, in that guide all items of the array is visible in the list view.
Here is the main activity file that host the recyclerView, below the adapter java class that filter through the array and pop out the list View. please help me
PickupActivity.java
package com.demo.driverconsole;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerViewAccessibilityDelegate;
import android.util.Log;
import android.widget.Adapter;
import android.widget.Toast;
import java.util.ArrayList;
public class pickupActivity extends AppCompatActivity {
private static final String TAG = "pickupActivity";
private RecyclerView studentList;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> myDataset = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pickup);
myDataset.add("John Doe");
myDataset.add("Jane Doe");
myDataset.add("Susan");
initRecycler();
}
public void initRecycler() {
Log.d(TAG, "Called");
RecyclerView recyclerView = findViewById(R.id.recyclerView);
StudentListAdapter adapter = new StudentListAdapter(myDataset);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
StudentListAdapter.java
package com.datastoneglobal.driverconsole;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ViewHolder> {
private static final String TAG = "StudentListAdapter";
private ArrayList<String> names = new ArrayList<>();
public StudentListAdapter(ArrayList<String> names) {
Log.d(TAG, "StudentListAdapter: " + names);
this.names = names;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.text.setText(names.get(position));
}
#Override
public int getItemCount() {
return names.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
RelativeLayout layout;
public ViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.textView);
}
}
}
ActivityPickup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
StudentItem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/layout"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Loading ...">
</TextView>
</RelativeLayout>
The code looks right the only problem i find is the hight of your RelativeLayout in StudentItem.xml
Change the hight of your RelativeLayout to android:layout_height="wrap_content" in your StudentItem.xml
SAMPLE CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/layout"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Loading ...">
</TextView>
</RelativeLayout>
EDIT
Use this
private StudentListAdapter adapter;
Instead of this
private RecyclerView.Adapter adapter;
⬇ Remove this code ⬇
public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ViewHolder>
{
// ....
}
⬇ use this code ⬇
public class StudentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
// ....
}
because you extend :-
public class StudentListAdapter
extends RecyclerView.Adapter<StudentListAdapter.ViewHolder>
and you extend at here :- public class ViewHolder extends RecyclerView.ViewHolder

"Demo App has Stopped" but code compiles fine?

i recently tried adding in a RecycleView to my android app, and since then the Demo app just refuses to open on the emulator, all of the code compiles and i cant find the area in which I've gone wrong, ill post the relevant (I think, because im not sure as to whats wrong) code below, but will also provide a download link to the project in case that is easier to anyone.
I know there's a lot of code but im super stumped as to where this has gone wrong and caused the demo app to not work at all, im fairly certain it lies within the RecycleView but again, im genuinely not sure what so any guidance is greatly appreciated
Download Link https://www.dropbox.com/s/7uott0yaiktieym/JavaMini.zip?dl=0
Image List adapter Class
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ImageListAdapter
extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
TextView imageTitle;
ImageView mainImage;
public ViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (ImageView) itemView.findViewById(R.id.mainImage);
}
}
public ArrayList<String> imageList = new ArrayList<String>();
//context
private Context context;
public ImageListAdapter(Context context) { this.context = context;}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.cell_image_card,
parent, false);
ImageListAdapter.ViewHolder vh = new
ImageListAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String image = imageList.get(position);
holder.imageTitle.setText(image);
}
#Override
public int getItemCount() {
return 5;
}
}
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private ImageListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cell_image_card);
recyclerView = (RecyclerView) findViewById(R.id.imageList);
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new ImageListAdapter(this);
recyclerView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/imageList"
android:layout_width="361dp"
android:layout_height="484dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
</LinearLayout>

Categories