My application gets all images URL from server and saves that to an ArrayList and displays these images in ViewPager. But it generates a IllegalStateException. Adapter given below:
public class FullScreenImageAdapter extends PagerAdapter {
private Context _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Context activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.item, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.cardImage);
Picasso.with(_activity).load(_imagePaths.get(position)).into(imgDisplay);
((ViewPager) container).addView(viewLayout, 0);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Adapter created as
FullScreenImageAdapter adapter=new FullScreenImageAdapter(FullScreenActivity.this,all_url);
viewPager.setAdapter(adapter);
And the log looks like below:
java.lang.IllegalStateException: The application's PagerAdapter
> changed the adapter's contents without calling
> PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1,
> found: 3 Pager id: com.wat.clickzy:id/view_pager Pager class: class
> android.support.v4.view.ViewPager Problematic adapter: class
> com.wat.clickzy.FullScreenImageAdapter
Please help me
You need to call notifysetdatachanged on the adapter that you're using, every time you're adding/removing something to that adapter.
Look here for even more clarity.
Related
i want know why getview() function is not call
maybe override is wrong?
i want make custom listlayout
what's wrong in listviewadapter?
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Word> items;
public ListViewAdapter(Context mContext, ArrayList<Word> items) {
this.mContext = mContext;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
}
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
// this is my Main activity code
private ArrayList<Word> items;
private ListViewAdapter listviewadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.submit_Button);
btn.setOnClickListener(view -> {
ListView word_listview = findViewById(R.id.Wordle_Word_List);
items = new ArrayList<Word>();
items.add(new Word('a'));
listviewadapter = new ListViewAdapter(getApplicationContext(), items);
word_listview.setAdapter(listviewadapter);
});
}
i dont know what is missing in my code
In ListviewAdapter I dont know why getview() function not call
I think your problem is the condition in its body, not the whole function. There is no need to check if it is null or not. you can simply inflate the view as mentioned in this article:
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
wordview1.setText(items.get(i).w1);
return view;
}
I have the following code in eclipse and i can't put the correct layout and imageview because cannot be resolved or are not a field
public ViewPagerAdapter(Context context, String[] gallery) {
this.context = context;
this.gallery = gallery;
}
#Override
public int getCount() {
return gallery.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
// Locate the TextViews in viewpager_item.xml
ImageView imgflag = (ImageView) itemView.findViewById(R.id.image);
Picasso.with(context).load(gallery[position]).into(imgflag);
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((View) object);
}
I tried cleanning the project importing R, and making rigth click in my project/"Android tools"/"Fix project properties".
I have this problem only when i create an adapter class
Try to use group of key Ctr + Shift + o to let eclipse auto import neccessary things for your project.
I have searched multiple websites, android developer sites and after no luck to my issue had to seek the community help. All help and suggestion will be highly appreciated.
ISSUE :
I have in total 12 images that I receive from the server (Parse in here) and I show each of them in a PagerAdapter. If all the values are not null the adapter code works fine, the twist is I allow null values to be stored in server. When I get the whole list back from server, I just want to have those views in adapter which contains not null.
Example : Suppose 5 null paths I get then my adapter shows in total 12 views (7 with images, rest as blank pages).
My Adapter Code:
public class ProfileImageAdapter extends PagerAdapter implements OnTouchListener{
private Context localContext;
private LayoutInflater inflater;
private List<DataModel> parseObjects = new ArrayList<DataModel>();
private List<Integer> res = new ArrayList<Integer>();
// Declare Variables
private ImageView viewPagerDisplayImage;
/**
*
* #param context : The context where to display
* #param parseObjects : The ParseObject to work with
*/
public ProfileImageAdapter(Context context, List<DataModel> parseObjects) {
this.localContext = context;
this.parseObjects = parseObjects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return parseObjects.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == ((LinearLayout) object) && object != null);
}
/**
* What is the item to show
*/
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) localContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.profile_image_item, container, false);
// Locate the Image View in viewpager_item.xml
viewPagerDisplayImage = (ImageView) itemView.findViewById(R.id.view_pager_display_image_view);
// Getting the image
try {
ParseFile imageFile = parseObjects.get(position).getImage();
if (imageFile != null) {
byte[] bitmapImageData = imageFile.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapImageData, 0, bitmapImageData.length);
viewPagerDisplayImage.setImageBitmap(bitmap);
viewPagerDisplayImage.setOnTouchListener(this);
// Add viewpager_item.xml to ViewPager
itemView.setTag("VALID");
((ViewPager) container).addView(itemView);
} else {
((ViewPager) container).addView(itemView);
itemView.setTag("INVALID");
destroyItem(container, position, itemView);
}
} catch (ParseException e) {
e.printStackTrace();
}
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
//((ViewPager) container).invalidate();
((ViewPager) container).removeView((View) object);
}
}
My PagerActivity
public class PageViewerProfileImages extends Activity {
private ViewPager viewPager;
private ProfileImageAdapter profileImageAdapter;
private UserModel userModel = new UserModel();
// Create the Page View for the Profile Images
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_pager);
List<DataModel> parseObjects = userModel.getDataModels();
viewPager = (ViewPager) findViewById(R.id.pagerProfile);
profileImageAdapter = new ProfileImageAdapter(this, parseObjects);
// Setting the adapter
viewPager.findViewWithTag("VALID");
viewPager.setAdapter(profileImageAdapter);
}
}
My references:
http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#instantiateItem(android.view.ViewGroup, int)
ViewPager PagerAdapter not updating the View
Create a condition where you parse your server response.
if(imageUrl!=null){
// write code to add image url in your list, which will you pass in your adapter.
}
After using it, it will pass in adapter only those value in adapter which have some value, then it will work.
You can place this condition in adapter when you pass your image-Url list and assign value in your adapter list.
then before assigning your value please check same condition then assign.
Im trying to add a listview inside a fragment witch is also inside a tabhost. the app is not crashing or anything, but the list view is not showing anything.
Here is my code:
Class for the tabPager
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentHome();
case 1:
return new FragmentBadges();
case 2:
return new FragmentBenefits();
}
return null;
}
Following is the fragment where I want the listview:
public class FragmentBadges extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_badges_list, null);
BadgesListAdapter adapter = new BadgesListAdapter(this.getActivity(), getCurrentBadges());
ListView badgeslist = (ListView) rootView.findViewById(R.id.badges_list);
badgeslist.setAdapter(adapter);
badgeslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//TODO do some
}
});
return rootView;
}
And following is the adapter:
public class BadgesListAdapter extends ArrayAdapter<String> {
private final Activity context;
private List<Badge> badges;
public BadgesListAdapter(Activity context, List<Badge> badges) {
super(context, R.layout.fragment_badges_listrow);
this.context = context;
this.badges = badges;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.fragment_badges_listrow, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(badges.get(position).getName());
imageView.setImageResource(badges.get(position).getDrawableValue());
return rowView;
}
while debugging I found out that the getView method inside the badgesListAdapter is never called.
I have this same code for another app but without the tabpager and there it all works perfectly...
Check whether getCurrentBadges() method not returns empty list. If so there is no data to show in ListView.
I was able to make it work by inhering ListFragment in FragmentBadges, following this tutorial https://www.youtube.com/watch?v=heR4zhj_wV0
I have a dynamic array of drawables and want to display them in a scrollable list. The thing I am having the most trouble with is the array adapter. I don't get any compile time errors with this code, but the runtime error I get is -
Process: com.example.michael.myandroidappactivity, PID: 12297
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
I don't want to use a textview though! Here's the main code-
public class cards extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_old_cards);
ListView list = (ListView)findViewById(R.id.showCardList);
cardPile tmp = cardPile.getInstance();
ArrayList<Integer> discardPile = tmp.getDiscardPile();
ArrayAdapter<Integer> imgAdapt = new ArrayAdapter<Integer>(this,R.layout.listview_layout,discardPile);
list.setAdapter(imgAdapt);
}
}
You have to create a new class that extends the BaseAdapter interface and modify the getView method to return the view you want to show in your ListView. For example:
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Integer> imagesIds;
public ImageAdapter(Context _context, ArrayList<Integer> _imageIds)
{
context = _context;
imageIds = _imageIds;
}
#Override
public int getCount()
{
return imgIds.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView view;
if( convertView != null ) // recycle call
{
view = (ImageView) convertView;
}
else
{
view = new ImageView(context);
image.setBackgroundResource(imageIds.get(position));
}
return view;
}
}
Then modify your listView adapter as:
list.setAdapter( new ImageAdapter( this, discardPile) );