Inflating a View inside DialogFragment's onCreateView() causes a memory leak - android

I have a memory leak in my program. I have a ListView, each of its items has a Button, when user presses it, I want my program to show a DialogFragment with some EditText fields and a Button which would perform some action with the data from these fields and from the item that started the dialog. So, every time I open and close that dialog, my app's memory consumption increases and after many reopenings it reaches ~150 MB of RAM and then the app freezes (no OOM error or crash, just a freeze and slow down of the whole OS showing just a couple of MB of free RAM).
After some testing, I have discovered that the leak is caused, for some reason, by this line View v = inflater.inflate(R.layout.fragment_flag_dialog, container, false); (I have marked this in the code with a comment). If I "comment" this line, the leak is gone, RAM consumption of my app isn't increasing anymore after reopening. So, I would like you to help me find the problem why this line (or maybe something else) is causing a memory leak, because my knowledge isn't enough to find a clear clarification and a solution of this problem. The View should be garbage collected after I close the dialog, but, for some reason, it isn't.
Below is my code and, by the way, I instantiate and use the BaseAdapter provided below with the following code PopupListAdapter pListAdapter = new PopupListAdapter(getActivity(), someArrayList, someString); listView.setAdapter(pListAdapter); from another Fragment which is inside my Activity and which holds the ListView. Please don't pay attention to the class name PopupListAdapter. I don't use it in a popup window in this case, I use it in a normal Activity, I just designed it to be used in a popup window elsewhere and reused it here because it perfectly matches the situation.
public class PopupListAdapter extends BaseAdapter {
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater;
private Context context;
private static final String KEY_NAME = "name";
private static final String KEY_DATE = "date";
private static final String KEY_COMMENT = "comment";
private static final String KEY_COMMENT_ID = "id";
private final DialogFragment dFrag;
public PopupListAdapter(Context context, ArrayList<HashMap<String, String>> dataArg, String number) {
data = dataArg;
inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
dFrag = FlagDialogFragment.newInstance("test", number);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null)
view = inflater.inflate(R.layout.popuplistitem, null);
TextView name = (TextView) view.findViewById(R.id.name);
TextView date = (TextView) view.findViewById(R.id.date);
TextView comment = (TextView) view.findViewById(R.id.comment);
HashMap<String, String> element = new HashMap<String, String>();
element = data.get(position);
name.setText(element.get(KEY_NAME));
date.setText(element.get(KEY_DATE));
comment.setText(element.get(KEY_COMMENT));
final Button flagButton = (Button) view.findViewById(R.id.flag_button);
flagButton.setTag(element.get(KEY_COMMENT_ID));
flagButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(Utils.TAG, "Comment's ID is " + flagButton.getTag());
dFrag.show(((ActionBarActivity) context).getSupportFragmentManager(), "flag_dialog");
}
});
return view;
}
public static class FlagDialogFragment extends DialogFragment {
private String commentId;
private String number;
public static FlagDialogFragment newInstance(String listItemTag, String number) {
FlagDialogFragment f = new FlagDialogFragment();
Bundle args = new Bundle();
args.putString("comment_id", listItemTag);
args.putString("number", number);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
commentId = getArguments().getString("comment_id");
number = getArguments().getString("number");
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Log.i(Utils.TAG, "FlagDialogFragment's onCreateView() is called");
View v = inflater.inflate(R.layout.fragment_flag_dialog, container, false);// Something in this line is causing a memory leak!!!
TextView flagNumber = (TextView) v.findViewById(R.id.flag_number);
flagNumber.setText(number);
Button flagButton = (Button) v.findViewById(R.id.flag_button);
flagButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(Utils.TAG, "Comment id is " + commentId);
}
});
return v;
}
}
}

