1

In my app I have 3 views

  • ImagiveView
  • TextView
  • Button

ImagView displays image accordingly, TextView and Button display names and number accordingly. But the problem is when I click on the button it does not call to the number which is displaying on the button. Although it does open the android caller app.

Telephone numbers are in string.xml file.

Here I provide my all files. Please help me

strings.xml

<string-array name="names">
    <item>Abdul Malik</item>
    <item>Adeel ur Rehman</item>
    <item>Asad Majeeb</item>
    <item>Ata ul Salam</item>
    <item>Atta ul Qadir</item>
    <item>Bilal Scunder</item>
    <item>Chaudry Adnan Ahmed</item>
    <item>Chaudry Imran</item>
    <item>Ejaz Ahmed Saroya</item>
    <item>Hamid Joya</item>

</string-array>

<string-array name="telephones">
        <item>0000000000</item>
        <item>0486607636</item>
        <item>0485256515</item>
        <item>0485128196</item>
        <item>0465922084</item>
        <item>0487150005</item>
        <item>0488627993</item>
        <item>0484783792</item>
        <item>0484688663</item>
        <item>0497697050</item>
    </string-array>

MainActivity.xml

package com.example.android.listview_with_custom_layout;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    ListView listView;
    int [] movie_poster_resource={
            R.drawable.movie_1,
            R.drawable.movie_2,
            R.drawable.movie_3,
            R.drawable.movie_4,
            R.drawable.movie_5,
            R.drawable.movie_6,
            R.drawable.movie_7,
            R.drawable.movie_8,
            R.drawable.movie_9,
            R.drawable.movie_10,

    };

    String [] names ={};
    String [] telephones ={};

    MoviesAdapter moviesAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=(ListView) findViewById(R.id.list_view);
        telephones =getResources().getStringArray(R.array.telephones);
        names =getResources().getStringArray(R.array.names);
        int i=0;

        moviesAdapter= new MoviesAdapter(getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(moviesAdapter);

        for(String titles: names){
            MovieDataProvider movieDataProvider= new MovieDataProvider(movie_poster_resource[i], titles, telephones[i]);

            moviesAdapter.add(movieDataProvider);
            i++;
        }
    }
}

MoviesDataAdapter

package com.example.android.listview_with_custom_layout;

/**
 * Created by temp on 2/11/2015.
 */
public class MovieDataProvider {
    private int movie_poster_resource;
    private String movie_title;
    private String telePhone;

    public MovieDataProvider(int movie_poster_resource, String movie_title, String telePhone) {
        this.setMovie_poster_resource(movie_poster_resource);
        this.setMovie_title(movie_title);
        this.telePhone = telePhone;
    }

    public int getMovie_poster_resource() {
        return movie_poster_resource;
    }

    public String getMovie_title() {
        return movie_title;
    }

    public String getTelePhone() {
        return telePhone;
    }

    public void setMovie_poster_resource(int movie_poster_resource) {
        this.movie_poster_resource = movie_poster_resource;
    }

    public void setMovie_title(String movie_title) {
        this.movie_title = movie_title;
    }

    public void setTelePhone(String telePhone) {
        this.telePhone = telePhone;
    }
}

MoviesAdapter

package com.example.android.listview_with_custom_layout;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by temp on 2/11/2015.
 */
public class MoviesAdapter extends ArrayAdapter {

    List list = new ArrayList();

    MovieDataProvider dataProvider;



    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();
        }


        dataProvider = (MovieDataProvider) this.getItem(position);
        handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
        handler.title.setText(dataProvider.getMovie_title());
        handler.telePhone.setText(dataProvider.getTelePhone());
        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()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            }
        });

        return row;
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffd953"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingTop="10dp">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/movie_1" />

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_toRightOf="@+id/movie_poster"
        android:gravity="center"
        android:text="This is movie name"
        android:textColor="#FFFFFF" />


    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/movie_title"
        android:gravity="center"
        android:text="call"
        android:textColor="#FFFF" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_below="@+id/movie_poster"
        android:background="#FFFF"></View>

</RelativeLayout>
bizimunda
  • 819
  • 2
  • 9
  • 26

1 Answers1

0

you forgot to initialize the telephones string in your MainActivity, do same as you did for movie_poster_resource after this it should work, and dont forgot to add permission

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

and last you can update the button click code with

Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+dataProvider.getTelePhone()));
    startActivity(callIntent);

and update your AdapterClass with this

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;
    final MovieDataProvider dataProvider = (MovieDataProvider) this.getItem(position);;

    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);
    } else {
        handler = (DataHandler) row.getTag();
    }

   handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
    handler.title.setText(dataProvider.getMovie_title());
    handler.telePhone.setText(dataProvider.getTelePhone());
    handler.telePhone.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + dataProvider.getTelePhone()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        }
    });
    row.setTag(handler);
    return row;
}
}
Rahul
  • 1,380
  • 1
  • 10
  • 24
  • Telephone array is initialized in the onCreate method. I have already added the permission. buttonClick code is already there. – bizimunda Dec 03 '15 at 11:44
  • no error messages. It just selects the number randomly. – bizimunda Dec 03 '15 at 11:51
  • you mean to say its calling to some random number right ?, if am wrong please correct me. – Rahul Dec 05 '15 at 11:36
  • yes it is calling random number from the list which is provided in the strings.xml file. For example if you call the first person from the list it selects the 5th number from the list. – bizimunda Dec 06 '15 at 08:09
  • i updated my answer with adapter class, accept my answer if it works.else let me know. – Rahul Dec 06 '15 at 15:08
  • Click on Tick symbol located at top left, eg- https://blog.stackoverflow.com/images/wordpress/stackoverflow-answers-not-accepted1.png – Rahul Dec 07 '15 at 09:17
  • thank you very much for your kind help. Hope we can share our knowledge with each other. – bizimunda Dec 07 '15 at 09:31