2013-12-19 87 views
1

這是我的主要活動的.java[Android]如何僅對名字和姓氏進行搜索?

package com.example.search; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.Filter; 
import android.widget.ListView; 

public User getUserByName(String firstName, String lastName) { 
    synchronized (userList) { 
     Iterator<User> iterator = userList.iterator(); 
     while (iterator.hasNext()) { 
      User user = (User) iterator.next(); 
      if (user.getFirstName().equals(firstName) 
        && user.getLastName().equals(lastName)) { 
       return user; 
      } 
     } 
     // no user found 
     return null; 
    } 
} 


public class MainActivity extends FragmentActivity { 
    private EditText firstNameEditText; 
    private EditText lastNameEditText; 
    private UserListAdapter<User> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     firstNameEditText = (EditText) findViewById(R.id.firstNameEditText); 
     lastNameEditText = (EditText) findViewById(R.id.lastNameEditText); 
     final ListView listView = (ListView) findViewById(R.id.listView); 
     ArrayList<User> usersList = new ArrayList<User>(); 
     // add items to list 
     adapter = new UserListAdapter<User>(this, 
       R.layout.textview_layout, usersList); 
     listView.setAdapter(adapter); 
     firstNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus) { 
        adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_FIRST_NAME); 
       } 
      } 
     }); 
     lastNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus) { 
        adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_LAST_NAME); 
       } 
      } 
     }); 
     TextWatcher textWatcher = new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       adapter.getFilter().filter(s.toString()); 
      } 
     }; 

     firstNameEditText.addTextChangedListener(textWatcher); 
     lastNameEditText.addTextChangedListener(textWatcher); 
    } 

    public static class UserListAdapter<T> extends ArrayAdapter<T> { 
     public static final int MODE_FILTER_BY_FIRST_NAME = 0xFEED; 
     public static final int MODE_FILTER_BY_LAST_NAME = 0xDEAF; 
     private List<T> objects; 
     private int filterMode; 

     public UserListAdapter(Context context, int textViewResourceId, 
       List<T> objects) { 
      super(context, textViewResourceId, objects); 
      init(objects); 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId, T[] objects) { 
      super(context, resource, textViewResourceId, objects); 
      init(Arrays.asList(objects)); 
     } 

     private void init(List<T> objects) { 
      this.objects = objects; 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId, List<T> objects) { 
      super(context, resource, textViewResourceId, objects); 
      init(objects); 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId) { 
      super(context, resource, textViewResourceId); 
     } 

     public UserListAdapter(Context context, int resource) { 
      super(context, resource); 
     } 

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

       @Override 
       protected void publishResults(CharSequence constraint, 
         FilterResults results) { 
        UserListAdapter.this.clear(); 
        ArrayList<T> list = (ArrayList<T>) results.values; 
        UserListAdapter.this.addAll(list); 
        if (results.count > 0) { 
         notifyDataSetChanged(); 
        } else { 
         notifyDataSetInvalidated(); 
        } 
       } 

       @Override 
       protected synchronized FilterResults performFiltering(
         CharSequence constraint) { 
        FilterResults results = new FilterResults(); 
        ArrayList<T> list = new ArrayList<T>(); 
        Iterator<T> iterator = objects.iterator(); 
        while (iterator.hasNext()) { 
         T t = (T) iterator.next(); 
         User user = (User) t; 
         String s = null; 
         if (filterMode == MODE_FILTER_BY_FIRST_NAME) { 
          s = user.getFirstName(); 
         } else if (filterMode == MODE_FILTER_BY_LAST_NAME) { 
          s = user.getLastName(); 
         } 
         if (s.equals(constraint.toString())) { 
          list.add(t); 
         } 
        } 
        results.values = list; 
        results.count = list.size(); 
        return results; 
       } 
      }; 
     } 

     public int getFilterMode() { 
      return filterMode; 
     } 

     public void setFilterMode(int filterMode) { 
      this.filterMode = filterMode; 
     } 
    } 

    public static class User { 
     // attributes 
     private String accountID; 
     private String companyName; 
     private String designation; 
     private String firstName; 
     private String lastName; 
     private String profileimageBLOB; 

     // behaviors 
     public User() // default constructor 
     { 
      // attributes will be initialized to their default values 
     } 

     public User(String accID, String comName, String design, String fName, 
       String lName, String image) // parameterized constructor 
     { 
      accountID = accID; 
      companyName = comName; 
      designation = design; 
      firstName = fName; 
      lastName = lName; 
      profileimageBLOB = image; 
     } 

     // set and get methods 
     public void setAccountID(String accID) { 
      accountID = accID; 
     } 

     public String getAccountID() { 
      return accountID; 
     } 

     public void setCompanyName(String comName) { 
      companyName = comName; 
     } 

     public String getCompanyName() { 
      return companyName; 
     } 

     public void setDesignation(String design) { 
      designation = design; 
     } 

     public String getDesignation() { 
      return designation; 
     } 

     public void setFirstName(String fName) { 
      firstName = fName; 
     } 

     public String getFirstName() { 
      return firstName; 
     } 

     public void setLastName(String lName) { 
      lastName = lName; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

     public void setProfileImage(String image) { 
      profileimageBLOB = image; 
     } 

     public String getProfileImage() { 
      return firstName; 
     } 

     // toString() method 
     public String toString() { 
      return accountID + "\t" + companyName + "\t" + designation + "\t\t" 
        + firstName + "\t" + lastName + "\t" + profileimageBLOB; 
     } 
    } 

我做出相應的調整,以@Gopal饒,這是我上面的代碼,但是我面對了很多的錯誤,我無法理解,我在做正確嗎?

請指教,但仍然沒有工作

+0

什麼叫整個字符串是什麼意思? –

+0

你的搜索邏輯/方法在哪裏? – TheLostMind

+0

他可能是指「而不是toString()方法」。至於解決方案,@GopalRao的答案將適用於完全匹配。 – NitroNbg

回答

5

試試這個...

public User getUserByName(String firstName, String lastName) { 
    synchronized (userList) { 
     Iterator<User> iterator = userList.iterator(); 
     while (iterator.hasNext()) { 
      User user = (User) iterator.next(); 
      if (user.getFirstName().equals(firstName) 
        && user.getLastName().equals(lastName)) { 
       return user; 
      } 
     } 
     // no user found 
     return null; 
    } 
} 


public class MainActivity extends FragmentActivity { 
    private EditText firstNameEditText; 
    private EditText lastNameEditText; 
    private UserListAdapter<User> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     firstNameEditText = (EditText) findViewById(R.id.firstNameEditText); 
     lastNameEditText = (EditText) findViewById(R.id.lastNameEditText); 
     final ListView listView = (ListView) findViewById(R.id.listView); 
     ArrayList<User> usersList = new ArrayList<User>(); 
     // add items to list 
     adapter = new UserListAdapter<User>(this, 
       R.layout.textview_layout, usersList); 
     listView.setAdapter(adapter); 
     firstNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus) { 
        adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_FIRST_NAME); 
       } 
      } 
     }); 
     lastNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus) { 
        adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_LAST_NAME); 
       } 
      } 
     }); 
     TextWatcher textWatcher = new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       adapter.getFilter().filter(s.toString()); 
      } 
     }; 

     firstNameEditText.addTextChangedListener(textWatcher); 
     lastNameEditText.addTextChangedListener(textWatcher); 
    } 

    public static class UserListAdapter<T> extends ArrayAdapter<T> { 
     public static final int MODE_FILTER_BY_FIRST_NAME = 0xFEED; 
     public static final int MODE_FILTER_BY_LAST_NAME = 0xDEAF; 
     private List<T> objects; 
     private int filterMode; 

     public UserListAdapter(Context context, int textViewResourceId, 
       List<T> objects) { 
      super(context, textViewResourceId, objects); 
      init(objects); 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId, T[] objects) { 
      super(context, resource, textViewResourceId, objects); 
      init(Arrays.asList(objects)); 
     } 

     private void init(List<T> objects) { 
      this.objects = objects; 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId, List<T> objects) { 
      super(context, resource, textViewResourceId, objects); 
      init(objects); 
     } 

     public UserListAdapter(Context context, int resource, 
       int textViewResourceId) { 
      super(context, resource, textViewResourceId); 
     } 

     public UserListAdapter(Context context, int resource) { 
      super(context, resource); 
     } 

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

       @Override 
       protected void publishResults(CharSequence constraint, 
         FilterResults results) { 
        UserListAdapter.this.clear(); 
        ArrayList<T> list = (ArrayList<T>) results.values; 
        UserListAdapter.this.addAll(list); 
        if (results.count > 0) { 
         notifyDataSetChanged(); 
        } else { 
         notifyDataSetInvalidated(); 
        } 
       } 

       @Override 
       protected synchronized FilterResults performFiltering(
         CharSequence constraint) { 
        FilterResults results = new FilterResults(); 
        ArrayList<T> list = new ArrayList<T>(); 
        Iterator<T> iterator = objects.iterator(); 
        while (iterator.hasNext()) { 
         T t = (T) iterator.next(); 
         User user = (User) t; 
         String s = null; 
         if (filterMode == MODE_FILTER_BY_FIRST_NAME) { 
          s = user.getFirstName(); 
         } else if (filterMode == MODE_FILTER_BY_LAST_NAME) { 
          s = user.getLastName(); 
         } 
         if (s.equals(constraint.toString())) { 
          list.add(t); 
         } 
        } 
        results.values = list; 
        results.count = list.size(); 
        return results; 
       } 
      }; 
     } 

     public int getFilterMode() { 
      return filterMode; 
     } 

     public void setFilterMode(int filterMode) { 
      this.filterMode = filterMode; 
     } 
    } 

    public static class User { 
     // attributes 
     private String accountID; 
     private String companyName; 
     private String designation; 
     private String firstName; 
     private String lastName; 
     private String profileimageBLOB; 

     // behaviors 
     public User() // default constructor 
     { 
      // attributes will be initialized to their default values 
     } 

     public User(String accID, String comName, String design, String fName, 
       String lName, String image) // parameterized constructor 
     { 
      accountID = accID; 
      companyName = comName; 
      designation = design; 
      firstName = fName; 
      lastName = lName; 
      profileimageBLOB = image; 
     } 

     // set and get methods 
     public void setAccountID(String accID) { 
      accountID = accID; 
     } 

     public String getAccountID() { 
      return accountID; 
     } 

     public void setCompanyName(String comName) { 
      companyName = comName; 
     } 

     public String getCompanyName() { 
      return companyName; 
     } 

     public void setDesignation(String design) { 
      designation = design; 
     } 

     public String getDesignation() { 
      return designation; 
     } 

     public void setFirstName(String fName) { 
      firstName = fName; 
     } 

     public String getFirstName() { 
      return firstName; 
     } 

     public void setLastName(String lName) { 
      lastName = lName; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

     public void setProfileImage(String image) { 
      profileimageBLOB = image; 
     } 

     public String getProfileImage() { 
      return firstName; 
     } 

     // toString() method 
     public String toString() { 
      return accountID + "\t" + companyName + "\t" + designation + "\t\t" 
        + firstName + "\t" + lastName + "\t" + profileimageBLOB; 
     } 
    } 
} 

有關同步的照顧......

+0

這不是搜索功能。它從列表中刪除項目。 **你能解釋一下,如果用戶從Edittext中刪除所有文本會發生什麼。它顯示所有項目的前一個列表? –

+0

@MdAbdulGafur thanx,編輯答案... –

+0

謝謝,它只返回一個用戶,但如果兩個用戶名有姓和名和兩個字符相同。 ListView是由用戶數組的List不是一個單獨的對象。 此外,你不告訴我顯示他更新ListView中的userList,這裏需要調用這個函數。 請再次編輯您的答案並提供更多信息。 –

0

你需要做的,然後自定義搜索你需要添加自定義ListView搜索功能。

這是一個很大的例子,我給你這個鏈接。我認爲它可以幫助你。

Link

感謝

0

你需要重寫getFilter在適配器類中的方法perforFiltering使用 一些迭代方法獲取數據的適當列表。

請按照以下link