Android Studio recyclerview No adapter attached; skipping layout - android-studio

Hello everyone i have a problem with recyclerview
the app works in my emulator bluestacks but when I started testing on other devices the program gives such an error recyclerview No adapter attached; skipping layout
this fragment activity code:
public class AsosiyFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.form_asosiy, container, false);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
DBAdapter mDBAdapter = new DBAdapter(getContext());
Spinner spinner_viloyatlar = view.findViewById(R.id.spinner_viloyatlar);
RecyclerView recyclerView = view.findViewById(R.id.roza_vaqtlari);
recyclerView.setHasFixedSize(true);
Context context = view.getContext();
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<RamazonAdapter> ramazonAdapters = new ArrayList<>();
ramazonAdapters.clear();
String[] VILOYAT_NOMI_KIRIL = {
"Андижон","Бухоро",
"Жиззах","Қашқадарё",
"Қорақалпоғистон","Навоий",
"Наманган","Самарқанд",
"Сурхондарё","Сирдарё",
"Тошкент","Фарғона",
"Хоразм"
};
// Add viloyatlar to spinner
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i <= 12; i++){
list.add(VILOYAT_NOMI_KIRIL[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),R.layout.spinner_item,R.id.item,list);
spinner_viloyatlar.setAdapter(adapter);
// get all data from sqlite
Cursor res = mDBAdapter.getAllData();
final RamazonAdapter[] rAdapters = {null};
while (res.moveToNext()){
String data1 = res.getString(1);
String data2 = res.getString(2);
String data3 = res.getString(3);
String data4 = res.getString(4);
rAdapters[0] = new RamazonAdapter(data1,data2,data3,data4);
ramazonAdapters.add(rAdapters[0]);
Adapter adapter1 = new Adapter(getActivity(), ramazonAdapters);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter1);
}
}
}
this adapter:
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
Context context;
private List<RamazonAdapter> ramazonAdapterList;
public Adapter(Context context, List<RamazonAdapter> ramazonAdapterList){
this.context = context;
this.ramazonAdapterList = ramazonAdapterList;
}
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.ramazon_item,parent,false);
return new Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder holder, int position) {
RamazonAdapter ramazonAdapter = ramazonAdapterList.get(position);
holder.nameOfDay.setText(ramazonAdapter.getNameOfDay());
holder.dateOfRoza.setText(ramazonAdapter.getDate());
holder.iftorlikTime.setText(ramazonAdapter.getIftorlikTime());
holder.saharlikTime.setText(ramazonAdapter.getSaharlikTime());
}
#Override
public int getItemCount() {
return ramazonAdapterList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView nameOfDay;
TextView dateOfRoza;
TextView iftorlikTime;
TextView saharlikTime;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameOfDay = itemView.findViewById(R.id.nameOfDay);
dateOfRoza = itemView.findViewById(R.id.dateOfRoza);
iftorlikTime = itemView.findViewById(R.id.iftorlikTime);
saharlikTime = itemView.findViewById(R.id.saharlikTime);
}
}
}
I found several answers on this topic and tried them but I didn’t succeed help please

I think the error you are facing is due to the below line code
Adapter adapter1 = new Adapter(getActivity(), ramazonAdapters);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter1);
in the above block of code you are trying to notify an adapter before setting the adapter to recyclerview

Related

how to send data from popup window to a tabbed activity having fragments?

mainActivity has button. After button is clicked a popup message show having a button and a TextInputLayout.
data is sent to a tabbed acticity having 2 fragments and
the data is shown in textview of first fragment. Error is the data wont show in the textview while running
adapter for 2 fragments--
public class adapter extends FragmentPagerAdapter {
public adapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
Fragment fragment;
if (position == 1) {
fragment = new SecondFragment();
} else {
fragment = new FirstFragment();
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
String title = null;
if (position == 0) {
title = "TextView";
} else if (position == 1) {
title = "Listview";
}
return title;
}
}
mainActivity(having popup window on clicking button)
myDialog = new Dialog(this);
}
//for for joining match
public void ShowPopup(View view) {
myDialog.setContentView(R.layout.popup);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
name = myDialog.findViewById(R.id.ig_name);
String igname = Objects.requireNonNull(name.getEditText()).getText().toString();
entry = myDialog.findViewById(R.id.entry);
entry.setOnClickListener(v -> {
Intent intent = new Intent(getApplicationContext(), secondActivity.class);
intent.putExtra("igname", igname);
startActivity(intent);
finish();
});
}
}
my firstFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
secondActivity activity = (secondActivity) getActivity();
TextView output = view.findViewById(R.id.textshow);
Bundle results = activity.getMyData();
String value1 = results.getString("value");
output.setText(value1);
return view;
}
secondActivity (intent is transfered from popup window)
public class secondActivity extends AppCompatActivity {
private String name;
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new adapter(getSupportFragmentManager()));
tabLayout = findViewById(R.id.tabs_layout);
tabLayout.setupWithViewPager(viewPager);
Intent intent = getIntent();
name = intent.getExtras().getString("igname");
}
public Bundle getMyData() {
Bundle bundle = new Bundle();
bundle.putString("value", name);
return bundle;
}
}
Get the "igname" String value in onClickListner & also dismiss the dialog before finishing the activity.
entry.setOnClickListener(v -> {
String igname = Objects.requireNonNull(name.getEditText()).getText().toString();
Intent intent = new Intent(getApplicationContext(), secondActivity.class);
intent.putExtra("igname", igname);
startActivity(intent);
finish();
});

How do I put the adapter into the viewpager?

This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.

Android Studio: How to make an EditText in a listview retain the changed value?

