java.lang.NullPointerException with RecyclerView and CardView - java

I am trying to make dynamical list using RecyclerView and CardView using tutorial from this site and I am getting this error:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at kawi15.myapplication.CustomAdapter.getItemCount(CustomAdapter.java:66)
I run debugger to check that I have results from tmbd API and this is what I received:
http://prntscr.com/vdjj1h
This is my Fragment class activity when I want to create that list:
public class FragmentOne extends Fragment {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static List<MovieDb> data;
static View.OnClickListener myOnClickListener;
public static FragmentOne newInstance() {
FragmentOne fragment = new FragmentOne();
return fragment;
}
public class MovieTask extends AsyncTask<Void, Void, List<MovieDb>> {
#Override
protected List<MovieDb> doInBackground(Void... voids) {
MovieResultsPage movies = new TmdbApi("f753872c7aa5c000e0f46a4ea6fc49b2").getMovies().getUpcoming("en-US", 1, "US");
List<MovieDb> listMovies = movies.getResults();
return listMovies;
}
#Override
protected void onPostExecute(List<MovieDb> movieDb) {
data = movieDb;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
MovieTask mt = new MovieTask();
mt.execute();
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View returnView = inflater.inflate(R.layout.fragment_one, container, false);
recyclerView = (RecyclerView) returnView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext()); // ???
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new CustomAdapter(data);
recyclerView.setAdapter(adapter);
return returnView;
}
}
and my Adapter class:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private List<MovieDb> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewVersion;
ImageView imageViewIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.text1);
this.textViewVersion = (TextView) itemView.findViewById(R.id.text2);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
}
}
public CustomAdapter(List<MovieDb> data) {
this.dataSet = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);
//view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
TextView textViewVersion = holder.textViewVersion;
ImageView imageView = holder.imageViewIcon;
Glide.with(imageView).load("http://image.tmdb.org/t/p/w500" + dataSet.get(listPosition).getPosterPath()).into(imageView);
textViewName.setText(dataSet.get(listPosition).getOriginalTitle());
textViewVersion.setText(dataSet.get(listPosition).getReleaseDate());
//imageView.setImageResource(dataSet.get(listPosition).);
}
#Override
public int getItemCount() {
return dataSet.size();
}
}
I stucked with my code with this and I dont have idea where is mistake.

Initially dataSet is null. So using dataSet.size() is throwing NullPointerException.
Change getItemCount method in the following way.
#Override
public int getItemCount() {
if(dataSet == null){
return 0;
}
return dataSet.size();
}
This is removing error but its still doesnt work because data is null making dataSet null too. I dont know why data is null, I need help with this.
Reason for that could be, you are creating CustomAdapter object in onCreateView method, which is kind of called before you AsyncTask has finished. So, when onCreateView is being called, data is null.
To fix it, You can add a setter method in your CustomAdapter, which will allow you to update dataSet, even after the adapter object is created. Then you will have to update the dataSet in your onPostExecute method & after that use notifyDataSetChanged on the adapter, to reflect the changes.

At the start dataSet will be null until you assign it a value, and then feed it back with data, so change getItemCount() to below to return 0 data when it's null
#Override
public int getItemCount() {
return dataSet == null ? 0 : dataSet.size();
}
Ok, there is no error and application is launching but still I dont have list and this is my problem
You need to update the changes to your adapter with notifyDataSetChanged()
#Override
protected void onPostExecute(List<MovieDb> movieDb) {
data = movieDb;
adapter.notifyDataSetChanged();
}
Now two possible things, either TmdbApi("...").getMovies() doesn't really return data, in this case you need to debug it and check if it returns data or not.
The other thing, move the code of the AsyncTask execution after creating the RecyclerView instance in onCreateView and Initialize the adapter in onPostExecute();
Here are after the changes:
public class MovieTask extends AsyncTask<Void, Void, List<MovieDb>> {
#Override
protected List<MovieDb> doInBackground(Void... voids) {
MovieResultsPage movies = new TmdbApi("f753872c7aa5c000e0f46a4ea6fc49b2").getMovies().getUpcoming("en-US", 1, "US");
List<MovieDb> listMovies = movies.getResults();
return listMovies;
}
#Override
protected void onPostExecute(List<MovieDb> movieDb) {
data = movieDb;
adapter = new CustomAdapter(data);
recyclerView.setAdapter(adapter);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View returnView = inflater.inflate(R.layout.fragment_one, container, false);
recyclerView = (RecyclerView) returnView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext()); // ???
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
MovieTask mt = new MovieTask();
mt.execute();
return returnView;
}

