Recyclerview call contacts Android studio - android

I have an android app where I have used RecyclerView that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.
For now all it do is show the contacts, but when i click on it it calls, it does not do any action. I do not know how to get it to mark
I tried with onClick() and with button item.But Unsuccessful
My code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
Context mContext;
List<Contact> mData;
Dialog myDialog;
Button button;
public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v ;
v = LayoutInflater.from(mContext).inflate(R.layout.single_item_contact,parent,false);
final MyViewHolder vHolder = new MyViewHolder(v);
// Dialog ini
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(mContext,"Text Click item : "+String.valueOf(vHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show();
TextView dialog_name_tv = (TextView) myDialog.findViewById(R.id.dialog_name_id);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
myDialog.show();
}
});
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout item_contact;
private TextView tv_name;
private TextView tv_phone;
private ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
item_contact = (LinearLayout) itemView.findViewById(R.id.contact_item_id);
tv_name = (TextView) itemView.findViewById(R.id.name_contact);
tv_phone = (TextView) itemView.findViewById(R.id.phone_contact);
img = (ImageView) itemView.findViewById(R.id.img_contact);
}
}
}
AquĆ­ muestro mi XML de como tengo.
help please
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#232323"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="Contact Name"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/dialog_phone_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Phone Number"
android:textColor="#android:color/white"
android:textSize="23sp" />
<Button
android:id="#+id/dialog_btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_call_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Llamar"
android:textAlignment="textStart"
android:textSize="25dp" />
<Button
android:id="#+id/dialog_btn_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_message_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Mensaje"
android:textAlignment="textStart"
android:textSize="25dp" />
</LinearLayout>
<ImageView
android:id="#+id/dialog_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/ic_contacts"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>

You need to put setOnClickListener() into OnBindViewHolder().So far what i understand is you have Recycler View in which on item click you want to open a dialog box dialog_contact and this dialog box contains Contact information. After opening this dialog you want to call that person by clicking call button present in dialog.
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
holder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
TextView dialog_name_tv = (TextView)myDialog.findViewById(R.id.dialog_name_id);
Button dialog_btn_call= (Button )myDialog.findViewById(R.id.dialog_btn_call);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
dialog_btn_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your code here
myDialog.dismiss();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +mData.get(holder.getAdapterPosition()).getPhone()));
mContext.startActivity(intent);
}
});
}
});
}
Edit:-

Related

How to increment the value of edittext for each row in a recyclerview?

I am developing a shopping cart and I want to increase the number of Items purchased when a user clicks a button in each row of the recyclerview.
I have tried to increment the edit text when a user clicks the increase button but it updates value of all edittext in the recyclerview.
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="Add"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="#+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textColor="#FFF" />
<EditText
android:layout_weight="1"
android:id="#+id/numberOfItems"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/add"
android:inputType="phone" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="#+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/numberOfItems"
android:text="+"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dc3434"
android:text="Remove"
android:textColor="#FFF" />
</LinearLayout>
adapter
public class BreadAdapter extends RecyclerView.Adapter<BreadAdapter.ViewHolder> {
public List<BreadModel> breads;
Context context;
private int quntity= 0;
public BreadAdapter(List<BreadModel> breads, Context context) {
this.breads = breads;
this.context = context;
}
#NonNull
#Override
public BreadAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_bread_recyclerview, parent, false));
}
#Override
public void onBindViewHolder(#NonNull final BreadAdapter.ViewHolder holder, int position) {
final BreadModel bread_title = breads.get(position);
holder.tv_title.setText(bread_title.getName());
holder.tv_price.setText(String.valueOf(bread_title.getPrice()));
Log.i("info", bread_title.getImg());
GlideApp.with(context)
.load(bread_title.getImg())
holder.increase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
quntity= quntity+ 1;
holder.et_numberOfItems.setText(String.valueOf(quntity));
}
});
}
public void update(){
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return breads.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final EditText et_numberOfItems;
TextView tv_title;
TextView tv_price;
ImageView img_breads;
CardView cv_bread;
AppCompatButton add;
AppCompatButton remove;
AppCompatButton increase;
AppCompatButton decrease;
public ViewHolder(View itemView) {
super(itemView);
et_numberOfItems = (EditText) itemView.findViewById(R.id.numberOfItems);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_price = (TextView) itemView.findViewById(R.id.tv_price);
img_breads = (ImageView) itemView.findViewById(R.id.breadImageViews);
cv_bread = (CardView) itemView.findViewById(R.id.breadCardView);
add = (AppCompatButton) itemView.findViewById(R.id.add);
remove = (AppCompatButton) itemView.findViewById(R.id.remove);
increase = (AppCompatButton) itemView.findViewById(R.id.increase);
decrease = (AppCompatButton) itemView.findViewById(R.id.decrease);
}
}
}
Here is my adapter code please have look at it and help me thanks.
This is the screen shot
If I click on the plus(+) button it increases the number of item on all the edittext instead of that particular item