It's been a while since I saw this issue, but I keep notes on "gotchas" and this was one of them that might take a long time to debug.
Putting the dialogFragment in it's own class or declaring it not static should fix the memory leak.
Sorry I couldnt' give links but try it out and let others know this solution helped or not.
EDIT:
public static class FlagDialogFragment extends DialogFragment {
private String commentId;
private String number;
private View testView;
and inside your onCreateView
testView = inflater.inflate(R.layout.fragment_flag_dialog, container, false);
then on onDestroyView()
testView = null;
let me know how this works?

Related

Object is not updated in a fragment

I am doing an android project, trying to work with fragment
one of my fragments has a spinner which I need a separate listener for it
when I set the listener I send an object to the constructor as I need this object I don't know why I get null all the time when I call it in the fragment while it's updated in the listener
maybe I have to use another way to pass objects, but I didnt found any
I will be appreciated if you can help me to know why [chosenTrackable] is null in the fragment all the time ?
and if there is a better way to call object between fragments and listeners
here is my code :
public class AddTracking extends Fragment {
Spinner trackableSpinner;
Context context;
HashMap <Integer, MyTrackable> myTrackables ;
MyTrackable chosenTrackable;
private static final String TAG = "AddTracking";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = container.getContext();
View view = inflater.inflate(R.layout.fragment_add_tracking, container, false);
myTrackables= new Model(view.getContext()).getTrackables();
//initilizing
trackableSpinner = view.findViewById(R.id.trackable_spinner);
//setup trackable spinner
ArrayAdapter trackableSpinnerAdapter = new ArrayAdapter(this.getActivity(),
android.R.layout.simple_spinner_item, new ArrayList<>(myTrackables.values()));
trackableSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
trackableSpinner.setAdapter(trackableSpinnerAdapter);
trackableSpinner.setOnItemSelectedListener( new SpinnerTrackableItemSelectedListener(this.getActivity(),chosenTrackable));
chosenTrackable = myTrackables.get(trackableSpinner.getSelectedItemPosition());
Log.d(TAG, "onCreateView: " + chosenTrackable);
return view;
}
}
listner:
public class SpinnerTrackableItemSelectedListener implements AdapterView.OnItemSelectedListener {
Context context ;
private static final String TAG = "SpinnerTrackableItemSel";
MyTrackable chosenTrackable;
public SpinnerTrackableItemSelectedListener(Context context, MyTrackable chosenTrackable) {
this.context = context;
this.chosenTrackable = chosenTrackable ;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
chosenTrackable = (MyTrackable) parent.getAdapter().getItem(position);
Log.d(TAG, "loadTimeSpinner: " + chosenTrackable);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

How to use insert data from ArrayList<HashMap<String, String>> into a list view in Android Studio

I am working on a project that gets queries from a database on a remote server. The SQL side of it gets information from two separate tables, combines them and sends a JSON encoded string back me. I then parse the string and create an ArrayList with a HashMap of String, String to hold the data. All of this works just fine. There are 108 entries and all the related variables show this.
My problem arises when I try and put the final ArrayList HashMap into a listView. I have created a custom layout that I know works as I use to populate it from a set of arrays through the use of a dataprovider class and a custom adapter.
I followed a tutorial to create a custom adapter for the ArrayList HashMap (http://techlovejump.com/android-multicolum-listview/) . It appears to work except it will only pull 10-12 of the Items from the ArrayList HashMap, and then repeat. It does have exactly 108 items in the list. I notice that the first item in the list that is just off of the screen changes to different items as I scroll though the list.
I have debugged the code in the custom adapter class and everything is working fine the position starts at 0 and goes up. I have mostly seen it go to 10, once or twice 12. At this point on the next time through the getView method the position is returned to 0 and it starts over.
I am just learning all this and probably doing something wrong just don't know what. Here is the code I have to call the the custom adapter class and the class itself.
Custom Adapter this called from the method that parses my JSON string and builds the original ArrayList HashMap
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by jokerfwb on 11/12/15.
*/
public class UsersmyListAdapter extends BaseAdapter {
private static final String TAG_NAME = "Name";
private static final String AGE ="UserAge";
private static final String CLASS = "Class";
private static final String LEVEL = "Level";
private static final String SKILL = "Skill";
private static final String ACTIVITY = "Activity";
private static final String TIME_STAMP = "TimeStamp";
private static final String TAG_IN_GROUP = "InGroup";
public ArrayList<HashMap<String, String>> myList;
Activity activity;
TextView name;
TextView classType;
TextView skill;
TextView activityType;
public UsersmyListAdapter(Activity activity, ArrayList<HashMap<String, String>> myList){
super();
this.activity = activity;
this.myList = myList;
}
#Override
public int getCount(){
return myList.size();
}
#Override
public Object getItem(int position){
return myList.get(position);
}
#Override
public long getItemId(int position){
return 0;
}
public View getView(int position ,View convertView, ViewGroup parent){
LayoutInflater inflater = activity.getLayoutInflater();
if(convertView == null){
convertView = inflater.inflate(R.layout.users_lfg_list_item,null);
name = (TextView) convertView.findViewById(R.id.name);
classType = (TextView) convertView.findViewById(R.id.classType);
skill = (TextView) convertView.findViewById(R.id.skill);
activityType = (TextView) convertView.findViewById(R.id.activityType);
}
HashMap<String, String> map = myList.get(position);
name.setText(map.get(TAG_NAME));
classType.setText(map.get(CLASS));
skill.setText(map.get(SKILL));
activityType.setText(map.get(ACTIVITY));
return convertView;
}
}
The call from my activity to the Custom Adapter this gets called at the end of the method that parses the JSON string pulled by my AsyncTask
UsersLFGListAdapter adapter = new UsersLFGListAdapter(this, UsersLFG);
listView.setAdapter(adapter);
Found a solution as well
The answer from Varzaru worked wonderfully. I also found another solution after much more googling. Just wanted to post it for anyone else that sees this. It is as follows
impleAdapter UserArrayList = new SimpleAdapter(UsersLFGListActivity.this, UsersLFG, R.layout.users_lfg_list_item, new String[] {TAG_DESTINY_USER_NAME, TAG_ACTIVE_CLASS, TAG_ACTIVE_CLASS_LIGHT, TAG_ACTIVITY_TYPE}, new int[]{R.id.name, R.id.Class, R.id.Light, R.id.activityType});
listView.setAdapter(UserArrayList);
Well without seeing your other class I can take a shot at this. First your Listview adapter is not good, try this one:
public class UsersmyListAdapter extends ArrayAdapter<HashMap<String, String>> {
private static final String TAG_NAME = "Name";
private static final String AGE ="UserAge";
private static final String CLASS = "Class";
private static final String LEVEL = "Level";
private static final String SKILL = "Skill";
private static final String ACTIVITY = "Activity";
private static final String TIME_STAMP = "TimeStamp";
private static final String TAG_IN_GROUP = "InGroup";
public ArrayList<HashMap<String, String>> myList;
Activity activity;
public UsersmyListAdapter(Activity activity, ArrayList<HashMap<String, String>> myList){
super(activity, R.layout.users_lfg_list_item, myList);
this.activity = activity;
this.myList = myList;
}
#Override
public int getCount(){
return myList.size();
}
#Override
public Object getItem(int position){
return myList.get(position);
}
#Override
public long getItemId(int position){
return 0;
}
public View getView(int position ,View convertView, ViewGroup parent){
HashMap<String, String> map = myList.get(position);
ItemViewHolder viewHolder;
if(convertView == null){
viewHolder = new ItemViewHolder();
convertView = activity..getLayoutInflater().inflate(R.layout.users_lfg_list_item, null, true);
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.classType = (TextView) convertView.findViewById(R.id.classType);
viewHolder.skill = (TextView) convertView.findViewById(R.id.skill);
viewHolder.activityType = (TextView) convertView.findViewById(R.id.activityType);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
viewHolder.name.setText(map.get(TAG_NAME));
viewHolder.classType.setText(map.get(CLASS));
viewHolder.skill.setText(map.get(SKILL));
viewHolder.activityType.setText(map.get(ACTIVITY));
return convertView;
}
public class ItemViewHolder {
TextView name;
TextView classType;
TextView skill;
TextView activityType;
}
}
Second you should never set the Listview adapter on your complete method of AsyncTask. You should set the adapter on create after you declare the listview and when the AsyncTask finishes then add the new items to the list and call:
adapter.notifyDataSetChanged();
Hope it helps with your issue!

How to Pass values from one Activity to another Activity by using Tap on List View Item

I am writing an App in which i am trying to allow user to edit a quantity of an Item already in Cart, by Tap on Item in List View..
I want to show Quantity entered by user in EditText, when do Tap on an Item in Cart
How can i show description of an Item, when do Tap on an Item in Cart
[like : still i am just getting description word, not getting description of an
Item which i am showing in Screen Number:2]
I am using below code in getView() method in CartAdapter.java:
public static final String KEY_TITLE = "title";
public static final String KEY_COST = "cost";
public static final String KEY_TOTAL = "total";
public static final String KEY_QTY = "qty";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_THUMB_URL = "imageUri";
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = myinflater.inflate(R.layout.listrow_cart_edit, null);
vi.setClickable(true);
vi.setFocusable(true);
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
HashMap<String, String> item = Session.item_data.get(position);
Intent intent= new Intent(myactivity, com.easy.shoppingcart.SingleItem.class);
intent.putExtra(KEY_TITLE, item.get(com.easy.shoppingcart.SingleItem.KEY_TITLE));
intent.putExtra(KEY_COST, item.get(com.easy.shoppingcart.SingleItem.KEY_COST));
intent.putExtra(KEY_TOTAL, item.get(com.easy.shoppingcart.SingleItem.KEY_TOTAL));
intent.putExtra(KEY_QTY, item.get(com.easy.shoppingcart.SingleItem.KEY_QTY));
intent.putExtra(KEY_DESCRIPTION, (com.easy.shoppingcart.SingleItem.KEY_DESCRIPTION));
intent.putExtra(KEY_THUMB_URL, com.easy.shoppingcart.SingleItem.KEY_THUMB_URL);
myactivity.startActivity(intent);
}
});
Note: To get Description of an Item, i also tried with below code, but not worked, i know here is very small change need to do, but really i don't know what is that change:
intent.putExtra(KEY_DESCRIPTION, item.get(com.easy.shoppingcart.SingleItem.KEY_DESCRIPTION));
Intent intent= new Intent(myactivity, com.easy.shoppingcart.SingleItem.class);
intent.putExtra("position", position);
startActivity(intent);
on the other side of activity(SingleItem) inside oncreate()
int position;
if(this.getIntent().getExtras() != null) {
Bundle bundle = this.getIntent().getExtras();
position = Integer.parseInt(bundle.getString("position"));
}
Use this position to get the values.
HashMap<String, String> item = Session.item_data.get(position);

Android: Sorting a list of objects by one of three string arrays

Okay. So I'm making a little eBook store app. I have a string-array in strings.xml containing a list of books. Each array item contains four pieces of data, including the path to its cover image, the title, author, and genres. In the code below, I create a View object for each book in the array and print them out on-screen. I'm currently using a Tabs+Swipe layout, and there are three tabs: Title, Author, and Genre. What I want to do is sort the array of Views by, you know, either Title, Author, or Genre, depending on which tab is selected. Anyone know of any way that I can do this?
public static class SectionFragment extends Fragment {
private static ScrollView outerLayout;
private static LinearLayout innerLayout;
private static LayoutParams params;
private static ArrayList <String []> bookData = new ArrayList <String []> ();
private static String [] bookList;
public SectionFragment () { }
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
params = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
outerLayout = new ScrollView (context);
outerLayout.setLayoutParams(params);
innerLayout = new LinearLayout (context);
innerLayout.setLayoutParams(params);
innerLayout.setOrientation(LinearLayout.VERTICAL);
bookList = res.getStringArray(R.array.bookList);
for (int i = 0; i < bookList.length; i++) {
View bookView = new View (context);
bookView = inflater.inflate(R.layout.grid_cell, null);
ImageView bookCover = (ImageView) bookView.findViewById(R.id.bookCover);
TextView bookTitle = (TextView) bookView.findViewById(R.id.bookTitle);
TextView bookAuthor = (TextView) bookView.findViewById(R.id.bookAuthor);
TextView bookGenre = (TextView) bookView.findViewById(R.id.bookGenre);
bookData.add(bookList[i].split("\\|"));
try {
bookCover.setImageDrawable(Drawable.createFromStream(
context.getAssets().open(bookData.get(i)[0]), null));
} catch (IOException e) {
e.printStackTrace();
}
bookTitle.setText(bookData.get(i)[1]);
bookAuthor.setText("by " + bookData.get(i)[2]);
bookGenre.setText(bookData.get(i)[3]);
innerLayout.addView(bookView);
}
outerLayout.addView(innerLayout);
return outerLayout;
}
}
So I would advise you to create a Book class that encapsulates imageFilename, title, author and genre (this makes sorting MUCH easier).
public class Book{
private String imageName;
private String title;
private String author;
private String genre;
public Book(String image, String title1, String author1, String genre1)
{
imageName = image;
title = title1;
author = author1;
genre = genre1;
}
public String getImageName()
{
return this.imageName;
}
public String getTitle()
{
return this.title;
}
public String getAuthor()
{
return this.author;
}
public String getGenre()
{
return this.genre;
}
}
And then use the following code in your SectionFragment class. This should work as far as I can think. The sorting logic etc. is correct as far as I can think. The only problem that might arise is probably due to the arguments in the switch statement ( getArguments.getInt(SectionFragment.ARG_SECTION_NUMBER))
Oh! also, when you posted the code for getItem(i) you posted this:
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
#Override
public Fragment getItem(int i) {
Fragment fragment = new SectionFragment();
// Bundle args = new Bundle();
// args.putInt(SectionFragment.ARG_SECTION_NUMBER, i + 1);
// fragment.setArguments(args);
return fragment;
}
Make sure you uncomment the last three lines because these are important in the following switch statement in the code I've provided below (to figure out which fragment we are dealing with.)
public static class SectionFragment extends Fragment {
private static ScrollView outerLayout;
private static LinearLayout innerLayout;
private static LayoutParams params;
private static ArrayList <String []> bookData = new ArrayList <String []> ();
private static String [] bookList;
private static ArrayList<Book> bookList = new ArrayList<Book>();
public SectionFragment () { }
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
params = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
outerLayout = new ScrollView (context);
outerLayout.setLayoutParams(params);
innerLayout = new LinearLayout (context);
innerLayout.setLayoutParams(params);
innerLayout.setOrientation(LinearLayout.VERTICAL);
bookList = res.getStringArray(R.array.bookList);
for(int i = 0; i < bookList.length; i++)
{
String filename = bookList[i].split("\\|")[0];
String title = bookList[i].split("\\|")[1];
String author = bookList[i].split("\\|")[2];
String genre = bookList[i].split("\\|")[3];
Book book = new Book(filename, title, author, genre);
bookList.add(book);
}
switch(getArguments.getInt(SectionFragment.ARG_SECTION_NUMBER))
{
// I am assuming the first fragment is Title
case(1): Collections.sort(bookList, new Comparator<Book> {
public int compare(Book a, Book b)
{
return a.getTitle().compareTo(b.getTitle());
}
});
break;
case(2): Collections.sort(bookList, new Comparator<Book> {
public int compare(Book a, Book b)
{
return a.getAuthor().compareTo(b.getAuthor());
}
});
break;
case(3): Collections.sort(bookList, new Comparator<Book> {
public int compare(Book a, Book b)
{
return a.getGenre().compareTo(b.getGenre());
}
});
break;
}
for (int i = 0; i < bookList.length; i++) {
View bookView = new View (context);
bookView = inflater.inflate(R.layout.grid_cell, null);
ImageView bookCover = (ImageView) bookView.findViewById(R.id.bookCover);
TextView bookTitle = (TextView) bookView.findViewById(R.id.bookTitle);
TextView bookAuthor = (TextView) bookView.findViewById(R.id.bookAuthor);
TextView bookGenre = (TextView) bookView.findViewById(R.id.bookGenre);
try {
bookCover.setImageDrawable(Drawable.createFromStream(
context.getAssets().open(bookList.get(i).getImageName()), null));
} catch (IOException e) {
e.printStackTrace();
}
bookTitle.setText(bookList.get(i).getTitle());
bookAuthor.setText("by " + bookList.get(i).getAuthor());
bookGenre.setText(bookList.get(i).getGenre());
innerLayout.addView(bookView);
}
outerLayout.addView(innerLayout);
return outerLayout;
}
}
bookList contains the whole data so just create your sorting method based on criteria if author selected re-arrange your booklist array based on author name.
You will have to modify your String arrays programmatically in runtime. Get your full array with bookList = res.getStringArray(R.array.bookList);
Perform simple string operations and split your strings up to 3 arrays, one for title, Author and Genre and you can sort them alphabetically using Arrays.sort().
Of course, you will need to use a loop and some programming logic here so when you sort by Title, eg: bookTitleByTitle[0] must correspond to bookAuthorByTitle[0] and bookGenreByTitle[0]. And by the way, Using Arrays.sort() will destroy your original Array, so make copies of your array. (NOT CLONE!).
Repeat it for by author and genre.
This would be my approach.

