Question

I have a listview with number and an image buton with each number. I want to make call on button click to the number in the row. my getview method is

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rowView = convertView;
    ContactStockView sv = null;
    if (rowView == null) {
        // Get a new instance of the row layout view
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(
                R.layout.activity_row, null);

        // Hold the view objects in an object,
        // so they don't need to be re-fetched
        sv = new ContactStockView();
        sv.name = (TextView) rowView.findViewById(R.id.textacti_row1);
        sv.number = (TextView) rowView.findViewById(R.id.textacti_row2);
        sv.btncall=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
        //sv.btncall.setOnClickListener((OnClickListener) activity);
        rowView.setTag(sv);
        //ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
      /*  */
    } else {
        sv = (ContactStockView) rowView.getTag();
    }

    // Transfer the stock data from the data object
    // to the view objects
    ContactStock currentStock = (ContactStock) stocks.get(position);
    sv.name.setText(currentStock.getName());
    number=currentStock.getNumber();
    sv.number.setText(number);
    //ImageButton btn=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
    sv.btncall.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            //Toast.makeText( activity, number, Toast.LENGTH_SHORT).show();
            String phoneCallUri = "tel:"+number;
            Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
            phoneCallIntent.setData(Uri.parse(phoneCallUri));
             activity.startActivity(phoneCallIntent);
        }
    });
    /*  btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            //Toast.makeText( activity, "abc", Toast.LENGTH_SHORT).show();
        /*}
    });*/
    // TODO Auto-generated method stub
    return rowView;
}
 protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageButton btncall;
    }

My log cat on call is enter image description here

Was it helpful?

Solution

you should declare the string number in get view method not outside it. change

number=currentStock.getNumber();

to

final String number=currentStock.getNumber();

and delete the string(ie,number) from outside getview method

OTHER TIPS

You can't findout the problem that way. You have to debug your getView method.

Do that by

1) double click on the first line of the method on the left of the outer frame to see a small point thay represents a breakpoint.

2) Right click on the project and click Debug as Android application.

3) Using the button F6 (in Eclipse) start moving steps untill you find the specific line you code produce error.

4) Now when you know the line it's easier to find out what happened.

attach the number to the view with setTag, then retrieve it in the click listener. you also don't need a new listener every time, define that once outside the function...

sv.btncall.setTag(number);

then in the onclick listener...

new View.OnClickListener() {
@Override
  public void onClick(View v) {

   String number = (String) v.getTag();
   String phoneCallUri = "tel:"+number;
   Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
   phoneCallIntent.setData(Uri.parse(phoneCallUri));
   activity.startActivity(phoneCallIntent);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top