Call contacts from a list in Android Studio

I have an Android application where I used Linear Layout that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.
For now, all it do is show the contacts, but when I click on call, it does not take any action. I do not know how to make to mark
I tried with setOnClickListener().But Unsuccessful
My code:
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactViewHolder>{
Dialog myDialog;
private List<ContactModel> contactModelList;
private Context mContext;
public ContactsAdapter(List<ContactModel> contactModelList, Context mContext){
this.contactModelList = contactModelList;
this.mContext = mContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.single_contact_view, null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
final ContactModel contactModel = contactModelList.get(position);
holder.tvContactName.setText(contactModel.getContactName());
holder.tvPhoneNumber.setText(contactModel.getContactNumber());
holder.contactsRowLV.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view) {
Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return contactModelList.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
ImageView ivContactImage;
TextView tvContactName;
TextView tvPhoneNumber;
LinearLayout contactsRowLV;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
contactsRowLV = itemView.findViewById(R.id.contactsRowLV);
}
}
}
Here I show my XML of how I have. Help me please
<LinearLayout
android:id="#+id/contactsRowLV"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivContactImage"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center"
android:src="#drawable/ic_person"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="#+id/tvContactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"
android:text="Name"/>
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="25dp"
android:textColor="#android:color/primary_text_light"
android:text="Phone"/>
</LinearLayout>
</LinearLayout>
You just need to add following code in setOnClickListener(). This code is used to call particular number.
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
final ContactModel contactModel = contactModelList.get(position);
holder.tvContactName.setText(contactModel.getContactName());
holder.tvPhoneNumber.setText(contactModel.getContactNumber());
holder.contactsRowLV.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +contactModel.getContactNumber()));
mContext.startActivity(intent);
}
});
}

Using ViewHolder Pattern with FirebaseListAdapter

I want to use view holder pattern in my firebase list adapter. The firebase adapter looks a bit different than the ones with converter view because here in populateView method, the view parameter can't be null and always have a value, so I'm looking for the best way to implement the view holder pattern.
Here is my adapter class:
public class NewsAdapter extends FirebaseListAdapter<News> {
public NewsAdapter(Activity activity, Class<News> modelClass, int modelLayout, Query ref) {
super(activity, modelClass, modelLayout, ref);
}
#Override
protected void populateView(View view, final News news, int i) {
final Context context = view.getContext();
final String title = news.getTitle();
final String photoUrl = news.getPhotoUrl();
// Create views and assign values
ImageView ivPhoto = (ImageView) view.findViewById(R.id.iv_news_photo);
if (photoUrl != null)
// Load the image using Glide
Glide.with(context)
.load(photoUrl)
.into(ivPhoto);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_news_title);
tvTitle.setText(title)
}
}
Here is View Holder
public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title,desc,points,remaining;
public ImageView image;
public ItemClickListener clickListener;
public TaskViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.task_title);
desc = (TextView) itemView.findViewById(R.id.task_description);
points = (TextView) itemView.findViewById(R.id.task_point);
remaining = (TextView) itemView.findViewById(R.id.task_remaining_entries);
image = (ImageView) itemView.findViewById(R.id.task_image);
itemView.setOnClickListener(this);
}
public void setClickListener(ItemClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "clicked on task item", Toast.LENGTH_SHORT).show();
clickListener.onClick(view,getAdapterPosition(),false);
}
}
and here is what i'm doing to load data in RecyclerView
public void loadData(){
database = FirebaseDatabase.getInstance();
tasks = database.getReference("Tasks");
adapter = new FirebaseRecyclerAdapter<TaskPOJO, TaskViewHolder>(TaskPOJO.class, R.layout.task_item, TaskViewHolder.class, tasks) {
#Override
protected void populateViewHolder(TaskViewHolder viewHolder, final TaskPOJO model, int position) {
Log.wtf("TEstScript1", "populateViewHolder: "+model.getTitle() );
viewHolder.title.setText(model.getTitle());
viewHolder.desc.setText(model.getDescripttion()+"\n");
viewHolder.remaining.setText(model.getRemaining()+"/"+model.getPoint());
viewHolder.points.setText("Points "+model.getRemaining());
Glide.with(viewHolder.title.getContext())
.load(model.getImage())
.into(viewHolder.image);
viewHolder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(getActivity(), "" + model.getTitle(), Toast.LENGTH_SHORT).show();
Intent TaskPOJODetail = new Intent(getActivity(), Main.class);
TaskPOJODetail.putExtra("value","detail");
TaskPOJODetail.putExtra("taskId",adapter.getRef(position).getKey());
startActivity(TaskPOJODetail);
}
});
}
};
recyclerViewTasks.setAdapter(adapter);
}
here is my task_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="8dp"
android:clipChildren="true"
android:clipToPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="110dp"
android:layout_height="130dp"
android:id="#+id/task_image"
android:src="#drawable/test_tast"
android:scaleType="centerCrop"
/>
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:layout_alignParentRight="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/task_image"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title of Task"
android:textSize="18sp"
android:textColor="#color/colorAccent"
android:id="#+id/task_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30/300"
android:textSize="18sp"
android:layout_marginRight="16dp"
android:textColor="#color/colorAccent"
android:layout_alignParentRight="true"
android:id="#+id/task_remaining_entries"
/>
</RelativeLayout>
<me.biubiubiu.justifytext.library.JustifyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loren Ipsum is Simply dummy text of the printing and type settin industry. Developers and designers frequently use this\n "
android:layout_marginRight="16dp"
android:maxLines="3"
android:id="#+id/task_description"
android:lineSpacingMultiplier="1.1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Point 2345345"
android:textColor="#android:color/white"
android:background="#color/colorAccent"
android:layout_gravity="right"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:layout_marginTop="8dp"
android:id="#+id/task_point"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
hope this will help you.
You can find below example as a reference,
public class RecipeHolder extends RecyclerView.ViewHolder{
private static final String TAG = RecipeHolder.class.getSimpleName();
public TextView recipeName;
public ImageView recipeImage;
public RecipeHolder(View itemView) {
super(itemView);
recipeName = (TextView)itemView.findViewById(R.id.recipe_name);
recipeImage = (ImageView)itemView.findViewById(R.id.recipe_image);
}
}
And create your custom adapter
public class RecipeAdapter extends FirebaseRecyclerAdapter<RecipeObject, RecipeHolder>{
private static final String TAG = RecipeAdapter.class.getSimpleName();
private Context context;
public RecipeAdapter(Class<RecipeObject> modelClass, int modelLayout, Class<RecipeHolder> viewHolderClass, DatabaseReference ref, Context context) {
super(modelClass, modelLayout, viewHolderClass, ref);
this.context = context;
}
#Override
protected void populateViewHolder(RecipeHolder viewHolder, RecipeObject model, int position) {
viewHolder.recipeName.setText(model.getRecipename());
Glide.with(context).load(model.getRecipeImageUrl()).into(viewHolder.recipeImage);
}
}
For more reference you can refer FirebaseRecyclerView Adapter

