2017-09-05 53 views
-1

點擊具有姓名和手機號碼的列表視圖時,我無法撥打電話。如何打電話給lisview,在arraylist中有姓名和號碼

此外,當點擊事件發生時,每次點擊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", "90")); 
     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); 
    } 


} 

定義適配器

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

主要活動佈局文件

<?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> 
+1

那是真的* * A *最小*的例子嗎?閱讀[mcve]並考慮您可能會[編輯]以減少示例代碼。 –

回答

1

實現一個ItemClickListener的聯繫人列表內的ListView活動

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

集列表項點擊監聽器

listView.setOnItemClickListener(this); 
相關問題