-3

PrintScreen

I have a listView with 3 Views 1) ImageView 2) TextView 3) Button

What I want to make is that when I click on a button, it gets triggered and call to the specific person. Telephone numbers are stored in strings.xml file as

<string-array name="telePhoneNummber">
        <item>123</item>
        <item>8765</item>
        <item>565767</item>
</string-array>

And here is my Adapter Class.

public class MoviesAdapter extends ArrayAdapter {

List list = new ArrayList();


public MoviesAdapter(Context context, int resource) {
    super(context, resource);
}

static class DataHandler {
    ImageView Poster;
    TextView title;
    Button telePhone;
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return this.list.size();
}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View row;
    row = convertView;

    DataHandler handler;
    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.row_layout, parent, false);
        handler = new DataHandler();
        handler.Poster = (ImageView) row.findViewById(R.id.movie_poster);
        handler.title = (TextView) row.findViewById(R.id.movie_title);
        handler.telePhone = (Button) row.findViewById(R.id.btn_call);

        row.setTag(handler);

    } else {
        handler = (DataHandler) row.getTag();
    }

    MovieDataProvider dataProvider;
    dataProvider = (MovieDataProvider) this.getItem(position);
    handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
    handler.title.setText(dataProvider.getMovie_title());
    handler.telePhone.setText(dataProvider.getTelePhone());

    return row;
}

}

Don't pay attention on the naming convention please.

bizimunda
  • 819
  • 2
  • 9
  • 26

2 Answers2

0

In your getView() method, simply add an onClickListener() to the appropriate button resource...

handler.telePhone.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Intent to launch phone dialer
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + dataProvider.getTelePhone().trim()));
        context.startActivity(intent);
    }
});

EDIT 1: not sure whether the NullPointerException in your onClickListener occurs on line 82 or 83 of your adapter class but you probably need to pass context to it. Add the following to your current code:

Below List list = new ArrayList(); add Context context;

Change your constructor to:

public MoviesAdapter(Context context, int resource) {
    super(context, resource);
    this.context = context;
}

And see if it solves the problem.

EDIT 2: Or, leave your original code untouched and in the onClickListener, change context.startActivity(intent); to initActivity(intent); for which you'll need to add the following method:

private void initActivity(Intent intent) {
    this.getContext().startActivity(intent);
}
mjp66
  • 4,214
  • 6
  • 26
  • 31
  • I'm getting this error message java.lang.NullPointerException at line 82 and line 82 is intent.setData(Uri.parse("tel:" + dataProvider.getTelePhone().trim())); – bizimunda Nov 16 '15 at 13:55
  • Are you inserting the onClickListener before declaring the handler.telePhone resource? Without more logcat info it's not possible to pinpoint the exception. – mjp66 Nov 16 '15 at 14:11
  • No, I'm not declaring it before handler.telePhone – bizimunda Nov 16 '15 at 14:36
  • Can you post the logcat? – mjp66 Nov 16 '15 at 14:53
  • at com.example.android.listview_with_custom_layout.MoviesAdapter$1.onClick(MoviesAdapter.java:83) at android.view.View.performClick(View.java:3591) at android.view.View$PerformClick.run(View.java:14263) at android.os.Handler.handleCallback(Handler.java:605) – bizimunda Nov 16 '15 at 19:02
  • at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4507) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) – bizimunda Nov 16 '15 at 19:02
  • at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method) – bizimunda Nov 16 '15 at 19:02
  • Not sure now if your error is on line 82 or 83.... see my edited answer in a minute... – mjp66 Nov 16 '15 at 19:32
  • still getting the same error message. I tried both solutions. – bizimunda Nov 17 '15 at 10:19
  • is there anyone who can help me please? – bizimunda Nov 18 '15 at 11:10
  • I tried to refactor the code. Now what is happening is, no matter which button I click on, it always dial the number 6th which is 0487150005. Please see prinScreen attached above. – bizimunda Nov 20 '15 at 08:38
  • If you're now able to launch the dialer with a phone number inserted, then the current question is resolved. If the same number is inserted for each list item, then you're not passing your data properly to the adapter. You should ask a new question regarding this, and if you do, please post all relevant code (your activity, adapter, xml, logcat, etc), so that people don't have to resort to guessing when trying to formulate an answer. Debugging your own code first would help too (I would take a close look at how/where you are calling public void add(Object object)). – mjp66 Nov 20 '15 at 09:04
  • thank you very much mjp66. I really appreciate your help. How you will have look on my code? it will notify you that I have posted another question? – bizimunda Nov 24 '15 at 18:42
0

Write your Listview on click listener and open the phone Dialer like this:

    listView.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
          String[] numberArray= context.getResources().getStringArray(R.array.telePhoneNummber); // get array from strings.xml

       // launch dialer with pre-filled phone number
        Intent phoneDialerIntent= new Intent(Intent.ACTION_DIAL);
        phoneDialerIntent.setData(Uri.parse("tel:" +  numberArray[position]));
        startActivity(phoneDialerIntent);


        } 

    }); 
Anjali
  • 1
  • 1
  • 13
  • 20