2017-07-21 1 views
0
package rjj.tutorial_jsonandlistview; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


import android.view.View; 
import android.widget.ListView; 
import android.widget.SearchView; 
import android.widget.TextView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 



public class DisplayListView extends AppCompatActivity { 

    String json_string; 
    JSONObject jsonObject; 
    JSONArray jsonArray; 
    ContactAdapter contactAdapter; 
    ListView listView; 
    SearchView sv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display_listview_layout); 
     listView = (ListView)findViewById(R.id.listview); 

     contactAdapter = new ContactAdapter(this,R.layout.row_layout); 
     listView.setAdapter(contactAdapter); 
     json_string = getIntent().getExtras().getString("json_data"); 
     //Searchview 
     sv = (SearchView)findViewById(R.id.search); 

     sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String query) { 
       return false; 
      } 

      @Override 
      public boolean onQueryTextChange(String newText) { 
       contactAdapter.getFilter().filter(newText); 
       return false; 
      } 
     }); 
     //End of Searchview 

     try { 
      jsonObject = new JSONObject(json_string); 
      jsonArray = jsonObject.getJSONArray("server_response"); 
      int count = 0; 
      String id ,firstname , surname, age , username, password; 


      while(count<jsonArray.length()){ 
       JSONObject JO = jsonArray.getJSONObject(count); 
       id = JO.getString("id"); 
       firstname = JO.getString("firstname"); 
       surname = JO.getString("surname"); 
       age = JO.getString("age"); 
       username = JO.getString("username"); 
       password = JO.getString("password"); 
       Contacts contact = new Contacts(id, firstname, surname, age,username,password); 
       contactAdapter.add(contact); 


       count++; 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

    public void hello(View view) { 

     Intent intent = new Intent(this, MainActivity.class); 
     final TextView textView = (TextView)findViewById(R.id.tx_id); 
     String id = textView.getText().toString(); 
     intent.putExtra("id", id); 
     startActivity(intent); 
    } 
} 

J'ai ajouté le widget searchview pour filtrer mais je suis confus pourquoi il ne recherche pas ou filtre les enregistrements au moins . SVP expliquez où j'ai mal fait. Je suis un débutant, alors expliquez-le de manière à ce que vous appreniez d'abord la programmation.Impossible de rechercher (avec searchview) pour un enregistrement dans la vue de liste qui est récupéré avec Json

Ci-dessous est mon journal d'erreur lorsque je tape quelque chose:

07-21 13:41:52.108 17026-17026/rjj.tutorial_jsonandlistview E/EGL_emulation: tid 17026: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH) 

Répondre

0

pour widget SearchView faire cette méthode override et l'essayer.

@Override 
public boolean onQueryTextChange(String newText) { 
    contactAdapter.getFilter().filter(newText); 
    return true; //here make it true 
} 

et contactAdapter ont ajouté suivant @Override

public Filter getFilter() 
{ 
    return new Filter(){ 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      constraint = constraint.toString().toLowerCase(); 
      FilterResults result = new FilterResults(); 

      if (constraint != null && constraint.toString().length() > 0) { 
       List<String> founded = new ArrayList<String>(); 
       for(YourListItemType item: origData){ 
        if(item.toString().toLowerCase().contains(constraint)){ 
         founded.add(item); 
        } 
       } 
       result.values = founded; 
       result.count = founded.size(); 
      }else { 
       result.values = origData; 
       result.count = origData.size(); 
      } 
      return result; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      clear(); 
      for (String item : (List<String>) results.values) { 
       add(item); 
      } 
      notifyDataSetChanged(); 
     } 
    } 
} 
+0

Où dois-je mettre le filtre public? –

+0

il devrait être adaptateur – Avinash

+0

Je ne comprends pas vraiment, où dois-je taper ce code? : / –