Reputation: 155
I am having a listview with buttons and I am trying to toast a text which will be the text of the textview with the id mobile when the user click on the button of the list item. My view class is as below and when I click on the button app closes ans restarts.
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.photo);
holder.tvName = (TextView) v.findViewById(R.id.doctor);
holder.tvMobile = (TextView) v.findViewById(R.id.mobile);
Button btnCall = (Button) v.findViewById(R.id.btnCall);
btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message= ((TextView) view.findViewById(R.id.mobile)).getText().toString();
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
});
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.noimage);
new DownloadImageTask(holder.imageview).execute(doctorList.get(position).getImage());
holder.tvName.setText(doctorList.get(position).getName());
holder.tvMobile.setText(doctorList.get(position).getMobile());
return v;
}
Upvotes: 0
Views: 564
Reputation: 3376
You are doing it wrong:-
instead of:-
@Override
public void onClick(View view) {
String message= ((TextView) view.findViewById(R.id.mobile)).getText().toString();
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
you need to do
@Override
public void onClick(View view) {
String message= ((TextView) v.findViewById(R.id.mobile).getText().toString();
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
Because you mobile text view is not a child of clicked button and you are trying to get his reference from the button which is wrong.
Upvotes: 1