Created
September 12, 2018 12:44
-
-
Save alfianyusufabdullah/bc9b5addcc0043d4b9a9326b0067caea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.papercom.imxd.Fragment; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.LoaderManager; | |
import android.support.v4.content.CursorLoader; | |
import android.support.v4.content.Loader; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import me.papercom.imxd.Adapter.FavoriteAdapter; | |
import me.papercom.imxd.Helper.BaseHelper; | |
import me.papercom.imxd.Model.FavoriteModel; | |
import me.papercom.imxd.R; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class FavoriteFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { | |
private RecyclerView rv; | |
private ArrayList<FavoriteModel> FavoriteModels; | |
private FavoriteAdapter FavoriteAdapter; | |
private static final int ID_LOADER = 100; | |
private String TAG = "Main"; | |
public FavoriteFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
getActivity().getSupportLoaderManager().restartLoader(ID_LOADER, null, this); | |
FavoriteModels = new ArrayList<>(); | |
initialize(FavoriteModels); | |
rv.setLayoutManager(new LinearLayoutManager(getActivity())); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_favorite, container, false); | |
LoadView(view); | |
return view; | |
} | |
private void initialize(ArrayList<FavoriteModel> FavoriteModels) { | |
Toast.makeText(getActivity(), "Loaded " + FavoriteModels.size(), Toast.LENGTH_LONG).show(); | |
FavoriteAdapter = new FavoriteAdapter(getActivity(), FavoriteModels); | |
rv.setAdapter(FavoriteAdapter); | |
} | |
private void LoadView(View view) { | |
rv = view.findViewById(R.id.fav); | |
} | |
@Override | |
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
switch (id) { | |
case ID_LOADER: | |
Uri filmUri = BaseHelper.CONTENT_URI; | |
Log.d(TAG, "onCreateLoader: " + filmUri.toString()); | |
return new CursorLoader(getActivity(), filmUri, null, null, null, null); | |
default: | |
throw new RuntimeException("Loader Not Implemented: " + id); | |
} | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | |
if (data.getCount() > 0) { | |
initialize(getMoviesFromCursor(data)); | |
} else { | |
Toast.makeText(getActivity(), "Not Found", Toast.LENGTH_LONG).show(); | |
} | |
} | |
private ArrayList<FavoriteModel> getMoviesFromCursor(Cursor cursor) { | |
ArrayList<FavoriteModel> items = new ArrayList<>(); | |
if (cursor != null) { | |
if (cursor.moveToFirst()) { | |
do { | |
FavoriteModel FavoriteModel = new FavoriteModel(cursor); | |
FavoriteModel.setId(cursor.getString(cursor.getColumnIndex("id"))); | |
FavoriteModel.setPosterPath(cursor.getString(cursor.getColumnIndex("image"))); | |
FavoriteModel.setTitle(cursor.getString(cursor.getColumnIndex("title"))); | |
FavoriteModel.setOverview(cursor.getString(cursor.getColumnIndex("overview"))); | |
FavoriteModel.setReleaseDate(cursor.getString(cursor.getColumnIndex("mov_release"))); | |
FavoriteModel.setVoteCount(cursor.getString(cursor.getColumnIndex("voteAverage"))); | |
FavoriteModel.setVoteAverage(cursor.getString(cursor.getColumnIndex("vote"))); | |
items.add(FavoriteModel); | |
} while (cursor.moveToNext()); | |
} | |
} | |
return items; | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> loader) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment