How to make interface in adapter class - java

I am making interface in adapter class but it is giving me error that interface expected here.How can I implement interface in adapter class.I made the adapter and main class but showing me error that interface is expected here.
public class Dashboard_Adapter extends ArrayAdapter<Dashboard_Model_File> implements RecyclerSwipeAdapter<Dashboard_Adapter.SimpleViewHolder> {
ArrayList<Dashboard_Model_File> dashboard_model_files_obj;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public Dashboard_Adapter(Context context, int resource, ArrayList<Dashboard_Model_File> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
dashboard_model_files_obj = objects;
}
Typeface font1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/cabin.regular.ttf");
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
SwipeLayout swipeLayout;
TextView textViewPos;
TextView textViewData;
Button buttonDelete;
public SimpleViewHolder(View itemView) {
super(itemView);
swipeLayout = (SwipeLayout) itemView.findViewById(com.daimajia.swipe.R.id.swipe);
textViewPos = (TextView) itemView.findViewById(com.daimajia.swipe.R.id.position);
textViewData = (TextView) itemView.findViewById(com.daimajia.swipe.R.id.text_data);
buttonDelete = (Button) itemView.findViewById(com.daimajia.swipe.R.id.delete);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(getClass().getSimpleName(), "onItemSelected: " + textViewData.getText().toString());
Toast.makeText(view.getContext(), "onItemSelected: " + textViewData.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
public Dashboard_Adapter(Dashboard dashboard, ArrayList<Dashboard_Model_File> dashboard_model_file_arraylist) {
super();
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.title_holder = (TextView) v.findViewById(R.id.title_view);
holder.desc_holder = (TextView) v.findViewById(R.id.desc_view);
holder.url_holder = (TextView) v.findViewById(R.id.url_view);
holder.bookmark_id_holder = (TextView) v.findViewById(R.id.bookmark_id);
holder.alternate_id_holder = (TextView) v.findViewById(R.id.alternate_id);
holder.bookmark_file_holder = (TextView) v.findViewById(R.id.bookmark_file);
holder.mode_holder = (TextView) v.findViewById(R.id.mode_id);
holder.swipeLayout = (SwipeLayout) v.findViewById(com.daimajia.swipe.R.id.swipe);
holder.textViewPos = (TextView) v.findViewById(com.daimajia.swipe.R.id.position);
holder.textViewData = (TextView) v.findViewById(com.daimajia.swipe.R.id.text_data);
holder.buttonDelete = (Button) v.findViewById(com.daimajia.swipe.R.id.delete);
holder.title_holder.setTypeface(font1); holder.title_holder.setTextSize(16.0f);
holder.desc_holder.setTypeface(font1); holder.desc_holder.setTextSize(14.0f);
holder.url_holder.setTypeface(font1); holder.url_holder.setTextSize(10.0f);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
new DownloadImageTask(holder.imageview).execute(dashboard_model_files_obj.get(position).getImage());
holder.title_holder.setText(dashboard_model_files_obj.get(position).getTitle());
holder.desc_holder.setText(dashboard_model_files_obj.get(position).getDescription());
holder.url_holder.setText(dashboard_model_files_obj.get(position).getUrl());
return v;
}
private Context mContext;
private ArrayList<String> mDataset;
#Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(com.daimajia.swipe.R.layout.recyclerview_item, parent, false);
return view;
}
#Override
public void onBindViewHolder(final SimpleViewHolder viewHolder,final int position) {
String item = mDataset.get(position);
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
viewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
#Override
public void onOpen(SwipeLayout layout) {
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(com.daimajia.swipe.R.id.trash));
}
});
viewHolder.swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
#Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
viewHolder.buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mItemManger.removeShownLayouts(viewHolder.swipeLayout);
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataset.size());
mItemManger.closeAllItems();
// Toast.makeText(view.getContext(), "Deleted " + viewHolder.textViewData.getText().toString() + "!", Toast.LENGTH_SHORT).show();
}
});
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(com.daimajia.swipe.R.layout.recyclerview_item, parent, false);
return new SimpleViewHolder(view);
}
#Override
public int getItemCount() {
return 0;
}
#Override
public int getSwipeLayoutResourceId(int position) {
return 0;
}
static class ViewHolder {
public ImageView imageview;
public TextView title_holder;
public TextView desc_holder;
public TextView url_holder;
public TextView bookmark_file_holder;
public TextView alternate_id_holder;
public TextView bookmark_id_holder;
public TextView mode_holder;
public TextView txtCategory;
SwipeLayout swipeLayout;
TextView textViewPos;
TextView textViewData;
Button buttonDelete;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
ImageView bmImage;
public DownloadImageTask(ImageView bmImage)
{
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls)
{
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}

You are not making an interface, you are trying to implement one. And as per the error it seems like this interface does not exist.

Related

Exoplayer not stopping after app closes in PagerAdapter

I have problem with Exoplayer to stopping after the app is closed down, or the fragment is changed from the BottomNavigationView, I get the data from my API that is cast to a Recyclerview/Adapter that handle the data, then the data with the images and video is gettting showed with a PagerAdapter.
I also tried in the fragment to set a onDestroy, but not working.
FeedAdapter.java
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private final List<FeedData> feedList;
private Activity context;
private View.OnClickListener onClickListener;
private SimpleExoPlayer videoPlayer;
REST apiService =
RestClient.getClient().create(REST.class);
public FeedAdapter(Activity context, List<FeedData> feedList) {
this.context = context;
this.feedList = feedList;
this.onClickListener = onClickListener;
}
public List<FeedData> getFeedData(){
return feedList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_feed_photo, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.mBinding.setFeed(feedList.get(position));
Boolean isLiked = holder.mBinding.getFeed().getIsLiked();
Boolean isSaved = holder.mBinding.getFeed().getIsSaved();
String username = holder.mBinding.getFeed().getUsername();
if (holder.mBinding.getFeed().getAvatar().length() > 0) {
Glide.with(context).load(holder.mBinding.getFeed().getAvatar())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(holder.mBinding.userAvatar);
}
holder.mBinding.username.setText(username);
holder.mBinding.description.setHashtagModeColor(ContextCompat.getColor(context, R.color.fitnessm8_green));
holder.mBinding.description.addAutoLinkMode(AutoLinkMode.MODE_HASHTAG, AutoLinkMode.MODE_MENTION);
holder.mBinding.description.setAutoLinkText(holder.mBinding.getFeed().getDescription());
holder.mBinding.likecount.setText(holder.mBinding.getFeed().getLikes().toString());
holder.mBinding.likecounttext.setText(context.getResources().getString(R.string.likes));
holder.mBinding.commentcount.setText(holder.mBinding.getFeed().getComments().size() + " " + context.getResources().getString(R.string.comments));
holder.mBinding.viewMoreComment.setText(context.getResources().getString(R.string.view) + " " + holder.mBinding.getFeed().getComments().size() + " " + context.getResources().getString(R.string.comments));
holder.mBinding.viewMoreComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
if (isLiked) {
holder.mBinding.like.setLiked(true);
} else {
holder.mBinding.like.setLiked(false);
}
if (isSaved) {
holder.mBinding.fav.setLiked(true);
} else {
holder.mBinding.fav.setLiked(false);
}
if (holder.mBinding.getFeed().getType().equals("image")) {
if (holder.mBinding.getFeed().getMediaSet().size() > 1) {
holder.mBinding.image.setVisibility(View.GONE);
MultiMediaAdapter pagerAdapter = new MultiMediaAdapter(context, holder.mBinding.getFeed().getMediaSet());
holder.mBinding.viewpagerId.setAdapter(pagerAdapter);
ScrollingPagerIndicator pagerIndicator = holder.mBinding.indicator;
pagerIndicator.attachToPager(holder.mBinding.viewpagerId);
} else {
holder.mBinding.viewpagerId.setVisibility(View.GONE);
Glide.with(context).load(holder.mBinding.getFeed().getMediaSet().get(0).getFile())
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_placeholder)
.into(holder.mBinding.image);
}
}
if (holder.mBinding.getFeed().getType().equals("video")) {
holder.mBinding.image.setVisibility(View.GONE);
MultiMediaAdapter pagerAdapter = new MultiMediaAdapter(context, holder.mBinding.getFeed().getMediaSet());
holder.mBinding.viewpagerId.setAdapter(pagerAdapter);
ScrollingPagerIndicator pagerIndicator = holder.mBinding.indicator;
pagerIndicator.attachToPager(holder.mBinding.viewpagerId);
}
holder.mBinding.clickUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, UserProfileActivity.class);
i.putExtra("profileUserID", holder.mBinding.getFeed().getUserId());
view.getContext().startActivity(i);
}
});
}
#Override
public int getItemCount() {
return feedList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ListItemFeedPhotoBinding mBinding;
public ViewHolder(View itemView) {
super(itemView);
mBinding = DataBindingUtil.bind(itemView);
}
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
MultiMediaAdapter.java
public class MultiMediaAdapter extends PagerAdapter {
private PlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private ImageView image;
Context context;
List<FeedMediaSet> Media_list;
public MultiMediaAdapter(Context context, List<FeedMediaSet> Media_list) {
this.context = context;
this.Media_list = Media_list;
}
#Override
public int getCount() {
return Media_list.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
FeedMediaSet mediaObj = Media_list.get(position);
if (mediaObj.getType().equals("image")) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item_media_photo,container,false);
image = view.findViewById(R.id.image);
Glide.with(context).load(mediaObj.getFile())
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_placeholder)
.into(image);
container.addView(view);
return view;
} else if (mediaObj.getType().equals("video")) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item_media_video,container,false);
simpleExoPlayerView = view.findViewById(R.id.playerView);
image = view.findViewById(R.id.image);
image.setVisibility(View.GONE);
initializePlayer(mediaObj.getFile(), mediaObj.getExtra());
container.addView(view);
return view;
}
return null;
}
private void initializePlayer(String uri, String extra) {
player = ExoPlayerFactory.newSimpleInstance(context);
try {
Uri mp4VideoExtraUri = Uri.parse(uri);
InputStream inputStream = context.getContentResolver().openInputStream(mp4VideoExtraUri);
Drawable yourDrawable = Drawable.createFromStream(inputStream, extra);
simpleExoPlayerView.setDefaultArtwork(yourDrawable);
} catch (FileNotFoundException e) {
Drawable yourDrawable = context.getResources().getDrawable(R.drawable.image_attach);
simpleExoPlayerView.setDefaultArtwork(yourDrawable);
}
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getResources().getString(R.string.app_name)));
MediaSource videoSource = new ProgressiveMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(uri));
player.setPlayWhenReady(false);
player.prepare(videoSource);
if (simpleExoPlayerView != null) simpleExoPlayerView.setPlayer(player);
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
releasePlayer();
}
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
simpleExoPlayerView.setPlayer(null);
}
}
}

Android - Unable to clear information after returning to activity

After clicking on the list and setting the information on EditText & ImageView to update the database information
When I return to the previous one and return to the same activity again, the information on EditText & ImageView will not be deleted and will still be displayed.
How can I fix this problem?
code java class Manage:
MyDB db = new MyDB(getApplicationContext());
List<InfoData> data = db.fetchmaindata();
db.close();
adapter = new MyListAdapter(getApplicationContext(), postdata(data));
BtnLoadAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
});
}
private View.OnClickListener onEditEmployeeSubmit = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (EdtNAME.length()>0 && EdtFAMILY.length()>0 && EdtDESCRIPTION.length()>0) {
try {
myDB.open();
myDB.editEmployee(
Integer.parseInt(EdtID.getText().toString()),
EdtNAME.getText().toString(),
EdtFAMILY.getText().toString(),
EdtDESCRIPTION.getText().toString(),
ImgViewOrgsToByte(ImgViewOrgs)
);
//refreshList();
Toast.makeText(getApplicationContext(), "Information was updated!", Toast.LENGTH_SHORT).show();
EdtID.setText("");
EdtNAME.setText("");
EdtFAMILY.setText("");
EdtDESCRIPTION.setText("");
ImgViewOrgs.setImageResource(R.mipmap.ic_launcher);
myDB.close();
}catch (Exception e){
e.printStackTrace();
myDB.close();
}
}else{
Toast.makeText(getApplicationContext(),"Fill in all the information!",Toast.LENGTH_SHORT).show();
}
//btnCancel.performClick();
}
};
public void info(){
findViewById(R.id.EdtID).setVisibility(View.VISIBLE);
byte[] infoimage = Manage.image;
if (infoimage != null && infoimage.length > 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(infoimage, 0, infoimage.length);
EdtID.setText(Integer.toString(id));
EdtNAME.setText(title);
EdtFAMILY.setText(family);
EdtDESCRIPTION.setText(description);
ImgViewOrgs.setImageBitmap(bitmap);
}else{
return;
}
}
private List<Info> postdata(List<InfoData> db) {
List<Info> data = new ArrayList<>();
for (int i = 0; i < db.size(); i++) {
Info cur = new Info();
cur.name = db.get(i).getName();
cur.family = db.get(i).getFamily();
cur.description = db.get(i).getDescription();
cur.image = db.get(i).getImage();
data.add(cur);
}
return data;
}
code adapter:
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
private Context context;
private List<Info> data;
private LayoutInflater inflater;
public MyListAdapter(Context context, List<Info> data) {
inflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row_main, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Info cur = data.get(position);
holder.textView.setText(cur.name);
holder.textViewf.setText(cur.family);
byte[] infoimage = cur.image;
Bitmap bitmap = BitmapFactory.decodeByteArray(infoimage, 0, infoimage.length);
holder.imageView.setImageBitmap(bitmap);
Typeface font = Typeface.createFromAsset(context.getAssets(), "BKoodkBd.ttf");
holder.textView.setTypeface(font);
holder.textViewf.setTypeface(font);
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView,textViewf,textViewi;
private ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.title_main);
textViewi = (TextView) itemView.findViewById(R.id.textViewi);
textViewf = (TextView) itemView.findViewById(R.id.title_mainf);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
Button btnEdit = (Button) itemView.findViewById(R.id.btn_edit);
final Button btnDelete = (Button) itemView.findViewById(R.id.btn_delete);
//imageView = (ImageView) itemView.findViewById(R.id.iii);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Info curr = new Info();
curr = data.get(getPosition());
Manage.id = curr.id;
Manage.title = curr.name;
Manage.family = curr.family;
Manage.description = curr.description;
Manage.image = curr.image;
Manage.id = getPosition() + 1;
Intent intent = new Intent(context, Manage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
}
}
Picture of the program

How to add OnItemClickListener on my CustomAdapter

I'm in a bind here, I wanted to integrate OnItemClickListener in my CustomAdapter, so when an item is click, it'll show some message or something. But I am not sure where to set it. I've look at bunch of tutorials and some post here but it only make me more confuse. So I need guidance on how and where would I need to set it to make it work. Here is my code:
MainActivity.java
ListView listView;
listView = (ListView) findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();
ReadRSS.java
#Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (feedItems != null) {
CustomAdapter customAdapter = new CustomAdapter(context,R.layout.activity_listview,feedItems);
listView.setAdapter(customAdapter);
}
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
}
Here is not the direct answer to your question, but my own code example so you can have a look at it and get the idea;
ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listword,R.id.wordView1, companies);
ListView listView = (ListView) findViewById(R.id.secondListView1);
listView.setAdapter(adapter);
// Item Click listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> ok, View v, int position, long id){
// Do your stuff here
}
});
You don't set it inside the adapter, but inside your activity where you define you ListView :
ListView listView = (ListView)findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code here
}
}
You can perform task on individual component of CustomAdapter class. Check below code.
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
tt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform your task
});
tt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform your task
});
}
return v;
}
}
Just like this,hope it helps:
public class CustomAdapter extends ArrayAdapter<FeedItem> implements OnClickListener{
private Context mContext;
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
v.setTag(p);
v.setOnClickListener(this);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
#Override
public void onClick(View v){
FeedItem p = (FeedItem) v.getTag();
// TODO,do something with p and mContext
}
}
But I think you should use listview.setOnItemClickListener(listener) to do this work.
Write this code on your activity below of your set adapter method.
Set Adapter Method
private void setAdapter() {
try {
DoctorAppointmentListAdapter doctorAppointmentListAdapter = new DoctorAppointmentListAdapter(getContext(), doctorAppointmentObjectArrayList, selectedItemClickListener, screen);
appointmentListView.setAdapter(doctorAppointmentListAdapter);
}catch (Exception e){}
}
Costume Click Methods
//ListAdapter Click Listener
DoctorAppointmentListAdapter.SelectItemClickListener selectedItemClickListener = new DoctorAppointmentListAdapter.SelectItemClickListener() {
#Override
public void itemClickedAtIndex(int index, String operation) {
if (operation.equalsIgnoreCase("00")) {
m_handler.removeCallbacks(m_handlerTask);
viewDetailsScreen(index);
} else if (operation.equalsIgnoreCase("11")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("33")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallCompletedService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("22")) {
m_handler.removeCallbacks(m_handlerTask);
//Phone Call Operation
if (doctorAppointmentObjectArrayList.get(index).getPatient_contact_no().length() > 0) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + doctorAppointmentObjectArrayList.get(index).getPatient_contact_no()));
startActivity(intent);
} else {
}
}
}
};
This is Adapter Class
public class DoctorAppointmentListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private Context context;
private int screen;
private ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList;
private SelectItemClickListener itemClickListener;
public DoctorAppointmentListAdapter(Context context, ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList, SelectItemClickListener listener, int screen) {
this.context = context;
this.screen = screen;
this.doctorAppointmentObjectArrayList = doctorAppointmentObjectArrayList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
if (itemClickListener != null) {
itemClickListener = null;
}
itemClickListener = listener;
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getCount() {
return doctorAppointmentObjectArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
public interface SelectItemClickListener {
public void itemClickedAtIndex(int index, String operation);
}
private class ViewHolder {
private TextView doctorNameTextView, numberTextView, datetimeTextView, enteryTextView;
private TextView viewDetailTextView, videoCallTextView, phoneCallTextView;
private CircleImageView profile_image;
private ImageView onlineStatusImageView;
private CircularProgressView progress_view;
}
ViewHolder holder = null;
#Override
public View getView(final int i, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_doctor_appointment_list, parent, false);
holder = new ViewHolder();
holder.doctorNameTextView = (TextView) convertView
.findViewById(R.id.doctorNameTextView);
holder.numberTextView = (TextView) convertView
.findViewById(R.id.numberTextView);
holder.datetimeTextView = (TextView) convertView
.findViewById(R.id.datetimeTextView);
holder.enteryTextView = (TextView) convertView
.findViewById(R.id.enteryTextView);
holder.viewDetailTextView = (TextView) convertView
.findViewById(R.id.viewDetailTextView);
holder.videoCallTextView = (TextView) convertView
.findViewById(R.id.videoCallTextView);
holder.phoneCallTextView = (TextView) convertView
.findViewById(R.id.phoneCallTextView);
holder.profile_image = (CircleImageView) convertView
.findViewById(R.id.profile_image);
holder.progress_view = (CircularProgressView) convertView.findViewById(R.id.progress_view);
holder.onlineStatusImageView = (ImageView) convertView.findViewById(R.id.onlineStatusImageView);
holder.onlineStatusImageView.setVisibility(View.INVISIBLE);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (screen == 1) {
holder.videoCallTextView.setVisibility(View.GONE);
}
try {
Picasso.with(context).load(doctorAppointmentObjectArrayList.get(i).getPatient_profile_pic()).into(holder.profile_image, new Callback() {
#Override
public void onSuccess() {
holder.progress_view.setVisibility(View.GONE);
}
#Override
public void onError() {
holder.progress_view.setVisibility(View.GONE);
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
}
});
} catch (Exception e) {
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
holder.progress_view.setVisibility(View.GONE);
e.printStackTrace();
}
holder.doctorNameTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_display_name());
holder.numberTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_contact_no());
if (doctorAppointmentObjectArrayList.get(i).getAppointment_time().equalsIgnoreCase("")) {
holder.numberTextView.setText("");
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date testDate = null;
try {
testDate = sdf.parse(doctorAppointmentObjectArrayList.get(i).getAppointment_time());
} catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm a");
String newFormat = formatter.format(testDate);
holder.numberTextView.setText("" + newFormat);
}
holder.datetimeTextView.setText(doctorAppointmentObjectArrayList.get(i).getAppointment_reason());
if (screen==0){
holder.enteryTextView.setText("Entry Time :" + doctorAppointmentObjectArrayList.get(i).getAppointment_entry_time());
}else {
holder.enteryTextView.setText("");
}
holder.viewDetailTextView.setId(i);
holder.viewDetailTextView.setOnClickListener(viewDetailSelectedListener);
holder.videoCallTextView.setId(i);
holder.videoCallTextView.setOnClickListener(videoCallSelectedListener);
holder.phoneCallTextView.setId(i);
holder.phoneCallTextView.setOnClickListener(phoneCallSelectedListener);
return convertView;
}
View.OnClickListener viewDetailSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "00");
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener videoCallSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
if (screen == 2) {
itemClickListener.itemClickedAtIndex(index, "33");
} else {
itemClickListener.itemClickedAtIndex(index, "11");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener phoneCallSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "22");
} catch (Exception e) {
e.printStackTrace();
}
}
};
}

Android: How to setTag with Adapter

I have following situation: I have PostBaseAdapter where I initialize my views and MainActivity where I want to set Checkbox with switch parameter by getTag() but I don't know how to setTag() for my checkbox and where.
public class PostBaseAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private ArrayList<PostValue> postValueArrayList;
public PostBaseAdapter(Context context, ArrayList<PostValue> postValueArrayList) {
this.layoutInflater = LayoutInflater.from(context);
this.postValueArrayList = postValueArrayList;
}
#Override
public int getCount() {
return postValueArrayList.size();
}
#Override
public PostValue getItem(int position) {
return postValueArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_post, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
PostValue postValue = getItem(position);
viewHolder.tvTitle.setText(postValue.getBrand());
viewHolder.tvPublishDate.setText(postValue.getDate());
viewHolder.cost.setText(postValue.getPrice());
viewHolder.descriptionOfItem.setText(postValue.getDescription());
viewHolder.deliveryTime.setText("Cрок доставки: "+postValue.getDeliveryTime()+" дней");
viewHolder.checkBox.setChecked(false);
return convertView;
}
private class ViewHolder {
Button order;
CheckBox checkBox;
TextView tvTitle, tvPublishDate, cost, deliveryTime, descriptionOfItem;
public ViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.tvTitle);
tvPublishDate = (TextView) item.findViewById(R.id.tvPublishDate);
cost = (TextView) item.findViewById(R.id.cost);
deliveryTime = (TextView) item.findViewById(R.id.delivery_time);
descriptionOfItem = (TextView) item.findViewById(R.id.description);
checkBox = (CheckBox) item.findViewById(R.id.checkbox);
order = (Button) item.findViewById(R.id.order);
}
}
}
and MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
order = (Button) findViewById(R.id.order);
checkbox = (CheckBox) findViewById(R.id.checkbox);
lvPcsPost = (ListView) findViewById(R.id.lvPcsPost);
lvPcsPost.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvPcsPost.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (postValueArrayList != null && postValueArrayList.size() > 0) {
}
}
});
new PostAsync().execute();
//Установка Статус Бара определенного цвета
Window window = this.getWindow();
window.setStatusBarColor(this.
getResources().getColor(R.color.colorPrimary));
Paper.init(this);
}
class PostAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
XMLHelper helper;
#Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "Загрузка", "Ждите, идет загрузка автозапчастей ...", true, false);
}
#Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get(getIntent().getStringExtra("level")); // Передаем Интент с артикулом из поиска
postValueArrayList = helper.getPostsList();
return null;
}
#Override
protected void onPostExecute(Void result) {
PostBaseAdapter postBaseAdapter = new PostBaseAdapter(MainActivity.this, postValueArrayList);
lvPcsPost.setAdapter(postBaseAdapter);
pd.dismiss();
}
}
public void checkBoxClick(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getTag().toString()) {
case "0":
if (checked) {
Toast.makeText(this, "ЩЕЛК:", Toast.LENGTH_LONG).show();
}
break;
}
}

Android. Java. RecyclerAdapter

I have two different card view in one layout. I need that The latest news is added to one card view, and the rest in another. For exapmle, on image
MyRecyclerAdapter code
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
//Download image using picasso library
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(customViewHolder.imageView);
//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();
FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
}
};
//Handle click event on both title and image click
customViewHolder.textView.setOnClickListener(clickListener);
customViewHolder.imageView.setOnClickListener(clickListener);
customViewHolder.textView.setTag(customViewHolder);
customViewHolder.imageView.setTag(customViewHolder);
}
#Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;
public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.title);
}
}
}
The approach I would use if I were in you, to accomplish what you posted in the picture is to override getItemViewType and have two different types of ViewHolder, once for the header, the one with the big image and the other adapter for the small rows. onBindViewHolder gets the type as parameter. In your case is int i.
I write a quickdemo like this:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparator(String item) {
mData.add(item);
mSeparatorSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + "type " + type);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.text_view);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position) + "");
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
or if you want to use header and footer, create headerView layout:
View header = (View) getLayoutInflater().inflate(R.layout.headerView,null);
listView.addHeaderView(header);

Categories