I want to list the JSON data received with this adapter, however am getting an error that says that he is not managing to find the view, The error I get in the log is "java.lang.IllegalArgumentException: view must not be null"
public class categoriaAdapter extends ArrayAdapter {
private List<categoriaModel> categoriaModelList;
private int resource;
private LayoutInflater inflater;
public categoriaAdapter(Context context, int resource, List<categoriaModel> objects) {
super(context, resource, objects);
categoriaModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.imgCategoria = (ImageView)convertView.findViewById(R.id.ivIcon);
holder.catTitulo = (TextView)convertView.findViewById(R.id.tvMovie);
holder.catDesc = (TextView)convertView.findViewById(R.id.tvTagline);
holder.catID = (TextView)convertView.findViewById(R.id.tvYear);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);
ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
holder.catTitulo.setText(categoriaModelList.get(position).getNome());
holder.catDesc.setText(categoriaModelList.get(position).getDescricacao());
holder.catID.setText(categoriaModelList.get(position).getIdeCategoria());
return convertView;
}
class ViewHolder{
ImageView imgCategoria;
TextView catTitulo;
TextView catDesc;
TextView catID;
}
}
And my
Error logs
Please check your layout(xml) and see if R.id.ivIcon is an imageView or if it is present in the resource layout. Make sure imgCategoria is not null and is properly initialised before calling displayImage.
Also initialize ImageLoader configuration. i.e.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
Hope it helps you..
Check whether you included the right item layout. I had same issue turns out I was using item layout of different activity. And I had duplicated the adapter code.
Related
I have a ListView and a CustomAdapter. The elements are all successfully loaded into the list. Now I want to change the background color of a certain element of the list by clicking on an external button. But I do not know how to access a specific item in the list.
Here is the CustomAdapter class:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
}
You can do it like this inside your getView() method
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
}
});
If you have a button on your view then performs the listener on that button
If you want to get your selected item view from your parent activity then :
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
selectedposition = position ;
}
});
View view = listView.getAdapter().getView(selectedposition,null,listview);
Then change its background:
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
please define your color in your color.xml file
If you have more than one view then , create an ArrayList<View> and do some loop
create a custom listener interface in your activity and your
adapter will implement this.
public interface OnClickListenerFromActivity {
void onActivityButtonClick(int position);
}
on click in your button call your listener's method
mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));
implement this listener into your adapter
public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
public void onActivityButtonClick(int position) {
// get your item through position and
// set your color here
}
}
I have a viewPager with two types of layouts. When I am in first layout I cannot change second layout value. How can I reach another layout's component.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sorag);
slideViewPager = findViewById(R.id.slideViewPager);
sliderAdapter = new SliderAdapter(this, bilet , slideViewPager, );
slideViewPager.setAdapter(sliderAdapter);
slideViewPager.setCurrentItem(mCurrentSorag);
slideViewPager.setOffscreenPageLimit(11);
slideViewPager.addOnPageChangeListener(viewListener);
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mCurrentSorag = position;;
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
My adapter (PagerAdapter)
#Override
public int getCount() {
return 11;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert layoutInflater != null;
final View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
final View viewResult = layoutInflater.inflate(R.layout.slide_layout_Result, container, false);
if(position<10) {
textViewStatusResult = viewResult.findViewById(R.id.textViewStatusLast);
textViewResult = viewResult.findViewById(R.id.textViewResultLast);
textViewStatusResult.setText("Not changed textView");
button.setOnClickListener(new View.OnClickListener() {
#SuppressLint("RestrictedApi")
#Override
public void onClick(View v) {
// onButtonClicked(button, position, currentButton, view);
}
});
container.addView(view);
container.addView(viewResult);
return view;
}else{
textViewStatusResult = viewResult.findViewById(R.id.textViewStatusLast);
textViewResult = viewResult.findViewById(R.id.textViewResultLast);
textViewResult.setText("1 / 10");
}
container.addView(viewResult);
return viewResult;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((LinearLayout)object);
}
I create a two View in instantiateItem function. When application start it works when position < 10. But I want to change when I am in first position.
Pass in a callback object to your ViewPager's constructor and let your activity handle the callback. In the callback you can update the other layout as needed.
I am trying to fetch data from an php file. Which is then converted to JSON in VendorList.class. And want populate ListView using VendorListAdapter. But some of data is missing like it is in Screenshot below.
Here is the getView function code:
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.vendor_custom_list,null);
holder= new ViewHolder();
holder.textViewName = (TextView)view.findViewById(R.id.cameraManName);
holder.imageViewCamera=(ImageView)view.findViewById(R.id.cameraManImageView);
holder.ratingBarCamera = (RatingBar)view.findViewById(R.id.cameraManRatingBar);
holder.cameraManID = (TextView) view.findViewById(R.id.cameraManID);
view.setTag(holder);
return view;
}
else
{
holder=(ViewHolder)view.getTag();
}
holder.textViewName.setText(cameraManName.get(i));
holder.cameraManID.setText(cameramanID.get(i));
// myId = cm_id.get(i);
holder.imageViewCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context, "cm_id: " + myId, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,CustomProfile.class);
myId = holder.cameraManID.getText().toString();
v_name = holder.textViewName.getText().toString();
String imgUri = cameraManImages.get(i);
intent.putExtra("cm_id", myId);
intent.putExtra("v_name",v_name);
intent.putExtra("imageUri",imgUri);
context.startActivity(intent);
}
});
ImageLoader.getInstance().displayImage(cameraManImages.get(i), holder.imageViewCamera, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
// Toast.makeText(view.getContext(),imageUri+" started",Toast.LENGTH_LONG).show();
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//Toast.makeText(view.getContext(), String.format("%s%s", imageUri, failReason.getCause()),Toast.LENGTH_LONG).show();
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//Toast.makeText(view.getContext(),imageUri+" complete",Toast.LENGTH_LONG).show();
//123 holder.imageView.setImageBitmap(loadedImage);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
// Toast.makeText(view.getContext(),imageUri+" cancelled",Toast.LENGTH_LONG).show();
}
});
// holder.ratingBarCamera.setNumStars(cameraManRating.get(i));
holder.ratingBarCamera.setRating(cameraManRating.get(i));
// PicassoHelp.downloadImages(context,images.get(i),holder.imageView);
// holder.imageView.setImageBitmap(images.get(i)); */
return view;
}
In your getView
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.vendor_custom_list,null);
holder= new ViewHolder();
holder.textViewName = (TextView)view.findViewById(R.id.cameraManName);
holder.imageViewCamera=(ImageView)view.findViewById(R.id.cameraManImageView);
holder.ratingBarCamera = (RatingBar)view.findViewById(R.id.cameraManRatingBar);
holder.cameraManID = (TextView) view.findViewById(R.id.cameraManID);
view.setTag(holder);
return view;
}
Remove that return, you're returning before you actually set the values.
How can I remove the selected item on my gridView list?
Now I delete my last added element.
#Override
public void onClick(View view) {
remove(personItems);
notifyDataSetChanged();
}
i try do like this
public View getView(final int position, View convertView, ViewGroup parent) {
personItems = getItem(position);
...
public void onClick(View view) {
personItems.remove(position);
notifyDataSetChanged();
}
but in this way i can t delete element from my grid view
Full code of my adapter
public class CustomAdapter extends ArrayAdapter<PersonItems> {
PersonItems personItems;
static class ViewHolder {
TextView member_name;
ImageView profile_pic;
}
public CustomAdapter(Context context, ArrayList<PersonItems> users) {
super(context, R.layout.list_item, users);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
personItems = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.member_name = (TextView) convertView.findViewById(R.id.member_name);
viewHolder.profile_pic = (ImageView) convertView.findViewById(R.id.profile_pic);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.member_name.setText(personItems.name);
viewHolder.profile_pic.setImageResource(setImage());
Button buttonDelete = (Button) convertView.findViewById(R.id.buttonDelete);
buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
remove(personItems);
notifyDataSetChanged();
}
});
return convertView;
}
public int setImage() {
switch (personItems.getName()) {
case Names.AII:
return R.drawable.profile;
case Names.VdV:
return R.drawable.vdm;
}
return 0;
}
}
In your adapter's constructor receive the list view from your Activity:
MyCustomAdapter adapter = new MyCustomAdapter(context, mylist);
Your adapter's constructor should look like this:
public MyCustomAdapter (Context context, List<Object> objectsList){
this.objectsList = objectsList;
this.context = context;
}
Now in your getView() method:
Button buttonDelete = (Button) convertView.findViewById(R.id.buttonDelete);
buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
objectsList.remove(position);
notifyDataSetChanged();
}
});
Hope this helps.
get the postion of current grid view and perform the delete method
I am using Base Adapter for displaying image in grid view. The working fine when images are fixed IMAGE_URLS directly but am try with geting url from list and assign into IMAGE_URLS that shows non static variable cannot be referenced from a static context. I don't know how to solve this issue. please help to solve this issue
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
String description="";
ArrayList<String> img = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
img.clear();
Bundle bundle = this.getArguments();
description = bundle.getString("description");
String[] separated= description.split(",");
for(int i=0;i<separated.length;i++)
{
img.add(separated[i]);
}
ImageGalleryFragment.imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private static class ImageAdapter extends BaseAdapter {
String[] IMAGE_URLS = img.toArray(new String[img.size()]);
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
#Override
public int getCount() {
return IMAGE_URLS.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
Change the class from private static to "private class ImageAdapter".