Related

My recycler view not shown for the first time in fragment

I am a beginner at android development. I tried to display data on Recycle View using MVVM. When I opened the fragment the first time it is empty, the second time it showed data. Adapter not called the first time. How to fix this.
Fragment class
public class fragmentEmployee extends Fragment {
private FragmentEmployeeBinding binding;
private ViewGroup viewGroup;
private View view;
private RecyclerView mRecycleView;
private UserAdapter mAdapter;
private EmployeeRegisterViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_employee, viewGroup, false);
view = binding.getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getContext(), "onViewCreated", Toast.LENGTH_SHORT).show();
mRecycleView = binding.recyleViewEmployee;
viewModel = new ViewModelProvider(this).get(EmployeeRegisterViewModel.class);
viewModel.init();
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
}
private void initRecycleView() {
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecycleView.setLayoutManager(layoutManager);
mRecycleView.setAdapter(mAdapter);
}
}
UserAdapter class
public class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
public List<employeeModel> employeeModelList= new ArrayList<>();
public Context context1;
public UserAdapter(Context context,List<employeeModel> employeeModels){
employeeModelList = employeeModels;
context1 = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
(ViewHolder)holder).username.setText(employeeModelList.get(position).getName());
}
#Override
public int getItemCount() {
return employeeModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView username;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.userText);
}
}
}
Try reordering like this.
Remove
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
from initRecycleView
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
initRecycleView();
mAdapter.notifyDataSetChanged();
Above method will work if the list declared inside adapter has any change. So, calling above method without changing adapter's list is useless.
Add a method to UserAdapter class to update list data. Like:
public void updateData(List<employeeModel> updatedList){
employeeModelList.clear();
employeeModelList.addAll(updatedList);
notifyDataSetChanged();
}
And call this method inside observer. Like:
viewModel.getEmployee().observe(getViewLifecycleOwner(), new
Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.updateData(employeeModels);
}
});
where are you get your network data?
If your get in here -> viewModel.init();
I think it just timing problem.
Just change code sequence
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
viewModel.init();

E/RecyclerView: No adapter attached; skipping layout , Not duplicated nothing works ( no frame is shown)

