2017-01-02 3 views
-2

Ich versuche, den Kontakt Vom Telefon zum ListView anzuzeigen (die in einem Fragment verwendet wird) ..... Ich habe versucht, eine SearchView Putting Daten von ListView auf Filter .....Suche nicht Searching Listview

Suche Enthält Daten, die nicht filtern ....

Pls Hilfe ... ich bin schlecht stuck ...

ScreenShot von App mit SearchView über Listenansicht angezeigt Kontaktdaten ....

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

sind drei verschiedene Dateien ..

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

     } 


    } 
+0

Haben Sie in GetContactAdapter keinen Fehler erhalten? Weil Sie keine Methoden von Filterable überschrieben haben? – Raghavendra

+0

@Raghavendra Pls erklären ... –

+0

Überprüfen Sie [diese] (http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview) – Raghavendra

Antwort

0

diesen Code versuchen, wenn es helfen könnte Sie

public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener { 

    private SearchView mSearchView; 
    private ListView mListView; 

    private final String[] mStrings = Cheeses.sCheeseStrings; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

     setContentView(R.layout.searchview_filter); 

     mSearchView = (SearchView) findViewById(R.id.search_view); 
     mListView = (ListView) findViewById(R.id.list_view); 
     mListView.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       mStrings)); 
     mListView.setTextFilterEnabled(true); 
     setupSearchView(); 
    } 

    private void setupSearchView() { 
     mSearchView.setIconifiedByDefault(false); 
     mSearchView.setOnQueryTextListener(this); 
     mSearchView.setSubmitButtonEnabled(true); 
     mSearchView.setQueryHint("Search Here"); 
    } 

    public boolean onQueryTextChange(String newText) { 
     if (TextUtils.isEmpty(newText)) { 
      mListView.clearTextFilter(); 
     } else { 
      mListView.setFilterText(newText.toString()); 
     } 
     return true; 
    } 

    public boolean onQueryTextSubmit(String query) { 
     return false; 
    } 
} 
+0

Bitte erklären Sie dem Autor wie es werde lösen. Post eine Beschreibung zusammen mit Code? – Raghavendra

+0

Was ist falsch in meinem Code ... Der Code Liste der Kontakt Sehr gut ... Aber nur Problem sucht ... Pls Hilfe –

+0

@siddhesh Mehr über Ihre Liste wird nicht zurückgesetzt ... Entfernen aller Char aus der Suchansicht. . –