2017-09-05 9 views
-1

Ich kann nicht anrufen, wenn Sie auf die Listenansicht mit Name und Handynummer klicken.Anruf von lisview mit Name und Nummer in Arraylist

Weiter, wenn Click Event passieren, dann, wie mobile Nummer von Arraylist jedes Mal erhalten, klicken Sie auf Listview.

Hauptaktivität

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

Benutzerdefinierte Objekt

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; 

    } 

} 

Kontaktliste Aktivität

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


} 

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

Main Activity-Layout

Datei
<?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> 

List Item Layout-Datei

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

Kontaktliste Layout-Datei

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

das ist * wirklich * a * minimal * Beispiel? Lesen Sie [mcve] und überlegen Sie, was Sie [bearbeiten] könnten, um den Beispielcode zu reduzieren. –

Antwort

1

eine ItemClickListener Implement für das Listview innerhalb Kontaktliste Aktivität

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

Set List Artikel Klicken Listener

listView.setOnItemClickListener(this); 
Verwandte Themen