Android 如何使用arraylist中的姓名和号码从listview拨打电话

Android 如何使用arraylist中的姓名和号码从listview拨打电话,android,Android,当点击有姓名和手机号码的列表视图时,我无法呼叫 此外,当click事件发生时,如何在每次单击listview时从arraylist获取手机号码 主要活动 package com.example.vinod.mycommunity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; i

当点击有姓名和手机号码的列表视图时,我无法呼叫

此外,当click事件发生时,如何在每次单击listview时从arraylist获取手机号码

主要活动

package com.example.vinod.mycommunity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

    }

    public void contactClick (View view){

        // Create a new intent to open the {@link NumbersActivity}
        Intent numbersIntent = new Intent(MainActivity.this, ContactList.class);

        // Start the new activity
        startActivity(numbersIntent);
    }
}
自定义对象

    package com.example.vinod.mycommunity;



/**
 * Created by vinod on 24/8/17.
 */

public class CustomContactList {

    private String mNames;
    private String mNumber;


    public CustomContactList(String Names, String Numbers) {
        mNames = Names;
        mNumber = Numbers;
    }

    public String getmNames() {
        return mNames;

    }

    public String getmNumber() {
        return mNumber;

    }

}
联系人列表活动

package com.example.vinod.mycommunity;


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

import java.util.ArrayList;


public class ContactList extends AppCompatActivity {


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

        // Create an arrayList of Contact Name and contact Number
        ArrayList<CustomContactList> contactsArray = new ArrayList<CustomContactList>();

        contactsArray.add(new CustomContactList("Vinod Lohar", "9987376064"));
        contactsArray.add(new CustomContactList("Mukesh Lohar", "9983154742"));
        contactsArray.add(new CustomContactList("Arjun Lohar", "9694544296"));
        contactsArray.add(new CustomContactList("Sapna Lohar", "9521130633"));
        contactsArray.add(new CustomContactList("Ramesh Lohar", "7718835888"));
        contactsArray.add(new CustomContactList("Manju Lohar", "9029788725"));
        contactsArray.add(new CustomContactList("Jagdish Lohar", "9987409707"));
        contactsArray.add(new CustomContactList("Rekha Lohar", "9001239708"));
        contactsArray.add(new CustomContactList("Jyoti Lohar", "9828146608"));
        contactsArray.add(new CustomContactList("Dinesh Lohar", "9521663206"));
        contactsArray.add(new CustomContactList("Chunnibai Lohar", "9521085134"));
        contactsArray.add(new CustomContactList("Rekha Lohar Udaipur", "9828504595"));
        contactsArray.add(new CustomContactList("Jagdish Ji Lohar Udaipur", "9828119641"));


