I'm new to android development, and recently I wrote a code to show all saved contacts in an android device. For this, I used a custom list view which has 2 text views (to display contact name, contact number). It worked but now I want to get the contact name and the number (Toast message) when I click on list items. How to achieve this?
// list item click listener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Contact Name : " + contactList.get(i) , Toast.LENGTH_SHORT).show();
}
});
This is the code I wrote in MainActivity.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ListView list;
public String Contactname;
public String Contactnumber;
// array list
final ArrayList<Contacts> contactList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listContact);
fetchContact(); // call fetch contact method
// show number of contacts - toast message (testing purpose)
int count = contactList.size();
Toast.makeText(this, "Count : " + count , Toast.LENGTH_SHORT).show();
}
public void fetchContact()
{
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection ={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
String selection = null;
String[]selectionArgs = null;
String sortOrder =null;
ContentResolver resolver =getContentResolver();
Cursor cursor =resolver.query(uri,projection,selection,selectionArgs,sortOrder);
while(cursor.moveToNext()){
Contactname =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Contactnumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contacts contacts = new Contacts(Contactname,Contactnumber);
contactList.add(contacts);
}
contactAdapter adapter = new contactAdapter(MainActivity.this,R.layout.activity_list_item,contactList);
list.setAdapter(adapter);
// list item click listner
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Contact Name : " + contactList.get(i) , Toast.LENGTH_SHORT).show();
}
});
}
}
This is the Class I used to save Contact name, number., I stored class to an array object
public class Contacts {
// properties
public String name;
public String number;
public Contacts(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
You can fetch and data from textview Using Interface. For that You have to do Following Steps.
Step1: Create Interface in your contactAdapter.class File.TO create Interface You Can write.
public interface contactClickListener{
void onContactClick(List<contact> currentContact);
}
Step2: Then after Create an Object of Interface using below statment.
contactClickListener listener;
Step3: In contactAdapter OnBindMethod First fetch Current ContactInfo Using Below Statment.
Contacts currentContact=contactList.get(position);
Step 4: Then after On Contact adapater Implement OnClick event for Textview and inside onclick Event call your interface.
listener.onContactClick(currentContact);
Step 5: Currently Your have three parameter in contactAdapter so now add One Parameter in contactAdapter class.
contactAdapater(Existingparameter1 p1,Existingparameter2 p2,Existingparameter3 p3,contactClickListener listener){
this.listener=listener;
}
Step6: Then after Goto Your activity When You Create and call Contact Adapter. Implement Contact adapter method their using below Statement.
public class MainActivity extends AppCompatActivity implements contactadapter.OnContactClickListener{
}
Step 7: Then after change Contactadapter in activity to implement interface.
contactAdapter adapter = new contactAdapter(MainActivity.this,R.layout.activity_list_item,contactList);
list.setAdapter(adapter);
Step8: OncontactClick method is genrated in activity where You can get all details ov textview and your contacts model class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.