E/RecyclerView: No adapter attached; skipping layout
I've seen almost a similar question like this on the site but they are kind of different problems , because in my case nothing works. I tried that solution but NO LUCK
I've clearly attached the adapter why this error is shown and in my case nothing happens , no fragment is shown
public class MessageFragment extends Fragment {
private RecyclerView recyclerViewMessages;
private RecyclerView.Adapter adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{ return inflater.inflate(R.layout.fragment_messages, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Messages");
recyclerViewMessages = (RecyclerView) view.findViewById(R.id.recyclerViewMessages);
recyclerViewMessages.setHasFixedSize(true);
recyclerViewMessages.setLayoutManager(new LinearLayoutManager(getActivity()));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Messages> call = service.getMessages(SharedPrefManager.getInstance(getActivity()).getUser().getId());
call.enqueue(new Callback<Messages>() {
#Override
public void onResponse(Call<Messages> call, Response<Messages> response) {
adapter = new MessageAdapter(response.body().getMessages(), getActivity());
recyclerViewMessages.setAdapter(adapter);
Log.d("Tag","onResponse" );
}
#Override
public void onFailure(Call<Messages> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
the error is (as you can see i get the error onResponse so there shouldn't be a problem with API :
E/RecyclerView: No adapter attached; skipping layout
D/Tag: onResponse
Just in case if you need to see the adapter class:
public class MessageAdapter extends
RecyclerView.Adapter<MessageAdapter.ViewHolder> {
private List<Message> messages;
private Context mCtx;
public MessageAdapter(List<Message> messages, Context mCtx) {
this.messages = messages;
this.mCtx = mCtx;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_messages, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(MessageAdapter.ViewHolder holder, int position) {
final Message message = messages.get(position);
holder.textViewName.setText(message.getFrom());
holder.textViewTitle.setText(message.getTitle());
holder.textViewMessage.setText(message.getMessage());
holder.textViewTime.setText(message.getSent());
}
#Override
public int getItemCount() {
return messages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public TextView textViewTitle;
public TextView textViewMessage;
public TextView textViewTime;
public ViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewTitle = (TextView) itemView.findViewById(R.id.textViewTitle);
textViewMessage = (TextView) itemView.findViewById(R.id.textViewMessage);
textViewTime = (TextView) itemView.findViewById(R.id.textViewTime);
}
}
}
Is it possible that the problem could be caused because of something else (another part of the program?

Crash in adapter, in method getItemCount nullpointer

When I'm running my project in debug mode it does not give problems, but when I squeeze into normal android studio mode it crashes.
My Adapter
public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.ViewHolder> {
Context context;
Screen screen;
public InfoAdapter(Context context, Screen screen) {
this.context = context;
this.screen = screen;
}
#NonNull
#Override
public InfoAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.info_list, parent, false);
return new InfoAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull InfoAdapter.ViewHolder holder, int position) {
holder.name.setText(screen.getInfo().get(position).getName());
holder.data.setText(screen.getInfo().get(position).getData());
}
#Override
public int getItemCount() {
return screen.getInfo().size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, data;
public ViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
data = view.findViewById(R.id.data);
}
}
}
My Fragment
public class InvestmentFragment extends Fragment implements FoundView{
TextView titleInvestment, fundNameInvestment, whatIs, definition, riskTitle, infoTitle, fundMonth,
cdiMonth, fundYear, cdiYear, fundTwelveMonths, cdiTwelveMonths;
View view;
RecyclerView recyclerViewInfo;
RecyclerView recyclerViewDownInfo;
public Screen screen = new Screen();
InfoAdapter adapter;
DownInfoAdapter downInfoAdapter;
public List<DownInfo> downInfos = new ArrayList<>();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_investment, container, false);
onResume();
//Recycler info items
recyclerViewInfo = view.findViewById(R.id.recyclerviewInfo);
recyclerViewInfo.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerViewInfo.setNestedScrollingEnabled(false);
recyclerViewInfo.setLayoutManager(layoutManager);
adapter = new InfoAdapter(getContext(), screen);
recyclerViewInfo.setAdapter(adapter);
//RecyclerView DownInfo items
recyclerViewDownInfo = view.findViewById(R.id.recyclerviewDowninfo);
recyclerViewDownInfo.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerViewDownInfo.setNestedScrollingEnabled(false);
recyclerViewDownInfo.setLayoutManager(linearLayoutManager);
downInfoAdapter = new DownInfoAdapter(getContext(), screen);
recyclerViewDownInfo.setAdapter(downInfoAdapter);
TextView textInvestment = view.findViewById(R.id.text_investment);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "DINEngschriftStd.otf");
textInvestment.setTypeface(font);
titleInvestment = view.findViewById(R.id.title);
fundNameInvestment = view.findViewById(R.id.fundName);
whatIs = view.findViewById(R.id.whatIs);
definition = view.findViewById(R.id.definition);
riskTitle = view.findViewById(R.id.riskTitle);
infoTitle = view.findViewById(R.id.infoTitle);
fundMonth = view.findViewById(R.id.fundMonth);
cdiMonth = view.findViewById(R.id.cdiMonth);
fundYear = view.findViewById(R.id.fundYear);
cdiYear = view.findViewById(R.id.cdiYear);
fundTwelveMonths = view.findViewById(R.id.fundTwelveMonths);
cdiTwelveMonths = view.findViewById(R.id.cdiTwelveMonths);
return view;
}
#Override
public void onResume() {
super.onResume();
FoundPresenter foundPresenter = new FoundPresenter(this);
foundPresenter.getFounds();
}
#Override
public void founds(Screen screen) {
adapter = new InfoAdapter(getContext(), screen);
downInfoAdapter = new DownInfoAdapter(getContext(), screen);
recyclerViewInfo.setAdapter(adapter);
recyclerViewDownInfo.setAdapter(downInfoAdapter);
titleInvestment.setText(screen.getTitle());
fundNameInvestment.setText(screen.getFundName());
whatIs.setText(screen.getWhatIs());
definition.setText(screen.getDefinition());
riskTitle.setText(screen.getRiskTitle());
infoTitle.setText(screen.getInfoTitle());
fundMonth.setText(screen.getMoreInfo().getMonth().getFund()+"%");
cdiMonth.setText(screen.getMoreInfo().getMonth().getFund()+"%");
fundYear.setText(screen.getMoreInfo().getYearMoreInfo().getFund()+"%");
cdiYear.setText(screen.getMoreInfo().getYearMoreInfo().getcDI()+"%");
fundTwelveMonths.setText(screen.getMoreInfo().get_12months().getFund()+"%");
cdiTwelveMonths.setText(screen.getMoreInfo().get_12months().getcDI()+"%");
}
}
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.allan.santanderapp.adapter.InfoAdapter.getItemCount(InfoAdapter.java:41)
The exception message is very clear.
Maybe you are not populating your Screen object with its list of infos that's why infos is null and you are trying to invoke a method size() on that null object.
Please show when you are dealing with the screen object.

Recyclerview in fragment inside a viewpager

I wanted to use recyclerview inside a fragment of my 3 fragment viewpager. I wrote the code and it ran perfectly but I don't know why it is too slow.When I swap from one fragment to another it almost takes half minute.Don't know what mistakes I did here.
Main Activity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolBar);
tabLayout = (TabLayout)findViewById(R.id.tabs);
viewPager = (ViewPager)findViewById(R.id.viewPager);
setSupportActionBar(toolbar);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new OneFragment(), "One");
viewPagerAdapter.addFragment(new TwoFragment(), "Two");
viewPagerAdapter.addFragment(new ThreeFragment(), "Three");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
Here it is one of my fragment where I tried to implement recyclerview.
Fragment
public class TwoFragment extends Fragment {
public TwoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_two, container, false);
OurData ourData = new OurData();
ourData.pic.add(R.drawable.b);
ourData.title.add("Hello");
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter((listAdapter));
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}
}
public class ListAdapter extends RecyclerView.Adapter {
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ListViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((ListViewHolder) holder).bindView(position);
}
#Override
public int getItemCount() {
return OurData.title.size();
}
private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView text;
private ImageView image;
public ListViewHolder(View itemview) {
super(itemview);
text = (TextView) itemview.findViewById(R.id.textF);
image = (ImageView) itemview.findViewById(R.id.imageF);
itemview.setOnClickListener(this);
}
public void bindView(int position) {
text.setText(OurData.title.get(position));
image.setImageResource(OurData.pic.get(position));
}
public void onClick(View view) {
}
}
}
public class OurData {
public static ArrayList<String> title = new ArrayList<String>();
public static ArrayList<Integer> pic = new ArrayList<Integer>();
}
It tooks my whole day thinking what I did wrong. Please help me out!
set your adapter recyclerView.setAdapter((listAdapter));
insise onViewCreated instead of onCreateView
I think the problem is because of one of your Methods.
maybe 'OurData'
and also are you sure of this:
#Override
public int getItemCount() {
return OurData.title.size();
}
also try using Log in each code and see the part that takes time especially 'OurData'

