Skip to content

Instantly share code, notes, and snippets.

@kyodgorbek
Last active November 3, 2018 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyodgorbek/3bcd877832f127fbf8b750b49c2b2a47 to your computer and use it in GitHub Desktop.
Save kyodgorbek/3bcd877832f127fbf8b750b49c2b2a47 to your computer and use it in GitHub Desktop.
public class IntroductionAdapter extends RecyclerView.Adapter<IntroductionAdapter.MyViewHolder> {
private List<Introduction> introductionList;
Context context; // changes
public IntroductionAdapter(Context context, List<Introduction> introductionList) { // changes
this.context = context; // changes(here you can see context)
this.introductionList = introductionList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.introduction_list, parent, false); // change
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
final Introduction introduction = introductionList.get(position);
if (introduction.getImage() != null) {
Glide.with(holder.imageView).load(introduction.getImage()).into(holder.imageView);
}
holder.introduction.setText(introduction.getIntroduction());
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return introductionList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView introduction, about;
public CircularImageView imageView;
public MyViewHolder(View view) {
super(view);
introduction = (TextView) view.findViewById(R.id.introduction);
about = (TextView) view.findViewById(R.id.about);
imageView = (CircularImageView) view.findViewById(R.id.circularImageView);
}
}
}
public class IntroductionItem extends AppCompatActivity {
public RealmList<Introduction> introductionList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
//TODO move this initialization to App extends Application
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
final Realm realm = Realm.getInstance(realmConfiguration);
KitabInterface kitabInterface = ApiClient.getApiService();
Call<KitabSawti> call = kitabInterface.getIntroduction();
call.enqueue(new Callback<KitabSawti>() {
@Override
public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
introductionList = response.body().getIntroduction();
recyclerView.setAdapter(new IntroductionAdapter(IntroductionItem.this, introductionList));
realm.beginTransaction();
Introduction introduction = realm.createObject(Introduction.class);
introduction.setImage(introductionList.get(0).getImage());
introduction.setIntroduction(introductionList.get(0).getIntroduction());
realm.commitTransaction();
}
@Override
public void onFailure(Call<KitabSawti> call, Throwable t) {
List<Introduction> list = realm.where(Introduction.class).findAll();
if (list != null) {
recyclerView.setAdapter(new IntroductionAdapter(IntroductionItem.this, list));
}
}
});
}
}
@kyodgorbek
Copy link
Author

My public gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment