ListView only updates after orientation change - android

I am attempting to enact the Parcelable pattern to save and restore my ListView data on orientation changes.
The problem is, the ListView will only update once I do an orientation change. It will not update otherwise. It used to work just fine, until I started implementing Parcelable.
All help is greatly appreciated!
My fragment class is copied below, but can also be accessed here on GitHub:
public class TrackActivityFragment extends Fragment {
private IconicAdapter trackResultListViewAdapter = null;
private String artistId = null;
private final String LOG_TAG = TrackActivityFragment.class.getSimpleName();
public TrackActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<TrackParcelable> trackParcelables = null;
View rootView = inflater.inflate(R.layout.fragment_track, container, false);
Intent intent = getActivity().getIntent();
if(intent != null && intent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
artistId = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
}
if(savedInstanceState == null || !savedInstanceState.containsKey("tracks_key")) {
trackParcelables = new ArrayList<TrackParcelable>();
performSearch(artistId);
}
else {
trackParcelables = savedInstanceState.getParcelableArrayList("tracks_key");
}
ListView listView = (ListView) rootView.findViewById(R.id.listViewOfTopTracks);
trackResultListViewAdapter = new IconicAdapter(trackParcelables,
getTrackNamesFromParcelables(trackParcelables));
listView.setAdapter(trackResultListViewAdapter);
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("tracks_key",
trackResultListViewAdapter.getTrackParcelables());
super.onSaveInstanceState(outState);
}
private ArrayList<String> getTrackNamesFromParcelables(ArrayList<TrackParcelable>
trackParcelables){
ArrayList<String> trackNames = new ArrayList<>();
for(TrackParcelable element : trackParcelables){
trackNames.add(element.name);
}
return trackNames;
}
private void performSearch(String artistId) {
SpotifyApi api = new SpotifyApi();
SpotifyService spotify = api.getService();
Map<String, Object> options = new HashMap<>();
options.put("country", "US");
spotify.getArtistTopTrack(artistId, options, new Callback<Tracks>() {
#Override
public void success(Tracks tracks, Response response) {
final ArrayList<TrackParcelable> trackParcelables =
new ArrayList<TrackParcelable>();
for (Track track : tracks.tracks) {
trackParcelables.add(new TrackParcelable(track.name,track.album.name,
track.album.images.get(0).url,track.preview_url));
}
trackResultListViewAdapter.swapItems(trackParcelables);
Log.d(LOG_TAG,trackParcelables.toString());
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if(trackParcelables.size()==0){
Toast.makeText(getActivity(),
getString(R.string.no_tracks_found_toast),
Toast.LENGTH_SHORT).show();
}
trackResultListViewAdapter.notifyDataSetChanged();
}
});
}
#Override
public void failure(RetrofitError error) {
}
});
}
//This ViewHolder Pattern is from Busy Android Coder's Guide page 274 of book version 6.7
class ViewHolder {
ImageView icon=null;
TextView trackName=null;
TextView trackAlbum=null;
ViewHolder(View row) {
this.icon = (ImageView)row.findViewById(R.id.imageViewAlbum);
this.trackName = (TextView)row.findViewById(R.id.textViewTrackTitle);
this.trackAlbum = (TextView)row.findViewById(R.id.textViewTrackAlbum);
}
}
//This IconicAdapter Pattern is from Busy Android Coder's Guide page 272 of book version 6.7
class IconicAdapter extends ArrayAdapter<String> {
private ArrayList<TrackParcelable> trackParcelables;
public IconicAdapter(ArrayList<TrackParcelable> trackParcelables,
ArrayList<String> trackNames) {
super(getActivity(), R.layout.list_item_top_tracks, R.id.textViewTrackTitle
, trackNames);
this.trackParcelables = trackParcelables;
}
public void swapItems(ArrayList<TrackParcelable> trackParcelables) {
this.trackParcelables = trackParcelables;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
ViewHolder holder = (ViewHolder)row.getTag();
if (holder==null) {
holder=new ViewHolder(row);
row.setTag(holder);
}
Picasso.with(getActivity()).load(trackParcelables.get(position).albumImageUrl)
.into(holder.icon);
TextView trackAlbumTextView = (TextView)row.findViewById(R.id.textViewTrackAlbum);
trackAlbumTextView.setText(trackParcelables.get(position).albumName);
return row;
}
public ArrayList<TrackParcelable> getTrackParcelables(){
return trackParcelables;
}
}

The issue is that IconicAdapter is only tracking changes in trackNames, not in ArrayList <TrackParcelable> trackParcelables.
To fix this, I added a a member variable in my IconicAdapter for the trackNames and set this in the constructor like so:
private ArrayList<TrackParcelable> trackParcelables;
private ArrayList <String> trackNames;
public IconicAdapter(ArrayList<TrackParcelable> trackParcelables,
ArrayList<String> trackNames) {
super(getActivity(), R.layout.list_item_top_tracks, R.id.textViewTrackTitle
, trackNames);
this.trackNames = trackNames;
this.trackParcelables = trackParcelables;
}
Then, I edited my swapItems to update my ListView data like so:
public void swapItems(ArrayList<TrackParcelable> trackParcelables) {
this.trackParcelables = trackParcelables;
trackNames.clear();
trackNames.addAll(getTrackNamesFromParcelables(trackParcelables));
}

Related

Loading data from sqlite database with AsyncTask and display inside recyclerview items

I have a Navigation menu with nav menu on it. When clicked on each nav menu, the specific fragment is opened.For example, when I click on Words nav menu, words item display with recyclerView items on it. I'm fetching data from offline and external SQLite database and display on recyclerView items. Now I want to fetch data in another thread, NOT in the main thread, because I want increase loading speed data and app performance. But I don't know how to do this. please help me with a code. I read the same subject on the internet, but I still now have my issue.
this is my AllWordsFragment
public class AllWordsFragment extends Fragment {
private List<WordsList> wordsLists = new ArrayList<>();
private Cursor cursor;
ProgressBar progressBar;
RecyclerView recyclerView;
AllWordsAdapter allWordsAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.all_words_fragment, container, false);
progressBar = view.findViewById(R.id.progressBar);
progressBar.setMax(600);
allWordsAdapter = new AllWordsAdapter(getActivity(), wordsLists);
allWordsAdapter.notifyDataSetChanged();
recyclerView = view.findViewById(R.id.recyclerViewAllWords);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(allWordsAdapter);
loadingWords();
return view;
}
private void loadingWords() {
WordDatabase wordDatabase = new WordDatabase(getActivity());
try {
wordDatabase.createDatabase();
wordDatabase.openDatabase();
} catch (SQLiteException e) {
e.printStackTrace();
}
try {
cursor = wordDatabase.QueryData("SELECT Word, Definition, Example, WordList, ImageWord FROM Words");
if (cursor != null && cursor.moveToFirst()) {
do {
WordsList wordList = new WordsList();
wordList.setWordTitle(cursor.getString(0));
wordList.setDefinition(cursor.getString(1));
wordList.setExample(cursor.getString(2));
wordList.setVocubList(cursor.getString(3));
wordList.setImageWord(cursor.getString(4));
wordsLists.add(wordList);
} while (cursor.moveToNext());
wordDatabase.close();
}
} catch (SQLiteException w) {
w.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
and this is my AllWordsAdapter
public class AllWordsAdapter extends RecyclerView.Adapter {
private int lastPosition = -1;
protected Context context;
private List<WordsList> wordsListList = new ArrayList<>();
public AllWordsAdapter(Context context, List<WordsList> wordsListList) {
this.context = context;
this.wordsListList = wordsListList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.all_words_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
WordsList wordsList = wordsListList.get(position);
holder.wordTitle.setText(wordsList.getWordTitle());
holder.definitionWord.setText(Html.fromHtml(wordsList.getDefinition()));
holder.exampleWord.setText(Html.fromHtml(wordsList.getExample()));
holder.labelWordList.setLabelText(wordsList.getVocubList());
//get image from assets with Glide.
String pathImage = wordsList.getImageWord();
String assetsPath = "file:///android_asset/";
Glide.with(context)
.asBitmap()
.load(Uri.parse(assetsPath + "" + pathImage))
.into(holder.wordImage);
Log.d("path", assetsPath + "" + pathImage);
Typeface headerFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
holder.wordTitle.setTypeface(headerFont);
Typeface customFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf");
holder.exampleWord.setTypeface(customFont);
holder.definitionWord.setTypeface(customFont);
//cal animation function
setAnimation(holder.itemView, position);
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, AllWordsDetails.class);
intent.putExtra("word", holder.wordTitle.getText().toString());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return wordsListList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView wordImage;
private LabelTextView labelWordList;
private TextView wordTitle, definitionWord, exampleWord;
private RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
wordTitle = itemView.findViewById(R.id.allWordTitle);
wordImage = itemView.findViewById(R.id.circleHeaderImage);
exampleWord = itemView.findViewById(R.id.exampleAllWord);
definitionWord = itemView.findViewById(R.id.definitionAllWord);
labelWordList = itemView.findViewById(R.id.labelWordList);
relativeLayout = itemView.findViewById(R.id.relativeAllWords);
}
}
private void setAnimation(View viewToAnimation, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition) {
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(new Random().nextInt(501));//to make duration random number between [0,501)
viewToAnimation.startAnimation(scaleAnimation);
lastPosition = position;
}
}
}
I know must be use AsyncTask and do this in background, but I don't know how do this ? Please help me with a code. Thanks .
create an AsyncTask class inside your class:
class WordLoaderTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
loadingWords();
}
protected void onPostExecute(Void param) {
allWordsAdapter.notifyDataSetChanged();
}
}//asyncClass
replace calling loadingWords() in onCreate() with this line:
new WordLoaderTask().execute();
if you (for some reason or a way of using app) start getting duplicates in your ListView, then add wordsLists.clear(); as first line inside the do{} in loadingWords() method
Try like this
public class AllWordsFragment extends Fragment {
private List<WordsList> wordsLists = new ArrayList<>();
private Cursor cursor;
ProgressBar progressBar;
RecyclerView recyclerView;
AllWordsAdapter allWordsAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.all_words_fragment, container, false);
progressBar = view.findViewById(R.id.progressBar);
progressBar.setMax(600);
allWordsAdapter = new AllWordsAdapter(getActivity(), wordsLists);
allWordsAdapter.notifyDataSetChanged();
recyclerView = view.findViewById(R.id.recyclerViewAllWords);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(allWordsAdapter);
loadingWords();
return view;
}
private static LoadWordsTask extends AsyncTask<Void, Void, List<WordsList>> {
private Context context;
private AllWordsAdapter adapter;
private List<WordsList> wordsLists;
public LoadWordsTask(Context context, AllWordsAdapter adapter, List<WordsList> wordsLists) {
this.context = context;
this.adapter = adapter;
this.wordsLists = wordsLists;
}
#Override
public List<WordsList> doInBackground() {
List<WordsList> data = new ArrayList<>();
WordDatabase wordDatabase = new WordDatabase(getActivity());
try {
wordDatabase.createDatabase();
wordDatabase.openDatabase();
} catch (SQLiteException e) {
e.printStackTrace();
}
try {
cursor = wordDatabase.QueryData("SELECT Word, Definition, Example, WordList, ImageWord FROM Words");
if (cursor != null && cursor.moveToFirst()) {
do {
WordsList wordList = new WordsList();
wordList.setWordTitle(cursor.getString(0));
wordList.setDefinition(cursor.getString(1));
wordList.setExample(cursor.getString(2));
wordList.setVocubList(cursor.getString(3));
wordList.setImageWord(cursor.getString(4));
data.add(wordList);
} while (cursor.moveToNext());
wordDatabase.close();
}
} catch (SQLiteException w) {
w.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return data;
}
#Override
public void onPostExecute(List<WordsList> data) {
this.wordsLists.addAll(data);
this.adapter.notifyDataSetChanged();
}
}
}