        ContactAdapter adapter = new ContactAdapter(this, contactsArray);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
    }


}
public void onItemClick(AdapterView<?> adapterView, View view, int     position, long l)
{    
    // Get Phone Number    
    String phone = ((CustomContactList)adapter.getItem(position)).getmNumber();

    // Make a call
    Intent phoneIntent = new Intent(Intent.ACTION_CALL);
    phoneIntent.setData(Uri.parse("tel:" + phone));
    startActivity(phoneIntent);
}
package com.example.vinod.mycommunity;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.ListView;
导入java.util.ArrayList;
公共类联系人列表扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u contact\u list);
//创建联系人姓名和联系人号码的arrayList
ArrayList contactsArray=新的ArrayList();
contactsArray.add(新的客户联系人列表(“Vinod Lohar”,“9987376064”);
contactsArray.add(新的客户联系人列表(“Mukesh Lohar”,“9983154742”);
contactsArray.add(新的客户联系人列表(“Arjun Lohar”,“9694544296”);
contactsArray.add(新的客户联系人列表(“Sapna Lohar”、“9521130633”);
contactsArray.add(新的客户联系人列表(“Ramesh Lohar”,“7718835888”);
contactsArray.add(新的客户联系人列表(“Manju Lohar”,“9029788725”));
contactsArray.add(新的客户联系人列表(“Jagdish Lohar”,“9987409707”);
contactsArray.add(新的客户联系人列表(“Rekha Lohar”,“9001239708”);
contactsArray.add(新的客户联系人列表(“Jyoti Lohar”,“9828146608”);
添加(新的客户联系人列表(“Dinesh Lohar”,“9521663206”);
contactsArray.add(新的客户联系人列表(“Chunnibai Lohar”,“9521085134”);
contactsArray.add(新的客户联系人列表(“Rekha Lohar Udaipur”,“9828504595”);
contactsArray.add(新的客户联系人列表(“Jagdish Ji Lohar Udaipur”,“9828119641”);
ContactAdapter=新的ContactAdapter(此为contactsArray);
ListView ListView=(ListView)findViewById(R.id.list);
setAdapter(适配器);
}
}
自定义适配器

package com.example.vinod.mycommunity;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TextView;


import java.util.ArrayList;


/**
 * Created by vinod on 25/8/17.
 */

public class ContactAdapter extends ArrayAdapter<CustomContactList> {

    public ContactAdapter(Activity context, ArrayList<CustomContactList> contactsArray) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.
        super(context, 0, contactsArray);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        // Get the CustomContactList object located at this position in the list
        CustomContactList currentContactList = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID Name_TextView
        TextView nameTextView = (TextView) listItemView.findViewById(R.id.Name_TextView);
        // Get the contact name from the current CustomContactList object and
        // set this text on the name TextView
        nameTextView.setText(currentContactList.getmNames());

        // Find the TextView in the list_item.xml layout with the Id Number_TextView
        TextView numberTextView = (TextView) listItemView.findViewById(R.id.Number_TextView);
        // Get the contact number from the current CustomContactList object and
        // set this text on the number TextView
        numberTextView.setText(currentContactList.getmNumber());

        // Return the whole list item layout (containing 2 TextViews)
        // so that it can be shown in the ListView
        return listItemView;
    }
}
package com.example.vinod.mycommunity;
导入android.app.Activity;
导入android.support.annotation.NonNull;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
/**
*维诺德于2017年8月25日创作。
*/
公共类ContactAdapter扩展了ArrayAdapter{
公共联系人适配器(活动上下文,ArrayList contactsArray){
//这里,我们为上下文和列表初始化ArrayAdapter的内部存储。
//第二个参数在ArrayAdapter填充单个TextView时使用。
//因为这是两个TextView和一个ImageView的自定义适配器,所以适配器不是
//我们将使用第二个参数,所以它可以是任何值。
超级(上下文,0,contactsArray);
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.list_项,父项,false);
}
//获取位于列表中此位置的CustomContactList对象
CustomContactList currentContactList=getItem(位置);
//在列表_item.xml布局中查找ID名为_TextView的TextView
TextView Name TextView=(TextView)listItemView.findViewById(R.id.Name\u TextView);
//从当前CustomContactList对象获取联系人姓名,然后
//在名称TextView上设置此文本
nameTextView.setText(currentContactList.getmNames());
//在列表\u item.xml布局中查找Id号为\u TextView的TextView
TextView numberTextView=(TextView)listItemView.findViewById(R.id.Number\u TextView);
//从当前CustomContactList对象获取联系人号码,然后
//在数字文本视图上设置此文本
numberTextView.setText(currentContactList.getmNumber());
//返回整个列表项布局(包含2个文本视图)
//以便可以在ListView中显示它
返回listItemView;
}
}
主活动布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/contact_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#a4c639"
        android:onClick="contactClick"
        android:text="Contacts"
        android:textAlignment="center"
        android:textSize="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    android:padding="16dp">


    <TextView
        android:id="@+id/Name_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Names" />

    <TextView
        android:id="@+id/Number_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Numbers" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context="com.example.vinod.mycommunity.ContactList">

    <EditText
        android:id="@+id/textBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Contacts"
        android:paddingLeft="14dp"
        android:textSize="14sp" />

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

</LinearLayout>

列表项布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/contact_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#a4c639"
        android:onClick="contactClick"
        android:text="Contacts"
        android:textAlignment="center"
        android:textSize="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    android:padding="16dp">


    <TextView
        android:id="@+id/Name_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Names" />

    <TextView
        android:id="@+id/Number_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Numbers" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context="com.example.vinod.mycommunity.ContactList">

    <EditText
        android:id="@+id/textBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Contacts"
        android:paddingLeft="14dp"
        android:textSize="14sp" />

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

</LinearLayout>

联系人列表布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/contact_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#a4c639"
        android:onClick="contactClick"
        android:text="Contacts"
        android:textAlignment="center"
        android:textSize="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    android:padding="16dp">


    <TextView
        android:id="@+id/Name_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Names" />

    <TextView
        android:id="@+id/Number_TextView"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:textAlignment="textStart"
        android:textSize="14sp"
        tools:text="Numbers" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context="com.example.vinod.mycommunity.ContactList">

    <EditText
        android:id="@+id/textBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Contacts"
        android:paddingLeft="14dp"
        android:textSize="14sp" />

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

</LinearLayout>

为联系人列表活动中的listView实现ItemClickListener

package com.example.vinod.mycommunity;


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

import java.util.ArrayList;


public class ContactList extends AppCompatActivity {


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

        // Create an arrayList of Contact Name and contact Number
        ArrayList<CustomContactList> contactsArray = new ArrayList<CustomContactList>();

        contactsArray.add(new CustomContactList("Vinod Lohar", "9987376064"));
        contactsArray.add(new CustomContactList("Mukesh Lohar", "9983154742"));
        contactsArray.add(new CustomContactList("Arjun Lohar", "9694544296"));
        contactsArray.add(new CustomContactList("Sapna Lohar", "9521130633"));
        contactsArray.add(new CustomContactList("Ramesh Lohar", "7718835888"));
        contactsArray.add(new CustomContactList("Manju Lohar", "9029788725"));
        contactsArray.add(new CustomContactList("Jagdish Lohar", "9987409707"));
        contactsArray.add(new CustomContactList("Rekha Lohar", "9001239708"));
        contactsArray.add(new CustomContactList("Jyoti Lohar", "9828146608"));
        contactsArray.add(new CustomContactList("Dinesh Lohar", "9521663206"));
        contactsArray.add(new CustomContactList("Chunnibai Lohar", "9521085134"));
        contactsArray.add(new CustomContactList("Rekha Lohar Udaipur", "9828504595"));
        contactsArray.add(new CustomContactList("Jagdish Ji Lohar Udaipur", "9828119641"));


        ContactAdapter adapter = new ContactAdapter(this, contactsArray);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
    }


}
public void onItemClick(AdapterView<?> adapterView, View view, int     position, long l)
{    
    // Get Phone Number    
    String phone = ((CustomContactList)adapter.getItem(position)).getmNumber();

    // Make a call
    Intent phoneIntent = new Intent(Intent.ACTION_CALL);
    phoneIntent.setData(Uri.parse("tel:" + phone));
    startActivity(phoneIntent);
}
这真的是一个极小的例子吗?阅读并考虑如何减少示例代码。