How do I put the adapter into the viewpager? - android-studio

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.

Related

Android Studio recyclerview No adapter attached; skipping layout

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

Passing context for a radio group in a fragment instead of activity?

I'm trying to use a RadioGroup widget inside a fragment instead of an activity. I'm getting an error on the following line of code:
RadioButton button = new RadioButton(this);
I'm not sure what to pass instead of 'this'
I believe the problem is either in the snippet above or here:
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
Full code:
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButtons();
return view;
}
private void createRadioButtons() {
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(this);
button.setText(question);
group.addView(button);
}
}
}
The constructor requires context so use getActivity() instead of this. You can also use getContext().
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButton(view);
return view;
}
private void createRadioButton(View view) {
RadioGroup group = (RadioGroup)view.findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(getActivity());
button.setText(question);
group.addView(button);
}
}
}

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.

App crashed with noClassDefFound error in ArrayAdpter Android

I have a custom listView which contains images and text views. But in getView method, application got crashed.
My code:
#Override
public View getView(final int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout, null);
}
ImageView removeWatch = (ImageView) view.findViewById(R.id.remove_watch);
removeWatch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("BEFOERE","on click in menuwatchadapter");
}
});
ArrayAdpter :
public class MenuListAdapter extends ArrayAdapter<MenuListModel> {
private Context context;
private int layout;
private List<MenuListModel> Model;
public interface ModelAction {
public void removedModel(MenuListModel removedModel);
}
private ModelAction ModelActions;
public MenuListAdapter(Context context, int resource,
List<MenuListModel> objects, ModelAction ModelActionListener) {
super(context, resource, objects);
this.context = context;
this.layout = resource;
this.Model = objects;
this.ModelActions = ModelActionListener;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout, null);
}
ImageView remove = (ImageView) view.findViewById(R.id.remove);
remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TEST","CLICKED");
}
});
return view;
}
}
At line removeWatch.setOnClickListener, application got crashed.
Can anyone tell me what can be the issue ?
NOTE : I am using Android-Studio.
LOGCAT :
-13 15:01:25.502 28351-28351/com.example.watch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.watch, PID: 28351
java.lang.NoClassDefFoundError: com.example.watch.adapter.MenuWatchListAdapter$1
at com.example.watch.adapter.MenuWatchListAdapter.getView(MenuWatchListAdapter.java:95)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillSpecific(ListView.java:1337)
at android.widget.ListView.layoutChildren(ListView.java:1620)
at android.widget.AbsListView.onLayout(AbsListView.java:2087)
at android.view.View.layout(View.java:14948)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14948)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
Change this line view = inflater.inflate(layout, null) to
view = inflater.inflate(R.layout.Your_LAYOUT, null) and give it a try.
I do not know why but i have unistalled whole android-studio and android sdk and re-install it and this is now working.

Resources