2017-07-21 17 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); 
    } 
} 

フィルタにsearchviewウィジェットを追加しましたが、少なくともレコードを検索またはフィルタリングしない理由が混乱しています。私が間違ったところを説明してください。私は初心者ですので、最初にプログラミングを学ぶ方法で親切に説明してください。 searchviewウィジェットのリストビューでJsonで検索されたリストビューのレコードを検索できません

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

答えて

0

がオーバーライドとして、このメソッドを作成し、それを試してみてください。

以下は、私が何かを入力したときに、私のエラーログです。

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

とcontactAdapterでは、私は公共のフィルターを置くべき@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

次追加されましたか? –

+0

それはアダプターでなければなりません – Avinash

+0

私は本当にそれを取得しない、このコードを入力する必要がありますか? :/ –

関連する問題