I am making a listView and in each item, there is a TextView identifying the field, and an EditText for the user to fill out the value. When I input the text into the EditText, the text just reverts back to what it was before. How do I make it so that it saves the new input? I read an article on StackOverflow of someone with a similar problem and they suggested using a TextOnChanged listener, so I added that as well. When I ran my app, the app just froze. I think I am doing it wrong so it would be great if someone helped me. Thanks.
Here is my code for my custom adapter:
public class DataListAdapter extends ArrayAdapter<DataObject> {
private Context mContext;
int mResource;
public DataListAdapter(Context context, int resource, List<DataObject> objects) {
super(context, resource, objects);
mResource = resource;
mContext = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
String field = getItem(position).getField();
String value = getItem(position).getValue();
String unit = getItem(position).getUnit();
DataObject dataObject = new DataObject(field, value, unit);
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
TextView tvField = (TextView) convertView.findViewById(R.id.Field);
final EditText tvValue = (EditText) convertView.findViewById(R.id.Value);
tvField.setText(field);
tvValue.setText(value);
//the code I attempted to use to save the changed text
tvValue.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String edit = s.toString();
tvValue.setText(edit);
}
#Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
}
Here is my code for the main activity for now if needed:
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayList<DataObject> data = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView) findViewById(R.id.list);
data.add(new DataObject("Vitamin A", "14", ""));
data.add(new DataObject("Vitamin B", "15", ""));
data.add(new DataObject("Vitamin C", "16", ""));
DataListAdapter dataAdapter = new DataListAdapter(this, R.layout.row, data);
list.setAdapter(dataAdapter);
}
}

Click on widget of a recyclerview item in Android Studio

I have a car that loads data from a remote database. Each item in the list has two buttons and I need to make different volley requests for each button. I know how to make the onclick work in the complete item but not how to do it for a widget inside the item.
Item screenshot
I hope you can help me, thanks in advance.
RecyclerViewAdapater:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
//vars
private List<Buttons> buttonsList;
private Context mContext;
public RecyclerViewAdapter(List<Buttons> buttonsList, Context mContext) {
this.buttonsList = buttonsList;
this.mContext = mContext;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Buttons buttons = buttonsList.get(position);
Glide.with(mContext)
.load(buttons.getButton_url())
.into(holder.image);
holder.name.setText(buttons.getButton_score());
holder.price.setText(buttons.getButton_price());
}
#Override
public int getItemCount() {
return buttonsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
TextView name, price;
Button buy_button, select_button;
public ViewHolder(View itemView) {
super(itemView);
image = (CircleImageView) itemView.findViewById(R.id.image_view);
name = (TextView) itemView.findViewById(R.id.name);
price = (TextView) itemView.findViewById(R.id.price);
buy_button = (Button) itemView.findViewById(R.id.buy_button_item);
select_button = (Button) itemView.findViewById(R.id.select_button_item);
}
}
}
MainActivity:
private void initRecyclerView(){
Log.d(TAG, "initRecyclerView: init recyclerview");
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerButtonsView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(buttonsList, this);
recyclerView.setAdapter(adapter);
}
I solved this with the answer of LordParsley in this post

How to implement onActivityResult within RecyclerView Fragment

I am trying to have a button within a Tab which is part of an activity for uploading images, when clicking the button inside one of the 2 Tabs presented in the activity ( Tab 1 pics, Tab 2 Vid ) the user could choose an image from the device.
My problem is implemneting the onClick Listener and onActivityResult which is not being used when placed after onClick, plus the #Override is being underlined as an error.
I guess i am not implementing the onClick the right way and not sure how to activate onActivityResult the proper way, i would appreciate any guidance;
MainActivty;
public class upload extends AppCompatActivity {
public String UserID;
private static final int REQUEST_SIGNUP = 0;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
Firebase.setAndroidContext(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new UploadPictures(), "Pictures");
adapter.addFragment(new UploadVideos(), "Videos");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And here is the Fragment Code;
public class UploadPictures extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView picture;
public EditText tagEditText;
public Button tagCurLoc, choosePic, uploadContent;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.pictue_upload, parent, false));
picture = (ImageView) itemView.findViewById(R.id.imgToUpload);
tagEditText = (EditText) itemView.findViewById(R.id.tagEditText);
tagCurLoc = (Button) itemView.findViewById(R.id.tagCurLoc);
choosePic = (Button) itemView.findViewById(R.id.choosePic);
choosePic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
onActivityResult(requestCode, resultCode, data);
Toast.makeText(getActivity().getApplicationContext(), "Canceled ", Toast.LENGTH_SHORT).show();
} else {
if (requestCode == RESULT_LOAD_IMAGE) {
Uri selectedImgPath = data.getData();
picture.setImageURI(selectedImgPath);
RealFilePath = Uri.parse(String.valueOf(selectedImgPath));
Toast.makeText(getActivity().getApplicationContext(), " " + RealFilePath, Toast.LENGTH_SHORT).show();
}
}
}
});
uploadContent = (Button) itemView.findViewById(R.id.uploadContent);
}
}
/**
* Adapter to display recycler view.
*/
public class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return LENGTH;
}
}
Not sure if the onClick listener for the button should be implemented in the Adapter or the View holder, and from there the issue of onActivityResult cant be used ?
Weel, solved after some more reading and few try outs.
The onClick listener should be implemented at the ViewHolder class followed by
#Override
onActivityResult.
within the onClickListener onActivityResult invoked by;
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and within onActivityResult;
onActivityResult(requestCode, resultCode, data);
Considering all of this taking place in an activity extending Fragment.

Resources