2017-08-31 10 views
1

私はAndroidプログラミングでは新しく、私のBaseadapterにカスタムフィルタを作成したいと思います。BaseAdapterのオブジェクトをフィルタリングします

私が他の質問をチェックすると正直なところ混乱します。なぜなら、彼らは主にArraylistsを使用しているからです。私はすでに私のListViewに小さな絵文字を設定するカスタムView(getView)を作成しました。私はすでにArraylistのサンプルからこれらのカスタムフィルタを実装するために多くの方法を試しましたが、どういうわけか私は封鎖に入ります。

誰かが少なくともオブジェクトの私に

をカスタムフィルタのためのいくつかの方向性を与えることができればそれは非常に参考になるcryptoListAdapter.java

public class cryptoListAdapter extends BaseAdapter { 
    private Context context; 
    LayoutInflater mInlfater; 
    ArrayList<HashMap<String,String>> currencyList; 
    TextView name; 



    // Constructor 
    public cryptoListAdapter(Context context,ArrayList<HashMap<String,String>> currencyList) 
    { 
     mInlfater = LayoutInflater.from(context); 
     this.currencyList = currencyList; 
    } 





    @Override 
    public int getCount() { 
     return currencyList.size(); 

    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     cryptoPicto cp = new cryptoPicto(); 

     View newView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_crypto_items, null); 
     name = (TextView) newView.findViewById(R.id.name); 
     TextView symbol = (TextView) newView.findViewById(R.id.symbol); 
     ImageView image_list_icon = (ImageView)newView.findViewById(cryptopicto); 
     //cp.createFinalFileName(name); 

     HashMap<String, String> map; 
     map = currencyList.get(position); 
     name.setText(map.get("name")); 
     symbol.setText(map.get("symbol")); 
     Picasso.with(newView.getContext()).load(map.get("cryptopicto")).into(image_list_icon); 


     return newView; 
    } 

currencyTableView.java

事前に
public class currencyTableView extends AppCompatActivity { 

    private String TAG = currencyTableView.class.getSimpleName(); 

    private ProgressDialog pDialog; 
    private ListView lv; 
    private SearchView sv; 
    private ImageView im; 
    private static String url = "https//myURLToJSON"; 
    Context context; 
    cryptoListAdapter adapter; 

    ArrayList<HashMap<String, String>> currencyList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_currency_table_view); 
     sv = (SearchView) findViewById(R.id.search_currency); 

     currencyList = new ArrayList<>(); 

     lv = (ListView) findViewById(R.id.list); 
     im = (ImageView) findViewById(R.id.cryptopicto); 


     new GetCurrencies().execute(); 



    } 


    // URL to get currencies JSON 


    private class GetCurrencies extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(currencyTableView.this); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      databasehandler sh = new databasehandler(); 
      cryptoPicto cp = new cryptoPicto(); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url); 

      Log.e(TAG, "Response from url: " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONArray jsonArray = new JSONArray(jsonStr); 


        // looping through currencies 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject c = jsonArray.getJSONObject(i); 

         String name = c.getString("name"); 
         String symbol = c.getString("symbol"); 
         Double price_usd = c.getDouble("price_usd"); 
         Double price_eur = c.getDouble("price_eur"); 
         Double price_btc = c.getDouble("price_btc"); 
         Double volume_eur = c.getDouble("volume_eur"); 
         Double market_cap_usd = c.getDouble("market_cap_usd"); 
         Double percent_change_1h = c.getDouble("percent_change_1h"); 
         Double percent_change_24h = c.getDouble("percent_change_24h"); 
         Double percent_change_7d = c.getDouble("percent_change_7d"); 


         // tmp hash map for single currency 
         HashMap<String, String> currency = new HashMap<>(); 

         // create Path to picto Filename 

         cp.createFinalFileName(name); 

         // adding each child node to HashMap key => value 
         currency.put("cryptopicto", cp.getFinalFileName()); 
         currency.put("name", name); 
         currency.put("symbol", symbol); 

         // adding currency to currencyList 
         currencyList.add(currency); 

        } 
       } catch (final JSONException e) { 
        Log.e(TAG, "Json parsing error: " + e.getMessage()); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Json parsing error: " + e.getMessage(), 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 

       } 
      } else { 
       Log.e(TAG, "Couldn't get json from server."); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Couldn't get json from server. Check LogCat for possible errors!", 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      /** 
      * Updating parsed JSON data into ListView 
      * */ 



      adapter = new cryptoListAdapter(
          currencyTableView.this, currencyList) { 

        }; 

      lv.setAdapter(adapter); 

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

       @Override 
       public boolean onQueryTextChange(String currencyList) { 

        adapter.getFilter().filter(currencyList); 
        return false; 
       } 
      }); 
     } 
    } 
} 

感謝:)

答えて

1

あなたのBaseAdapderのインプリメンテーションフィルター可能 し、 "}"

@Override 
public Filter getFilter() { 

    Filter filter = new Filter() { 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 

     currencyList = (ArrayList<HashMap<String,String>>) results.values; 

      notifyDataSetChanged(); 
     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 
      ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>(); 

      if (mOriginalValues == null) { 
       mOriginalValues = new ArrayList<HashMap<String, String>>(currencyList); // saves 

      } 


      if (constraint == null || constraint.length() == 0) { 

       // set the Original result to return 
       results.count = mOriginalValues.size(); 
       results.values = mOriginalValues; 
      } else { 
       Locale locale = Locale.getDefault(); 
       constraint = constraint.toString().toLowerCase(locale); 
       for (int i = 0; i < mOriginalValues.size(); i++) { 
         HashMap<String, String> currency = mOriginalValues.get(i); 
        String data = currency.get("name"); 
        if (data.toLowerCase(locale).startsWith(constraint.toString())) { 

         FilteredArrList.add(currency); 
        } 

       } 
       // set the Filtered result to return 
       results.count = FilteredArrList.size(); 
       results.values = FilteredArrList; 

      } 
      return results; 
     } 
    }; 
    return filter; 
} 

をお使いのアダプタクラスで

ArrayList<HashMap<String,String>> currencyList; 

List<HashMap<String,String>> mOriginalValues; 

を追加し、閉じる括弧の前下部に、このメソッドを追加してみましょう私はそれが助けるかどうかを知る

+0

ありがとう!それは妥当と思われる、私は仕事から戻ってすぐにそれを試してみましょう! – Jagson

+1

これが役立つ場合。答えを正しいとマークアップしてください。 –

+0

本当に助けてくれました!ありがとう!私もそれを理解した:) – Jagson