Friday 20 September 2013

Filtering with basea adapter

this is my main activity edittext 

edtsearch.addTextChangedListener(searchTextWatcher);

make funcetion

private TextWatcher searchTextWatcher = new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if(phonebookadp != null){
                String myst = s.toString();
              
                if(myst.equalsIgnoreCase(s.toString())){
                    phonebookadp.getFilter().filter(s.toString());
                }
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
        public void afterTextChanged(Editable s) {}
    };


This is my adapter 

   
    public class ImportPhonebook_Adapter extends BaseAdapter implements Filterable{
      
        private LayoutInflater l_Inflater;
        Filter myFilter;

        public ImportPhonebook_Adapter(Context context) {
            l_Inflater = LayoutInflater.from(context);
        }
        public int getCount() {
            // TODO Auto-generated method stub
                return contactname.size();
        }

        public Object getItem(int position) {
            return contactname.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = l_Inflater.inflate(R.layout.contactlist, null);
                holder = new ViewHolder();
                holder.fname = (TextView) convertView.findViewById(R.id.contactlist);
                holder.lname = (TextView) convertView.findViewById(R.id.contactlistlname);
                holder.textnum = (TextView)convertView.findViewById(R.id.textnumber);
                holder.imguser = (ImageView)convertView.findViewById(R.id.imageViewuser);
                holder.chkbx = (CheckBox)convertView.findViewById(R.id.checkBox1);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.chkbx.setVisibility(View.GONE);
          
            try{
                if(contactname != null){
                    ContactSetGet contasetget = new ContactSetGet();
                    contasetget = contactname.get(position);
                  
                    cont_id = contasetget.getmyId();
                    Log.i(tag, "my cont id...."+cont_id);
                  
              
                            strmob_no = contasetget.getMainnumber();
                            if(!TextUtils.isEmpty(strmob_no)){
                                holder.textnum.setText(strmob_no);
                            }else{
                                holder.textnum.setVisibility(View.INVISIBLE);
                            }
                            strfname = contasetget.getFname();
                            strgivenname = contasetget.getLname();
                      
                              
                                if(!TextUtils.isEmpty(strfname)){
                                    holder.fname.setText(contasetget.getFname());  
                                }else{
                                    holder.fname.setVisibility(View.GONE);
                                }
                                if(!TextUtils.isEmpty(strgivenname)){
                                    holder.lname.setText(contasetget.getLname());  
                                }else{
                                    holder.lname.setVisibility(View.GONE);
                                }
                              
                            String photoId = contasetget.getPhoto();
                          
                            Log.i("Phonebook_Adapter", "image ....."+photoId);
                            if(photoId != null){
                                if(photoId.equalsIgnoreCase("00")){
                                    holder.imguser.setImageResource(R.drawable.ic_launcher);
                                }else if(photoId.contains("mnt")){
                                    Bitmap bitmap = BitmapFactory.decodeFile(photoId);
                                    holder.imguser.setImageBitmap(bitmap);
                                  
                                }
                                else{
                                    photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photoId));
                                    holder.imguser.setImageURI(photoUri);
                                }
                            }else{
                                holder.imguser.setImageResource(R.drawable.ic_contact_picture_holo_light);
                            }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return convertView;
        }
      
        @Override
        public android.widget.Filter getFilter() {
            // TODO Auto-generated method stub
            return new android.widget.Filter() {
              
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    // TODO Auto-generated method stub
                    contactname = (ArrayList<ContactSetGet>) results.values;
                    ImportPhonebook_Adapter.this.notifyDataSetChanged();  
                }
              
                @TargetApi(Build.VERSION_CODES.GINGERBREAD)
                @SuppressLint({ "DefaultLocale", "NewApi" })
                @Override
                protected FilterResults performFiltering(CharSequence prefix) {
                    // TODO Auto-generated method stub
                    FilterResults results = new FilterResults();
                    List<ContactSetGet> i = new ArrayList<ContactSetGet>();
                  
                    String myna = prefix.toString();
                  
                    if (prefix!= null && prefix.toString().length() > 0) {

                        for (int index = 0; index < contactname.size(); index++) {
                            ContactSetGet si = contactname.get(index);
                            Log.i("----------------si.getFirstName()---------","."+si.getFname());
                            Log.i("----------------prefix---------","."+prefix.toString());
                            //String number
                          
                            myna = si.getFname().toLowerCase();
                            if(myna.startsWith(prefix.toString())){
                                Log.i(tag, "insert name..."+si.getFname());
                              i.add(si);
                            }
                        }
                        results.values = i;
                        results.count = i.size();                 
                    }
                    else{
                        synchronized (contactname){
                            results.values = mycontnamesetget;
                            results.count = mycontnamesetget.size();
                        }
                    }
                    return results;
                }
            };
        }
}


contactname is my main listview

List<ContactSetGet> mycontnamesetget;
mycontnamesetget = new ArrayList<ContactSetGet>();

fill this list same where main list fiil up
mycontnamesetget = contactname;  

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...