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.
Related
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.
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
Here I'm using Custom Adapter by implementing listAdapter to Support buttons in the list items. When i click the Delete button it deletes currect row. But i can't able to refresh this listView.
File Structure is MainActivity -> FragmentClass-> this Custom class for the listAdpater.
public class Category_ListView_Custom_Adapter extends BaseAdapter implements ListAdapter{
private Realm realm;
private ArrayList<String> list = new ArrayList<>();
private Context context;
public Category_ListView_Custom_Adapter(ArrayList<String> list, Context context){
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View view1 = view;
realm = Realm.getDefaultInstance();
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_listview_row, null);
}
TextView listItemText = (TextView) view.findViewById(R.id.item);
listItemText.setText(list.get(i));
ImageView edit = (ImageView) view.findViewById(R.id.image);
ImageView delete = (ImageView) view.findViewById(R.id.delete);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// list.remove(i);
/// Toast.makeText(view.getContext(), "hey "+list.get(i).toString(), Toast.LENGTH_SHORT).show();
// notifyDataSetChanged();
Intent intent = new Intent(context, AddCategory.class);
intent.putExtra("category","expense");
intent.putExtra("subcategory", list.get(i));
context.startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RealmResults<Category> categorylist = realm.where(Category.class).equalTo("Id",i+1).and().equalTo("Subcategory",list.get(i)).findAll();
for (Category category : categorylist) {
realm.beginTransaction();
categorylist.deleteFirstFromRealm();
realm.commitTransaction();
try {
context.notifyAll();
}catch (Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
// Expense.call_resume
}
}
});
return view;
}
}
Please Help me. Thank you.
You are .removing from a wrong view. Try to make root List<> static and do Class.list.get(i).remove or something like that.
Also, try to hide a view inside your get view.
I am trying to get photos from the server into the gridview, everything looks ok however no images are coming and no error message, below is the custom adapter java file. What is wrong with the codes?
public class PhotoAdapter extends ArrayAdapter<PhotosActivity.Photos>{
ArrayList<PhotosActivity.Photos> photoList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public PhotoAdapter(Context context, int resource, ArrayList<PhotosActivity.Photos> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
photoList = objects;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.image = (ImageView) v.findViewById(R.id.grid_item_image);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Picasso.with(v.getContext()).load(photoList.get(position).getImage()).into(holder.image);
return v;
}
static class ViewHolder {
public ImageView image;
}
}
Actually my codes are working fine. It was my mistake, I didn't set the linear layout correctly, that's why the gridview goes off the screen.
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.