Data not shown in RecyclerView

I am trying to make a list of items with a Recycler View in a simple way by making a method called Data() that's has a for loop to set the text view continuously but when I run the app it show's me a white screen without anything.
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
RecyclerAdapter mAdapter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView =(RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
private class RecyclerAdapter extends RecyclerView.Adapter<MyViewHolder{
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.Data();
}
#Override
public int getItemCount() {
return 100;
}
}
private class MyViewHolder extends RecyclerView.ViewHolder{
public TextView mmTextView ;
private Button mButton;
public MyViewHolder(View itemView) {
super(itemView);
mmTextView = (TextView) findViewById(R.id.list_text_view);
mButton = (Button) findViewById(R.id.list_button);
}
private void Data (){
for (int i =0 ;i<=100;i++){
mmTextView.setText(String.valueOf("Text : " + i));
}
}
}
}
You should infilate mmTextView from itemView not from Activity. It should be mmTextView = (TextView) itemView.findViewById(R.id.list_text_view);, also since it's a view of holder you can get it in onBindViewHolder() method using holder.mmTextView.setText(). Infilate button on onCreate() of Activity, also it's convenient to pass data to an adapter inside it's constructor and bind data to adapter on onBindViewHolder() method since it knows which views it possesses.
In onBindViewHolder method bind the data into the textView:
mmTextView.mmTextView.setText("SOME STRING VALUE");

Categories

Resources