setOnItemClickListener() not working on ListView (select always first row)

I have created a custom ListView by extending LinearLayout for every row (Contact) and i need to select the item, but the method "setOnItemClickListener()" not working. I have just put a onItemSelectedListener under and now the method "setOnItemClickListener" select always the first item though i select other row
MainActivity:
public class MainActivity extends AppCompatActivity {
private ListView lvPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPhone = (ListView)findViewById(R.id.listPhone);
final List<PhoneBook> listPhoneBook = new ArrayList<PhoneBook>();
listPhoneBook.add(new PhoneBook(BitmapFactory.decodeResource(getResources(),R.drawable.image),"Contact_1","123456789","av1#gmail.com","1"));
listPhoneBook.add(new PhoneBook(BitmapFactory.decodeResource(getResources(),R.drawable.image),"Contact_2","123456789","av2#gmail.com","2"));
listPhoneBook.add(new PhoneBook(BitmapFactory.decodeResource(getResources(),R.drawable.image),"Contact_3","123456789","av3#gmail.com","3"));
final PhoneBookAdapter adapter = new PhoneBookAdapter(this, listPhoneBook);
lvPhone.setAdapter(adapter);
lvPhone.setItemsCanFocus(false);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Login");
d.setCancelable(true);
d.setContentView(R.layout.account);
d.show();
Button button_close = (Button) d.findViewById(R.id.DCancel);
button_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
Button button_login = (Button) d.findViewById(R.id.DLogin);
button_login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String mName = new String("Ciao");
String mPhone;
String mEmail;
String mID;
TextView TextName = (TextView) d.findViewById(R.id.DName);
TextView TextPhone = (TextView)d.findViewById(R.id.DPhone);
TextView TextEmail = (TextView)d.findViewById(R.id.DEmail);
TextView TextID = (TextView)d.findViewById(R.id.DID);
mName=TextName.getText().toString();
mPhone=TextPhone.getText().toString();
mEmail=TextEmail.getText().toString();
mID=TextID.getText().toString();
listPhoneBook.add(new PhoneBook(BitmapFactory.decodeResource(getResources(),R.drawable.image),mName,mPhone,mEmail,mID));
lvPhone.setAdapter(adapter);
d.dismiss();
}
});
}
});
lvPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView TextName = (TextView) view.findViewById(R.id.tvName);
TextView TextPhone = (TextView)view.findViewById(R.id.tvPhone);
TextView TextEmail = (TextView)view.findViewById(R.id.tvEmail);
TextView TextID = (TextView)view.findViewById(R.id.tvID);
String tvName = new String(TextName.getText().toString());
String tvPhone = new String(TextPhone.getText().toString());
String tvEmail = new String(TextEmail.getText().toString());
String tvID = new String(TextID.getText().toString());
Toast.makeText(MainActivity.this, tvName, Toast.LENGTH_SHORT).show();
}
});
lvPhone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
PhoneBookAdapter:
public class PhoneBookAdapter extends BaseAdapter{
private Context mContext;
private List<PhoneBook> mListPhoneBook;
public PhoneBookAdapter (Context context, List<PhoneBook> list) {
mContext = context;
mListPhoneBook = list;
}
#Override
public int getCount() {
return mListPhoneBook.size();
}
#Override
public Object getItem(int position) {
return mListPhoneBook.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
PhoneBook entry = mListPhoneBook.get(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.phonebook_row,null);
}
ImageView ivAvatar = (ImageView)convertView.findViewById(R.id.imgAvatar);
ivAvatar.setImageBitmap(entry.getmAvatar());
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
tvName.setText(entry.getmName());
TextView tvPhone = (TextView)convertView.findViewById(R.id.tvPhone);
tvPhone.setText(entry.getmPhone());
TextView tvEmail = (TextView)convertView.findViewById(R.id.tvEmail);
tvEmail.setText(entry.getmEmail());
TextView tvID = (TextView)convertView.findViewById(R.id.tvID);
tvID.setText(entry.getmID());
return convertView;
}
}
`
PhoneBook_row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="fitCenter"
android:src="#drawable/image"
android:clickable="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvName"
android:textStyle="bold"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvPhone"
android:textStyle="bold"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvEmail"
android:textStyle="bold"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvID"
android:textStyle="bold"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
Replace your PhoneBook_row.xml with the one I am putting up here, I have tried this and it worked for me. I have changed all your android:clickable="true" to android:clickable="false". The reason for doing this is, all of your child views consume click and the result is your parent view i.e ListView not getting the click event. Hope this helps.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="fitCenter"
android:src="#drawable/image"
android:clickable="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="false">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvName"
android:textStyle="bold"
android:clickable="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvPhone"
android:textStyle="bold"
android:clickable="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvEmail"
android:textStyle="bold"
android:clickable="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvID"
android:textStyle="bold"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
Looks like you are missing the override.
Add an #Override before the function as shown below.
lvPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Also adding android:clickable = true in the layout file wouldn't be necessary.
You have changed your question , anyway i believe you can now get the item selected listener working. To get the item which is selected you can use the position as below
lvPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PhoneBook phoneBook = listPhoneBook.get(position);
Toast.makeText(MainActivity.this, phoneBook.getName() , Toast.LENGTH_SHORT).show();
//where getName is a function to get the name in the phonebook class
}
});

Android - SetOnItemClickListener doesn't work

I know here we have a lot of questions like mine, but I don't know why none works for me.
My objective: I have an AlertDialog with a ListView with a check box in each row, I can select some of the items, and I wish to make an ArrayList with the elements selected.
For that reason, I'm calling the SetOnclickListener, I put a Log inside the method, but does nothing.
I tried with focusable and clickable almost everywhere, but my Log doesn't appear.
Here My alert Dialog
private void callAdditionalDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(ConfigProductActivity.this);
final View additionalView = layoutInflater.inflate(R.layout.dialog_additional, null);
additionalView.setFocusable(true);
// set the custom dialog components - text, buttons, accountants
TextView titleDialog = (TextView) additionalView.findViewById(R.id.title_additional);
titleDialog.setTypeface(boldFont);
Button buttonAccept = (Button) additionalView.findViewById(R.id.button_accept);
buttonAccept.setTypeface(boldFont);
Button buttonCancel = (Button) additionalView.findViewById(R.id.button_cancel);
buttonCancel.setTypeface(boldFont);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ConfigProductActivity.this);
alertDialogBuilder.setView(additionalView);
final AlertDialog alertD = alertDialogBuilder.create();
alertD.setCanceledOnTouchOutside(false);
//Fill object of additional
final ListView additionalListView = (ListView) additionalView.findViewById(R.id.list_additional);
TextView additionalNotFound = (TextView) additionalView.findViewById(R.id.additional_not_found);
if (!withoutModifiers){
additionalAdapter = new AdditionalAdapter(ConfigProductActivity.this, additionalList);
additionalListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
additionalListView.setAdapter(additionalAdapter);
final ArrayList<ModifierEntity> modifierList = new ArrayList<ModifierEntity>();
additionalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Object modifier = additionalListView.getAdapter().getItem(position).toString();
Log.d(TAG, "SOMETHIIIIING");
}
});
}
else{
additionalListView.setVisibility(View.GONE);
additionalNotFound.setVisibility(View.VISIBLE);
additionalNotFound.setTypeface(font);
buttonCancel.setVisibility(View.GONE);
}
//End of fill object of additional
buttonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
additionalBox.setEnabled(true);
alertD.dismiss();
}
});
buttonAccept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//additional.setText(additionalAdapter.getCount());
additionalBox.setEnabled(true);
alertD.dismiss();
}
});
alertD.show();
}
Here my adapter:
public class AdditionalAdapter extends ArrayAdapter {
private static String TAG = AdditionalAdapter.class.getName();
private List<ModifierEntity> originalData = null;
private ConfigProductActivity activity;
private Typeface font;
private Typeface boldFont;
private static ModifierEntity modifier;
public AdditionalAdapter (ConfigProductActivity activity, List<ModifierEntity> listArray){
super(activity, R.layout.additional_item);
this.activity = activity;
this.originalData = listArray ;
font = Typeface.createFromAsset(activity.getAssets(),"HelveticaNeueThn.ttf");
boldFont = Typeface.createFromAsset(activity.getAssets(), "avgardm.ttf");
}
public static class Row
{
public TextView labelName;
public TextView labelPrice;
public CheckBox check;
}
#Override
public int getCount() {
return originalData.size();
}
#Override
public ModifierEntity getItem(int position) {
return originalData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int colorFont = activity.getResources().getColor(R.color.brown_tataki);
final Row holder;
View rowView = convertView;
// reuse views
if (convertView == null) {
holder = new Row();
LayoutInflater inflater = LayoutInflater.from(activity);
rowView = inflater.inflate(R.layout.additional_item, null);
rowView.setClickable(true);
//rowView.setFocusable(false);
// configure view holder
holder.labelName = (TextView) rowView.findViewById(R.id.additional_name);
holder.labelPrice = (TextView) rowView.findViewById(R.id.additional_price);
holder.check = (CheckBox) rowView.findViewById(R.id.additional_check);
rowView.setTag(holder);
}
else {
holder = (Row) convertView.getTag();
// rowView.setClickable(true);
}
final ModifierEntity itm = originalData.get(position);
holder.labelName.setText(itm.getModifier_name());
holder.labelName.setTypeface(font);
holder.labelName.setTextColor(colorFont);
holder.labelPrice.setText(GlobalParameters.CURRENCY.concat(String.valueOf(itm.getModifier_cost())));
holder.labelPrice.setTypeface(boldFont);
holder.labelPrice.setTextColor(colorFont);
return rowView;
}
}
Here My Dialog
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:background="#color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange_tataki"
android:text="#string/additional_title"
android:textColor="#color/white"
android:textSize="20sp"
android:gravity="center"
android:id="#+id/title_additional"
android:padding="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title_additional"
android:gravity="center"
android:background="#color/white"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_additional"
android:divider="#color/brown_tataki"
android:dividerHeight="0.5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/list_additional"
android:gravity="center"
android:padding="10dp"
android:visibility="gone"
android:textColor="#color/brown_tataki"
android:id="#+id/additional_not_found"
android:text="#string/additional_not_found"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_marginBottom="10dp"
android:layout_below="#+id/additional_not_found"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/button_cancel"
android:background="#color/green_tataki"
android:text="#string/button_cancel"
android:textSize="15sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_accept"
android:padding="10dp"
android:background="#color/green_tataki"
android:text="#string/button_accept"
android:textSize="15sp"
android:layout_marginLeft="15dp"
/>
</LinearLayout>
</RelativeLayout>
And Here my item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="QUESO"
android:singleLine="true"
android:padding="10dp"
android:textColor="#color/brown_tataki"
android:id="#+id/additional_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/additional_price"
android:padding="10dp"
android:text="Bs. 500"
android:textColor="#color/brown_tataki"
android:layout_toRightOf="#id/additional_name"
android:layout_marginLeft="10dp"
/>
<CheckBox
android:id="#+id/additional_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp" />
Using interfaces I can solve my problem, this link provided me the solution, and bassically consist in create an interface and implement in my activity.
Like this:
1.- Interface
public interface MyListener {
void folderClicked();
}
2.- Implements the interface in the activity
public class ActivityA extends Activity implements MyListener
3.- You will have to auto override the method folderClicked it will look like this:
#Override
protected void folderClicked() {
// Do your stuff here.
}
4.- Send the activity listener to the adapter with constructor like this:
MyAdpater adapter = new MyAdpater(ActivityA.this);
5.- Your adapter class your code should be like this:
public class TimeLineAdapter extends BaseAdapter {
private MyListener mListener;
public TimeLineAdapter(MyListener listener) {
super();
mListener = listener;
}
holder.iconImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.onFolderClicked()
//code to do stuff when the image is clicked
}

Categories

Resources