java搜索视图未搜索ListView
我正在尝试显示从电话到ListView
(在Fragment
中使用)的联系人。。。。。我已尝试放置SearchView
以从ListView
中筛选数据
搜索视图不过滤数据强>
请帮忙。。。我被卡住了
在显示联系人详细信息的列表视图上方有SearchView
的应用程序屏幕截图
https://drive.google.com/file/d/0B8sFN35Zdhnfa0RtVEJKc2V2WG8/view?usp=sharing
https://drive.google.com/file/d/0B8sFN35ZdhnfWTljaTRWaGY3c1E/view?usp=sharing
接触碎片。xml,GetContactAdapter。java,请联系\u Fragment3。java
有三个不同的文件
**contractfragment.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<SearchView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/searchContactLIST"
安卓:queryHint="Search...."
安卓:clickable="true"
>
</SearchView>
<ListView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/lists"
安卓:scrollbarStyle="outsideOverlay"
/>
</LinearLayout>
**GetContactAdapter.java**
package com.example.cosmic.zumi_test;
import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.BaseAdapter;
import 安卓.widget.Filter;
import 安卓.widget.Filterable;
import 安卓.widget.ImageView;
import 安卓.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 安卓.Manifest;
import 安卓.content.ContentResolver;
import 安卓.content.ContentUris;
import 安卓.content.Intent;
import 安卓.content.pm.PackageManager;
import 安卓.database.Cursor;
import 安卓.graphics.Bitmap;
import 安卓.net.Uri;
import 安卓.os.Build;
import 安卓.os.Bundle;
import 安卓.provider.ContactsContract;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v4.content.ContextCompat;
import 安卓.support.v4.view.MenuItemCompat;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.SearchView;
import 安卓.text.TextUtils;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuInflater;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Filterable;
import 安卓.widget.ListView;
import 安卓.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 安卓.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 = (安卓.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 安卓.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;
}
}
# 1 楼答案
如果可能对您有所帮助,请尝试此代码