Super Method in BaseAdapter cannot be initialised in RealmBaseAdapter

I am using Realm Database to Save the Data From the JSON within the Mobile.I am using Custom Adapter rather than the RealmAdapter.I am able to save the Data and Retrieve the Data from the Realm Database but when i delete the item by position i got an error, that says the Object is no longer valid to operate on.
ToDoRealmAdapter
public class ToDoRealmAdapter extends RealmBaseAdapter<RealmDatabasePopularDestination> {
Context mContext;
RealmResults<RealmDatabasePopularDestination> clas_realm_bookmark = null;
String TAG = "HomeTab_adapter";
public ToDoRealmAdapter(#NonNull Context context, RealmResults<RealmDatabasePopularDestination> clas_realm_bookmark) {
super(context, clas_realm_bookmark);
this.context = mContext;
this.clas_realm_bookmark = clas_realm_bookmark;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder viewHolder;
if (convertView == null) {
// inflate the layout
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.bookmark_grid_list_item, parent, false);
// well set up the ViewHolder
// viewHolder = new ClassScheduleStudentAdapter.Holder();
viewHolder = new Holder();
// viewHolder.popular_destintion_id = (TextView) convertView.findViewById(R.id.student_profile_subject);
viewHolder.title = (TextView) convertView.findViewById(R.id.festivalName);
viewHolder.imageLogo = (ImageView) convertView.findViewById(R.id.event_festival_main_image);
viewHolder.location = (TextView) convertView.findViewById(R.id.eventAddress);
viewHolder.monthEvent = (TextView) convertView.findViewById(R.id.textDateBookmark);
viewHolder.textViewIcon = (ImageView) convertView.findViewById(R.id.imageLocationBookmark);
// Log.d(TAG, "## postion:" + position + " getTeacherName" + class_destination.get(position).getId());
convertView.setTag(viewHolder);
} else {
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
// viewHolder = (ClassScheduleStudentAdapter.Holder) convertView.getTag();
viewHolder = (Holder) convertView.getTag();
}
viewHolder.title.setText(clas_realm_bookmark.get(position).getTitle());
viewHolder.location.setText(clas_realm_bookmark.get(position).getLocation());
if (clas_realm_bookmark.get(position).getType().equals("popular_destination")) {
viewHolder.monthEvent.setVisibility(View.INVISIBLE);
viewHolder.textViewIcon.setImageResource(R.mipmap.fav_icon_popular);
} else {
viewHolder.monthEvent.setText(clas_realm_bookmark.get(position).getDateEvent());
viewHolder.textViewIcon.setImageResource(R.mipmap.events_festival_icon);
}
System.out.println("Display" + clas_realm_bookmark.get(position).getDateEvent());
Picasso picasso = Picasso.with(mContext);
// picasso.setIndicatorsEnabled(true);
picasso.load(clas_realm_bookmark.get(position).getImage()).memoryPolicy(MemoryPolicy.NO_STORE).networkPolicy(NetworkPolicy.OFFLINE).error(R.drawable.close).into(viewHolder.imageLogo, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again online if cache failed
Picasso.with(mContext)
.load(clas_realm_bookmark.get(position).getImage())
.error(R.drawable.close)
.into(viewHolder.imageLogo, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.v("Picasso", "Could not fetch image");
}
});
}
});
//Picasso.with(mContext).load(clas_realm_bookmark.get(position).getImage()).error(R.drawable.close).into(viewHolder.imageLogo);
return convertView;
}
class Holder {
TextView title;
ImageView imageLogo;
TextView location;
TextView monthEvent;
ImageView textViewIcon;
}
}
PopularDestinationGridDetail
public void savetoDatabase() {
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
//
count = (int) bgRealm.where(RealmDatabasePopularDestination.class).equalTo("Id", id).equalTo("Type", type).count();//mofidy Query here
if (count > 0) {
} else {
RealmDatabasePopularDestination realmDatabasePopularDestination = bgRealm.createObject(RealmDatabasePopularDestination.class);
realmDatabasePopularDestination.setId(id);
realmDatabasePopularDestination.setTitle(title);
realmDatabasePopularDestination.setLatitude(latitude);
realmDatabasePopularDestination.setLongitude(longitude);
realmDatabasePopularDestination.setImage(image);
realmDatabasePopularDestination.setType(type);
realmDatabasePopularDestination.setLocation(location);
realmDatabasePopularDestination.setDescription(description);
Log.v("Success", realmDatabasePopularDestination.getTitle());
}
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
if (count > 0) {
Toast.makeText(PopularDestinationGridDetail.this, "Already added", LENGTH_LONG).show();
} else {
Toast.makeText(PopularDestinationGridDetail.this, "Added to Favorites", LENGTH_LONG).show();
}
Log.v("Success", title);
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
Log.e("failed", error.getMessage());
}
});
}
Favourites
public class Favourites extends Fragment {
Realm realm;
GridView gridViewBookmark;
RealmResults<RealmDatabasePopularDestination> destination_bookmark_realm =null;
RealmResults<RealmDatabasePopularDestination> realmDatabasePopularDestinations;
FavouriteAdapter favouriteAdapter;
ToDoRealmAdapter toDoRealmAdapter;
RealmChangeListener<RealmResults<RealmDatabasePopularDestination>> realmChangeListener = new RealmChangeListener<RealmResults<RealmDatabasePopularDestination>>() {
#Override
public void onChange(RealmResults<RealmDatabasePopularDestination> databasePopularDestinations) {
toDoRealmAdapter.notifyDataSetChanged();
}
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Realm.init(getContext());
realm = Realm.getDefaultInstance();
View view = inflater.inflate(R.layout.bookmark_layout_gridview, container, false);
gridViewBookmark = (GridView) view.findViewById(R.id.gridviewBookmark);
getData();
getGridItemClick();
return view;
}
RealmResults<RealmDatabasePopularDestination> result;
public void getData() {
result = realm.where(RealmDatabasePopularDestination.class).findAll();
result.load();
// System.out.println("Result" + result.get(0).getTitle());
for (int i = 0; i < result.size(); i++) {
//if(result.get(i).getType().equals("popular_destination")) {
destination_bookmark_realm.add(result.get(i));
//}else{
// destination_bookmark_realm.add(result.get(i));
//}
}
// favouriteAdapter = new FavouriteAdapter(getContext(), destination_bookmark_realm);
// gridViewBookmark.setAdapter(favouriteAdapter);
toDoRealmAdapter = new ToDoRealmAdapter(getContext(), destination_bookmark_realm);
gridViewBookmark.setAdapter(toDoRealmAdapter);
destination_bookmark_realm.addChangeListener((RealmChangeListener<RealmResults<RealmDatabasePopularDestination>>) realmChangeListener);
System.out.println("Result is" + result);
}
String type;
public void getType(final int pos) {
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
//
RealmResults<RealmDatabasePopularDestination> results = bgRealm.where(RealmDatabasePopularDestination.class).findAll();//mofidy Query here
type = results.get(pos).getType();
System.out.println("LOg2" + type);
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
Log.e("failed", error.getMessage());
}
});
}
public void getGridItemClick() {
gridViewBookmark.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getType(position);
System.out.println("LOg" + type);
if (type == null) {
return;
}
if (type.equals("popular_destination")) {
System.out.println("LOg4" + type);
Intent Bookmark_Popular_Des_grid_intent = new Intent(getContext(), PopularDestinationGridDetail.class);
Bundle popular_destination_bundle = new Bundle();
popular_destination_bundle.putString("Popular_Destination_ID", String.valueOf(destination_bookmark_realm.get(position).getId()));
popular_destination_bundle.putString("Fav_Flag", "1");
Bookmark_Popular_Des_grid_intent.putExtras(popular_destination_bundle);
startActivity(Bookmark_Popular_Des_grid_intent);
} else {
System.out.println("LOg5" + type);
Intent Bookmark_Event_fes_grid_intent = new Intent(getContext(), EventAndFestivalGridDetail.class);
Bundle event_festival_bundle = new Bundle();
event_festival_bundle.putString("Event_Festival_ID", String.valueOf(destination_bookmark_realm.get(position).getId()));
event_festival_bundle.putString("Fav_Flag", "2");
Bookmark_Event_fes_grid_intent.putExtras(event_festival_bundle);
startActivity(Bookmark_Event_fes_grid_intent);
}
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
result.removeAllChangeListeners();
realm.close();
}
}
log
e: FATAL EXCEPTION: main
Process: org.municipality.mobile.patanheritage, PID: 31896
java.lang.NoSuchFieldError: No field handlerController of type Lio/realm/HandlerController; in class Lio/realm/BaseRealm; or its superclasses (declaration of 'io.realm.BaseRealm' appears in /data/app/org.municipality.mobile.patanheritage-2/base.apk)
at io.realm.RealmBaseAdapter.addListener(RealmBaseAdapter.java:67)
at io.realm.RealmBaseAdapter.<init>(RealmBaseAdapter.java:60)
at org.municipality.mobile.patanheritage.adapter.ToDoRealmAdapter.<init>(ToDoRealmAdapter.java:36)
at org.municipality.mobile.patanheritage.activity.PopularDestinationGridDetail.onCreate(PopularDestinationGridDetail.java:121)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
How can this issue be solved??
If you delete a given RealmObject on any thread, then when Realm becomes updated (and RealmChangeListeners are called), then that object will no longer be valid, and will no longer be available in any RealmResults<T> that previously had it.
However you for whatever reason copy the contents of the RealmResults to an ArrayList
ArrayList<T> arrayList = new ArrayList<>();
arrayList.addAll(realmResults); // <-- DON'T DO THIS AT HOME
which is super-duper horrible, because not only will the result set no longer update, but it will also be able to store and retain invalid objects.
Therefore,
public class FavouriteAdapter extends BaseAdapter {
Context mContext;
ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null; // <-- BAD
String TAG = "HomeTab_adapter";
public FavouriteAdapter(Context mContext,
ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark) { // <-- BAD
super();
this.mContext = mContext;
and should be either
public class FavouriteAdapter extends BaseAdapter {
Context mContext;
List<RealmDatabasePopularDestination> clas_realm_bookmark = null;
String TAG = "HomeTab_adapter";
public FavouriteAdapter(Context mContext, List<RealmDatabasePopularDestination> clas_realm_bookmark) {
super();
this.mContext = mContext;
with
RealmResults<...> results;
FavouriteAdapter adapter;
RealmChangeListener<RealmResults<...>> realmChangeListener = (element) -> {
adapter.notifyDataSetChanged();
};
public void onCreate(Bundle b) {
....
results = realm.where(RealmDatabasePopularDestination.class).findAll();
FavouriteAdapter adapter = new FavouriteAdapter(this, results);
results.addChangeListener(realmChangeListener);
}
public void onDestroy() {
....
results.removeAllChangeListeners();
realm.close();
}
OR
public class FavouriteAdapter extends RealmBaseAdapter<RealmDatabasePopularDestination> {
Context mContext;
//ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;
String TAG = "HomeTab_adapter";
public FavouriteAdapter(Context mContext, RealmResults<RealmDatabasePopularDestination> results) {
super(results);
...
EDIT: also, remove the commented code entirely
...executeTransactionAsync(realm -> {
if (count > 0) {
// RealmResults<RealmDatabasePopularDestination> result = bgRealm.where(RealmDatabasePopularDestination.class).equalTo("Id", pop_dest_id).equalTo("Type", "popular_destination").findAll();
// result.deleteAllFromRealm();
// results = realm.where(RealmDatabasePopularDestination.class).equalTo("Id", pop_dest_id).equalTo("Type", "popular_destination").findAll();
// adapter = new FavouriteAdapter(getApplicationContext(), results);
// results.addChangeListener(realmChangeListener);

Retrieve the Data From the RealmDatabase and Set it to GridView

I am able to Save the JSON Data to the Realm Database. I have used as the documentation of the Realm, but I am not able to set the data to the GridView. I am using Custom Adapter not the Realm Adapter. The Data are Logged but they are not Displayed to the GridView. How can this the Data be Retrieved and Displayed to the GridView?
PopularDestinationGridDetail this is where JSON data is parsed and saved to database
Realm.init(this);
realm = Realm.getDefaultInstance();
LinearAddTOFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
savetoDatabase();
}
});
public void savetoDatabase() {
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
RealmDatabasePopularDestination realmDatabasePopularDestination = bgRealm.createObject(RealmDatabasePopularDestination.class);
realmDatabasePopularDestination.setTitle(title);
realmDatabasePopularDestination.setTitle(latitude);
realmDatabasePopularDestination.setTitle(longitude);
realmDatabasePopularDestination.setImage(image);
// Toast.makeText(this, realmDatabasePopularDestination.setLatitude(realmDatabasePopularDestination1.getLatitude()))
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
Log.v("Success",title);
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
Log.e("failed", error.getMessage());
}
});
}
Favourites
public class Favourites extends Fragment {
Realm realm;
GridView gridViewBookmark;
ArrayList<RealmDatabasePopularDestination> destination_bookmark_realm = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Realm.init(getContext());
realm = Realm.getDefaultInstance();
RealmDatabasePopularDestination realmDatabasePopularDestination = new RealmDatabasePopularDestination();
View view = inflater.inflate(R.layout.bookmark_layout_gridview, container, false);
gridViewBookmark = (GridView) view.findViewById(R.id.gridviewBookmark);
destination_bookmark_realm.add(realmDatabasePopularDestination);
getData();
return view;
}
public void getData() {
FavouriteAdapter favouriteAdapter = new FavouriteAdapter(getContext(), destination_bookmark_realm);
gridViewBookmark.setAdapter(favouriteAdapter);
RealmResults<RealmDatabasePopularDestination> result = realm.where(RealmDatabasePopularDestination.class).equalTo("Title","niyash temple").findAll();
result.load();
System.out.println("Result is" + result);
// String output = "";
// for (RealmDatabasePopularDestination realmDatabasePopularDestination : result) {
//
//
// output += realmDatabasePopularDestination.toString();
//
// }
//
// System.out.println("output" + output);
// System.out.println("Total size=" + result.size());
}
}
getter and setter
public class RealmDatabasePopularDestination extends RealmObject {
String Title;
String Latitude;
String Longitude;
String image;
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
Longitude = longitude;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
FavouriteAdapter
public class FavouriteAdapter extends BaseAdapter {
Context mContext;
ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;
String TAG = "HomeTab_adapter";
public FavouriteAdapter(Context mContext, ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark) {
super();
this.mContext = mContext;
this.clas_realm_bookmark = clas_realm_bookmark;
}
#Override
public int getCount() {
return clas_realm_bookmark.size();
}
#Override
public Object getItem(int position) {
return clas_realm_bookmark.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final FavouriteAdapter.Holder viewHolder;
if (convertView == null) {
// inflate the layout
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.bookmark_grid_list_item, parent, false);
// well set up the ViewHolder
// viewHolder = new ClassScheduleStudentAdapter.Holder();
viewHolder = new FavouriteAdapter.Holder();
// viewHolder.popular_destintion_id = (TextView) convertView.findViewById(R.id.student_profile_subject);
viewHolder.title = (TextView) convertView.findViewById(R.id.festivalName);
viewHolder.imageLogo = (ImageView) convertView.findViewById(R.id.event_festival_main_image);
// Log.d(TAG, "## postion:" + position + " getTeacherName" + class_destination.get(position).getId());
convertView.setTag(viewHolder);
} else {
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
// viewHolder = (ClassScheduleStudentAdapter.Holder) convertView.getTag();
viewHolder = (Holder) convertView.getTag();
}
viewHolder.title.setText(clas_realm_bookmark.get(position).getTitle());
Picasso.with(mContext).load(clas_realm_bookmark.get(position).getImage()).error(R.drawable.close).into(viewHolder.imageLogo);
return convertView;
}
class Holder {
TextView title;
ImageView imageLogo;
}
}
I am not getting any error but they are not set on the ListView.This is the first time using realm, so don't get where I am doing wrong.
Instead of
public class FavouriteAdapter extends BaseAdapter {
Context mContext;
ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;
You should be using RealmBaseAdapter from realm-android-adapters as specified in the documentation.
you are setting adapter to list view before extracting data from database.
RealmResults<RealmDatabasePopularDestination> result = realm.where(RealmDatabasePopularDestination.class).equalTo("Title","niyash temple").findAll();
result.load();
FavouriteAdapter favouriteAdapter = new FavouriteAdapter(getContext(), destination_bookmark_realm);
gridViewBookmark.setAdapter(favouriteAdapter);
use above code and.
destination_bookmark_realm it should be load with the result you got from databse

how can i refresh fragment when back button is pressed?

How can I refresh the view of a fragment, when the back button is pressed?
I have tried this in the onResume method of the fragment but it doesn't work.
OK, here is the code
#SuppressWarnings("unused")
public class RestaurantMenuFragment extends Fragment {
private static final String TAG = "MenuItemsFragment";
private static final String CATEGORIES_KEY = "categories";
private static final String SELECTED_CATEGORY_ID_KEY = "category";
private static final String RESTAURANT_KEY = "restaurant123";
private static final String RESTAURANT_KCITY = "city";
private Spinner mCategoriesSpinner;
private ArrayAdapter<CategoriesResponse.Category> mCategoriesAdapter;
private ListView mListView;
private List<MenuItem> mItems;
private MenuItemsAdapter mItemsAdapter;
private EmptyLayout mEmptyLayout;
private Restaurant mRestaurant;
private int mCategoryId;
private List<CategoriesResponse.Category> mCategories;
private RestaurantActivity mActivity;
private MainApplication mApplication;
private CategoriesResponse mCategoriesResponse;
private ActionBar mActionBar;
private Gson mGson;
int categ;
private ObjectGetter mObjectGetter;
public static RestaurantMenuFragment newInstance(Restaurant restaurant) {
RestaurantMenuFragment fragment = new RestaurantMenuFragment();
Bundle args = new Bundle();
args.putString(RESTAURANT_KEY, new Gson().toJson(restaurant));
String dd=restaurant.city;
Log.i("dd12", dd);
fragment.setArguments(args);
return fragment;
}
public RestaurantMenuFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (RestaurantActivity) getActivity();
mApplication = (MainApplication) mActivity.getApplication();
mActionBar = mActivity.getSupportActionBar();
mGson = new Gson();
mObjectGetter = new ObjectGetter();
mCategories = new ArrayList<CategoriesResponse.Category>();
Log.i("mCategories",""+mCategories);
mItems = new ArrayList<MenuItem>();
Log.i("12345",""+mItems);
mItemsAdapter = new MenuItemsAdapter(getActivity(), mItems);
Bundle args = getArguments();
if (args != null) {
mRestaurant = mGson.fromJson(args.getString(RESTAURANT_KEY),
Restaurant.class);
}
if (savedInstanceState != null) {
mRestaurant = mGson.fromJson(
savedInstanceState.getString(RESTAURANT_KEY),
Restaurant.class);
mCategoryId = savedInstanceState.getInt(SELECTED_CATEGORY_ID_KEY);
mCategoriesResponse = mGson.fromJson(
savedInstanceState.getString(CATEGORIES_KEY),
CategoriesResponse.class);
}
assert mRestaurant != null;
updateCart();
}
public void updateCart() {
View view = mActionBar.getCustomView();
Button cartButton = (Button) view.findViewById(R.id.cartButton);
int nOfItems = 0;
if (mApplication.isCartCreated()) {
nOfItems = mApplication.getCart().getNOfAllItems();
}
cartButton.setText(String.format("%d", nOfItems));
if (nOfItems > 0) {
cartButton.setEnabled(true);
} else {
cartButton.setEnabled(false);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Gson gson = new Gson();
outState.putString(RESTAURANT_KEY, gson.toJson(mRestaurant));
outState.putInt(SELECTED_CATEGORY_ID_KEY, mCategoryId);
outState.putString(CATEGORIES_KEY, gson.toJson(mCategoriesResponse));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.spinner_list, container, false);
RestaurantActivity activity = (RestaurantActivity) getActivity();
String myDataFromActivity = activity.getMyData();
String myDataFromActivity1 = activity.getMyData1();
Log.i("myDataFromActivity",myDataFromActivity);
Log.i("myDataFromActivity1",myDataFromActivity1);
categ=Integer.parseInt(myDataFromActivity1);
mListView = (ListView) view.findViewById(R.id.list122334);
mListView.setAdapter(mItemsAdapter);
Log.d(TAG,"Querying items url "
+ Urls.menuItemsQuery(mRestaurant.id,categ));
mEmptyLayout = EmptyLayout.with(getActivity()).to(mListView)
.setEmptyMessage(R.string.categories_empty_message)
.showLoading();
loadItems();
return view;
}
private void loadItems() {
mEmptyLayout.showLoading();
mItems.clear();
mObjectGetter.getJsonObjectOrDialog(mActivity,
Urls.menuItemsQuery(mRestaurant.id, categ),
ItemsResponse.class,
new ObjectGetter.OnFinishedListener<ItemsResponse>() {
#Override
public void onFinishedLoadingObject(
ItemsResponse itemsResponse) {
mEmptyLayout.showEmpty();
if (itemsResponse != null
&& itemsResponse.items != null) {
mItems.addAll(itemsResponse.items);
}
mItemsAdapter.notifyDataSetChanged();
}
});
}
private class MenuItemsAdapter extends ArrayAdapter<MenuItem> {
private static final String TAG = "MenuItemsAdapter";
public MenuItemsAdapter(Context context, List<MenuItem> menuItems) {
super(context, 0, menuItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
LayoutInflater inflater;
if (convertView == null) {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
try
{
viewHolder.name.setText(menuItem.name);
viewHolder.description.setText(menuItem.description);
viewHolder.price.setText(String.valueOf(menuItem.price));
}catch(NullPointerException e){
e.printStackTrace();
}
viewHolder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mApplication.createNewCartIfPossibleAndAskIfNot(
getActivity(), mRestaurant,
new MainApplication.OnCreateCartListener() {
#Override
public void onCreateCart(Cart cart) {
cart.addOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}
});
viewHolder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mApplication.isCartCreated()) {
return;
}
mApplication.getCart().removeOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
return view;
}
private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {
if (!mApplication.isCartCreated()) {
return;
}
int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
if (quantity > 0) {
viewHolder.selectedView.setVisibility(View.VISIBLE);
} else {
viewHolder.selectedView.setVisibility(View.GONE);
}
viewHolder.quantity.setText(String.valueOf(quantity));
viewHolder.total.setText(String.valueOf(quantity
* menuItem.price));
}
class ViewHolder {
TextView name;
TextView description;
TextView price;
Button add;
View selectedView;
Button remove;
TextView total;
TextView quantity;
}
}
#Override
public void onResume() {
super.onResume();
updateCart();
mItems.clear();
if (mItemsAdapter != null) {
mItemsAdapter.notifyDataSetChanged();
}
}
#Override
public void onDestroy() {
if (mObjectGetter != null) {
mObjectGetter.stopRequests();
}
super.onDestroy();
}
}
Now, i want to update the listvieww data when the user pressed the back button. I set the new loadItems() method in the onResume() Method of the Fragment. This Method is called but the old listview data appears and new data also appears...
Back button should be handled from Activity.
You can override onBackPressed in Activity and call a function on corresponding fragment to reloadItems().
Here are your 3 options I could think of.
Get reference to Fragment and call function to reLoadItems and its better to define an interface for this communication which fragment implements.
Better solution than first one. Add a LocalBroadcast which Activity broadcasts and your fragment listens and updates data on receiving broadcast.
Example for this :
http://luboganev.github.io/blog/messaging-with-localbroadcastmanager/
Otto event bus where both activity and fragment classes are connected to the event bus and they activity publishes event and fragment subscribes to it. This is what I am using for something similar in my application. (But I have pretty frequent asynchronous events that come along. SO I am using this. 2nd option might be sufficient in your case).
Example for this :
http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html
As ramesh already mentioned, back button handling happens in your activity class that holds the fragments. Here is a simple example, how you can handle these back button events for your fragment.
Activity Code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean returnSuperKeyDown = true;
if(keyCode == KeyEvent.KEYCODE_BACK){
Fragment fragment = getYourCurrentFragment();
if (fragment instanceof YourFragment) {
returnSuperKeyDown = ((YourFragment) fragment).onFragmentKeyDown();
}
}
if (returnSuperKeyDown) {
return super.onKeyDown(keyCode, event);
} else {
return true;
}
}
YourFragment Method:
public boolean onFragmentKeyDown() {
updateYourFragment();
return false;
}
#Rithe, #sunder sharma
As per me there is simple to refresh the fragment when come back from other fragment,
We just have to override the onActivityCreated Method for refresh fragment.
Like as
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//your code which you want to refresh
loadItems();
}
You can also update/refresh the fragment using onStart() method.
public void onStart(){
super.onStart();
//update your fragment
}
This worked fine for me.
call your loadItem() method onHiddenChanged(boolean hidden)method.onHiddenChanged is overrided method

Listview in Fragment is causing Memory Leak

I have a FragmentActivity with a FragmentMediaOverview containing a list of MediaItemViews (each with a imageview and some text) and a click on one of the items opening a detail-Fragment.
Now when I go back (via back button) and forth (click on listitem) several times from list to detail fragment I eventually run into OOM-Errors. I use SoftReferences for the bitmaps in the listitems as well as in the detail fragment.
According to MAT there is an incresing number of MediaItemViews as well as FragmentMediaOverview instances, but I just cannot figure out why.
I read this Android: AlertDialog causes a memory leak , but couldn't solve it nulling out listeners.
Here is my code:
FragmentMediaOverview.java
(This is not a ListFragment because for a tablet-layout the MediaAdapter needs to connect to a gridview)
public class FragmentMediaOverview extends Fragment {
private static String TAG = FragmentMediaOverview.class.getSimpleName();
private MediaAdapter adapter;
private OnMediaSelectedListener selListener;
private ArrayList<BOObject> mediaItems;
private ViewGroup layoutContainer;
private AdapterView itemContainer; // list or gridview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
layoutContainer = (ViewGroup) inflater.inflate(R.layout.fragment_media_overview, null);
return layoutContainer;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
selListener = (OnMediaSelectedListener) activity;
}
#Override
public void onDestroy() {
super.onDestroy();
itemContainer.setOnItemClickListener(null);
selListener = null;
adapter = null;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initUi(layoutContainer);
displayMedia();
}
private void initUi(ViewGroup layoutContainer) {
itemContainer = (AdapterView) layoutContainer.findViewById(android.R.id.list);
itemContainer.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BOMedia mediaItem = ((BOMedia) mediaItems.get(position));
//the FragmentActivity is coordinating the FragmentTransactions
selListener.onMediaSelected(mediaItem);
}
});
}
private void displayMedia() {
Log.d(TAG, "Displaying List");
if (mediaItems == null) {
loadMedia();
return;
}
Log.d(TAG, "List: " + mediaItems.size() + ", adapter: " + itemContainer.getAdapter());
if (adapter == null) {
Log.d(TAG, "Create Adapter with " + mediaItems.size());
adapter = new MediaAdapter(getActivity(), mediaItems);
}
if (itemContainer.getAdapter() == null) {
itemContainer.setAdapter(adapter);
} else {
adapter.setItems(mediaItems);
adapter.notifyDataSetChanged();
}
}
private void loadMedia() {
FragmentHelper.showProgressSpinner(layoutContainer, android.R.id.list);
DbHelper.getInstance().getMedia(mediaType, new DbQueryFinishListener() {
#Override
public void onDbCallFinish(ArrayList<BOObject> objects) {
if (!getActivity().isFinishing()) {
mediaItems = objects;
Collections.sort(mediaItems, new Comparator<BOObject>() {
final Collator c = Collator.getInstance(Locale.GERMAN);
#Override
public int compare(BOObject s1, BOObject s2) {
if (s2 != null && ((BOMedia) s2).getTitle() != null && s1 != null
&& ((BOMedia) s1).getTitle() != null) {
return c.compare(((BOMedia) s1).getTitle(),((BOMedia) s2).getTitle());
} else {
return 0;
}
}
});
displayMedia();
FragmentHelper.hideProgressSpinner(layoutContainer, android.R.id.list);
}
}
#Override
public void onDbCallException(Exception exception) {
if (!getActivity().isFinishing()) {
FragmentHelper.hideProgressSpinner(layoutContainer, android.R.id.list);
}
}
});
}
}
MediaAdapter.java
public class MediaAdapter extends BaseAdapter {
private static final String TAG = MediaAdapter.class.getSimpleName();
private Context context;
private ArrayList<BOObject> mediaItems;
public MediaAdapter(Context c, ArrayList<BOObject> mediaItems) {
super();
context = c;
this.mediaItems = mediaItems;
}
#Override
public int getCount() {
return mediaItems.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) {
if (convertView == null) {
convertView = new MediaItemView(context);
}
((MediaItemView)convertView).initialize((BOMedia) mediaItems.get(position));
return convertView;
}
public void setItems(ArrayList<BOObject> mediaItems) {
this.mediaItems = mediaItems;
}
}
MediaItemView.java
public class MediaItemView extends LinearLayout {
private static final String TAG = MediaItemView.class.getSimpleName();
private BOMedia item;
private SoftReference<Bitmap> bm;
private ImageView iv;
private Context ctx;
public MediaItemView(Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.view_media_item, this);
this.ctx = context;
}
/** Init the view with a new BOMedia object
* #param mediaItem
*/
public void initialize(BOMedia mediaItem) {
this.item = mediaItem;
initUI();
}
private void initUI() {
TextView title = (TextView) findViewById(R.id.itemText);
iv = (ImageView) findViewById(R.id.itemImage);
title.setText(Html.fromHtml(item.getTitle()));
iv.setImageBitmap(null);
bm = null;
System.gc();
iv.invalidate();
if (item.getFilepathThumb() != null && !item.getFilepathThumb().equals("")) {
ExpansionPackManager.getInstance().getBitmapResource(item.getFilepathThumb(), false,
new BitmapReadListener() {
#Override
public void onFileRead(BitmapResponseMessage message) {
Log.d(TAG, "Bitmap read: " + message.getFilepath());
Bitmap image = message.getBitmap();
if (image != null && message.getFilepath().equals(item.getFilepathThumb())) {
bm = new SoftReference<Bitmap>(image);
iv.setImageBitmap(bm.get());
Log.d(TAG, "image set");
} else {
Log.d(TAG, "image too late: " + image);
}
}
#Override
public void onFileException(Throwable exception) {
Log.d(TAG, "image exception");
}
});
}
}
}
In MediaItemView the size of your bitmap must be too big. If the bitmap is 600x600 and you want to display a image with a size of 50x50 you can use Bitmap.createScaledBitmap. You should also use bitmap cache while loading your bitmap.
This is because the View for rach child in the ListView is recreated as you scroll through. This is very heavy on resources. To avoid this use a holder class in adapters getView() to hold and reuse the views. This is called an Efficient Adapter. For example see Efficient List Adapter in API demos. http://developer.android.com/tools/samples/index.html
You can also use:
android:hardwareAccelerated = true
Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline is designed to better support hardware acceleration. Hardware acceleration carries out all drawing operations that are performed on a View's canvas using the GPU.
For more info http://developer.android.com/guide/topics/graphics/hardware-accel.html

Categories

Resources