Guest User

Untitled

a guest
Jun 14th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package de.bodprod.rettinfo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import android.app.ListActivity;
  6. import android.content.Context;
  7. import android.os.Bundle;
  8. import android.text.Editable;
  9. import android.text.TextWatcher;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.EditText;
  15. import android.widget.ListView;
  16. import android.widget.TextView;
  17.  
  18. public class AntidotList extends ListActivity{
  19.     ArrayList<HashMap<String, Object>> antidots;
  20.     ArrayList<HashMap<String, Object>> antidotsResult;
  21.     LayoutInflater inflater;
  22.    
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.        
  27.         setContentView(R.layout.antidotlist_layout);
  28.         final EditText searchBox=(EditText) findViewById(R.id.EditText01);
  29.         ListView antidotListView=(ListView) findViewById(android.R.id.list);
  30.        
  31.         inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  32.        
  33.         antidots = new ArrayList<HashMap<String,Object>>();        
  34.         DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  35.         antidots = db.getAllAntidots();
  36.        
  37.         antidotsResult = new ArrayList<HashMap<String,Object>>(antidots);
  38.        
  39.         final CustomAdapter adapter = new CustomAdapter(this, R.layout.antidotlist_layout, antidotsResult);
  40.         antidotListView.setAdapter(adapter);
  41.        
  42.         searchBox.addTextChangedListener(new TextWatcher() {
  43.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  44.                 //get the text in the EditText
  45.                 String searchString=searchBox.getText().toString();
  46.                 int textLength=searchString.length();
  47.  
  48.                 antidotsResult.clear();
  49.  
  50.                 for(int i=0;i<antidots.size();i++){
  51.                     String toxinname=antidots.get(i).get("toxin").toString();
  52.                     if(textLength<=toxinname.length()){
  53.                         if(toxinname.toUpperCase().indexOf(searchString.toUpperCase()) != -1)
  54.                             antidotsResult.add(antidots.get(i));
  55.                     }
  56.                 }
  57.                 adapter.notifyDataSetChanged();
  58.             }
  59.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  60.             public void afterTextChanged(Editable s) {}
  61.         });
  62.     }
  63.        
  64.     private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
  65.         public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
  66.             super(context, textViewResourceId, Strings);
  67.         }
  68.         private class ViewHolder{
  69.             TextView sqlite_id, tox_layout, antidot_layout;
  70.         }
  71.  
  72.         ViewHolder viewHolder;
  73.  
  74.         @Override
  75.         public View getView(int position, View convertView, ViewGroup parent) {
  76.             if(convertView==null){        
  77.                 convertView=inflater.inflate(R.layout.antidotlistitem_layout, null);
  78.                 viewHolder=new ViewHolder();
  79.        
  80.                 viewHolder.sqlite_id=(TextView) convertView.findViewById(R.id.sqlite_id);
  81.                 viewHolder.tox_layout=(TextView) convertView.findViewById(R.id.tox_layout);
  82.                 viewHolder.antidot_layout=(TextView) convertView.findViewById(R.id.antidot_layout);
  83.                
  84.                 convertView.setTag(viewHolder);
  85.                
  86.             }else{
  87.                 viewHolder=(ViewHolder) convertView.getTag();
  88.             }
  89.        
  90.             viewHolder.sqlite_id.setText(antidots.get(position).get("sql_id").toString());
  91.             viewHolder.tox_layout.setText(antidots.get(position).get("toxin").toString());
  92.             viewHolder.antidot_layout.setText(antidots.get(position).get("antidot").toString());
  93.      
  94.             return convertView;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment