I'm new to android
I've this adapter class for listing contacts
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder>{
public ImageButton mCallButton, mMoreButton, mSmsButton, mEmailButton;
public TextView mContactName;
public List<Contact> mContactList;
public Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder implements OnClickListener{
public MyViewHolder(View view) {
super(view);
mContactName = (TextView) view.findViewById(R.id.tv_contact_name);
mCallButton = (ImageButton) view.findViewById(R.id.imgbtn_call);
mMoreButton = (ImageButton) view.findViewById(R.id.imgbtn_more);
mSmsButton = (ImageButton) view.findViewById(R.id.imgbtn_sms);
mEmailButton = (ImageButton) view.findViewById(R.id.imgbtn_email);
mContext = view.getContext();
mCallButton.setOnClickListener(this);
mMoreButton.setOnClickListener(this);
mSmsButton.setOnClickListener(this);
mEmailButton.setOnClickListener(this);
}
public void onClick(View v) {
int index = getAdapterPosition();
String number = mContactList.get(index).getContactNumber();
String email = mContactList.get(index).getContactEmail();
String name = mContactList.get(index).getContactName();
if(v.getId() == mCallButton.getId()) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+number));
mContext.startActivity(callIntent);
}
if(v.getId() == mMoreButton.getId()) {
Log.d("Index kya hai?", index + "");
mSmsButton.setVisibility(View.VISIBLE);
mEmailButton.setVisibility(View.VISIBLE);
}
if(v.getId() == mSmsButton.getId()) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", number);
mContext.startActivity(Intent.createChooser(smsIntent, "Send Sms To " + name + " (" + number + ")"));
}
if(v.getId() == mEmailButton.getId()) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
mContext.startActivity(Intent.createChooser(emailIntent, "Send Email to " + email));
}
}
}
public ContactAdapter(List<Contact> contactList) {
mContactList = contactList;
}
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_contact_layout, parent, false);
return new MyViewHolder(itemView);
}
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = mContactList.get(position);
mContactName.setText(contact.getContactName());
}
public int getItemCount() {
return mContactList.size();
}
}
now there is a image button mMoreButton
is has only one job of showing hidden image button mSmsButton
, mEmailButton
but whenever i click, the button do not show in correct index(?)
Let's say i clicked on index 5, but the mEmailButtton
and mSmsButton
will appear on random index say 10, I don't know what I'm doing wrong
Move your views ( TextView
and ImageButton
) to ViewHolder
class so their reference will not always replaced. OR you can instead set the button click listener under the onBindViewHolder
method, that's what I always do when creating custom recyclerview adapter.
ImageButton mSmsButton_last, mEmailButton_last;
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = mContactList.get(position);
holder.mContactName.setText(contact.getContactName());
holder.mCallButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+number));
mContext.startActivity(callIntent);
}
});
holder.mMoreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Index kya hai?", index + "");
holder.mSmsButton.setVisibility(View.VISIBLE);
holder.mEmailButton.setVisibility(View.VISIBLE);
if (mSmsButton_last != null && mEmailButton_last != null) {
mSmsButton_last.setVisibility(View.GONE);
mEmailButton_last.setVisibility(View.GONE);
}
mSmsButton_last = holder.mSmsButton;
mEmailButton_last = holder.mEmailButton;
}
});
holder.mSmsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", number);
mContext.startActivity(Intent.createChooser(smsIntent, "Send Sms To " + name + " (" + number + ")"));
}
});
holder.mEmailButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
mContext.startActivity(Intent.createChooser(emailIntent, "Send Email to " + email));
}
});
}
and your ViewHolder
will just look like this
public class MyViewHolder extends RecyclerView.ViewHolder{
public ImageButton mCallButton, mMoreButton, mSmsButton, mEmailButton;
public TextView mContactName;
public MyViewHolder(View view) {
super(view);
mContactName = (TextView) view.findViewById(R.id.tv_contact_name);
mCallButton = (ImageButton) view.findViewById(R.id.imgbtn_call);
mMoreButton = (ImageButton) view.findViewById(R.id.imgbtn_more);
mSmsButton = (ImageButton) view.findViewById(R.id.imgbtn_sms);
mEmailButton = (ImageButton) view.findViewById(R.id.imgbtn_email);
mContext = view.getContext();
}
}
Hope this works for you. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.