Custom Spinner Crashing

I asked a question on here about a week or so ago about a custom spinner and got led to this guide. http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/
I followed it and I've tried adapting it to work with my code and pull the results from a database onto the spinner but it keeps crashing.
This is the code for the spinner.
public class EditTeam extends Activity {
private final List<SpinnerEntry> spinnerContent = new LinkedList<SpinnerEntry>();
private Spinner D1Spinner;
private final ETSpinnerAdapter D1Adapter = new ETSpinnerAdapter(spinnerContent, this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editteam);
myDbHelper = new DataBaseHelper(this);
myDbHelper.openDataBase();
fillSpinner();
}
private void fillSpinner() {
Cursor c = myDbHelper.FetchDrivers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"FirstName", "LastName"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
spinnerContent.add(new SpinnerEntry(1, null, "Test"));
//adapter.setDropDownViewResource( R.layout.spinner_entry_with_icon );
D1Spinner = (Spinner) findViewById(R.id.spr_Driver1);
D1Spinner.setAdapter((SpinnerAdapter) D1Adapter);
}
}
And I am using the two classes from that contacts example which are un-modified at the moment.
As you can see I'm trying to just manually add one item at the moment but it just crashes when you load it.
This seems to be the breaking point?
05-25 15:17:34.773: E/AndroidRuntime(241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.f1manager.android/com.f1manager.android.EditTeam}: java.lang.ClassCastException: com.f1manager.android.ETSpinnerAdapter
Any ideas would be great.
Thanks.
ETSpinnerAdapter Code (Unmodified from the original code in the example):
public class ETSpinnerAdapter {
private final List<SpinnerEntry> content;
private final Activity activity;
public ETSpinnerAdapter(List<SpinnerEntry> content, Activity activity) {
super();
this.content = content;
this.activity = activity;
}
public int getCount() {
return content.size();
}
public SpinnerEntry getItem(int position) {
return content.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = activity.getLayoutInflater();
final View spinnerEntry = inflater.inflate(
R.layout.spinner_entry_with_icon, null); // initialize the layout from xml
final TextView contactName = (TextView) spinnerEntry
.findViewById(R.id.spinnerEntryContactName);
final ImageView contactImage = (ImageView) spinnerEntry
.findViewById(R.id.spinnerEntryContactPhoto);
final SpinnerEntry currentEntry = content.get(position);
contactName.setText(currentEntry.getContactName());
//contactImage.setImageBitmap(currentEntry.getContactPhoto());
return spinnerEntry;
}
}
It would seem like your ETSpinnerAdapter is not a SpinnerAdapter as your are getting a class cast exceptin. Maybe you can post your code for the ETSpinnerAdapter?

Categories

Resources