I am Trying to Display the Contact From Phone to the ListView
(Which is used in a Fragment
)..... I have Tried Putting a SearchView
to Filter Data from ListView
.....
Search View Does Not Filter Data ....
Pls Help ...I Am badly Stuck...
ScreenShot of App having SearchView
over List View Showing Contact Details....
https://drive.google.com/file/d/0B8sFN35Zdhnfa0RtVEJKc2V2WG8/view?usp=sharing
https://drive.google.com/file/d/0B8sFN35ZdhnfWTljaTRWaGY3c1E/view?usp=sharing
contactfragment.xml , GetContactAdapter.java , Contact_Fragment3.java
are three different files..
**contractfragment.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchContactLIST"
android:queryHint="Search...."
android:clickable="true"
>
</SearchView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lists"
android:scrollbarStyle="outsideOverlay"
/>
</LinearLayout>
**GetContactAdapter.java**
package com.example.cosmic.zumi_test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class GetContactAdapter extends ArrayAdapter<String> implements Filterable {
String[] phone = {};
String[] names = {};
int[] img = {};
Context c;
LayoutInflater inflater;
public class Viewholder {
TextView names;
TextView address;
ImageView img;
}
public GetContactAdapter(Context context, String[] names, String[] add) {
super(context, R.layout.customcontactlist, names);
this.c = context;
this.names = names;
this.phone = add;
this.img = img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customcontactlist, null);
}
Viewholder viewholder = new Viewholder();
viewholder.names = (TextView) convertView.findViewById(R.id.contact_name);
viewholder.address = (TextView) convertView.findViewById(R.id.contact_no);
viewholder.img = (ImageView) convertView.findViewById(R.id.image_contactlist);
//ASSIGN DATA
viewholder.img.setImageResource(R.drawable.com_facebook_button_icon_blue);
viewholder.names.setText(names[position]);
viewholder.address.setText(phone[position]);
return convertView;
}
}
**Contact_Fragment3.java**
package com.example.cosmic.zumi_test;
import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.Toast;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/**
* Created by cosmic on 31/12/16.
*/
public class Contact_FRAGMENT3 extends Fragment {
private Uri uriContact;
private String contactID;
private ListView lstNames;
private GetContactAdapter adapter;
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
private android.widget.SearchView search;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.contactfragment, container, false);
this.lstNames = (ListView) v.findViewById(R.id.lists);
this.search = (android.widget.SearchView) v.findViewById(R.id.searchContactLIST);
// Read and show the contacts
showContacts();
return v;
}
private void showContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
List<String> contacts = getContactNames();
// String[] arr_contact=contacts.to
List<String> contacts_no = getContactNo();
String[] strarray = new String[contacts.size()];
contacts.toArray(strarray);
String[] strarray2 = new String[contacts_no.size()];
contacts_no.toArray(strarray2);
adapter = new GetContactAdapter(getContext(), strarray, strarray2);
lstNames.setAdapter(adapter);
search.setOnQueryTextListener(new android.widget.SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
showContacts();
} else {
Toast.makeText(getContext(), "Until you grant the permission, we cannot display the names", Toast.LENGTH_SHORT).show();
}
}
}
private List<String> getContactNames() {
List<String> contacts = new ArrayList<>();
List<String> number = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getActivity().getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numbers = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
number.add(numbers);
contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return contacts;
}
private List<String> getContactNo() {
List<String> number = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getActivity().getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
// String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numbers = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
number.add(numbers);
// contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return number;
}
}