2015-05-09 63 views
0

我是新來的android。我想要搜索功能到我的自定義ListView。 我正在項目中,我試圖過濾我的listView使用addOnTextChangedListener 我在網上看到我沒有發現任何有用的東西在我的情況。無法過濾自定義列表視圖android

在這裏我的代碼。

EmployeeFragment.java

public class EmployeeFragment extends Fragment { 

private EditText searchEditText1; 
EmployeeDatabaseHelper dbHelper; 
ListView employeeList; 
public Cursor employees; 
private ArrayList<Employee> arrlst; 
private myadapter myadapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_employee, container, false); 
    searchEditText1 = (EditText) rootView.findViewById(R.id.searchEditText1); 
    employeeList = (ListView) rootView.findViewById(R.id.employeeList); 

    searchEditText1.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      ArrayList<Employee> tempList = new ArrayList<Employee>(); 
      for(Employee emp : arrlst){ 
       if(emp.name.contains(s) || emp.username.contains(s)) 
        tempList.add(emp); 
      } 
      //I am stucked here... 
      //What to do here 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 
    return rootView; 
} 

@Override 
public void onResume() { 
    populateEmployeeList(); 
    super.onResume(); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    populateEmployeeList(); 
} 

public void populateEmployeeList(){ 

    dbHelper=new EmployeeDatabaseHelper(getActivity()); 
    arrlst = dbHelper.getAllEmployee(); 
    myadapter = new myadapter(getActivity()); 
    employeeList.setAdapter(myadapter); 
} 

class myadapter extends ArrayAdapter<Employee> { 

Activity con; 
private LayoutInflater Inflater; 

public myadapter(Activity context) { 
    super(context,R.layout.row); 
    con=context; 
    Inflater = (LayoutInflater)con.getSystemService(con.LAYOUT_INFLATER_SERVICE); 
} 

class ViewHolder{ 
    TextView tvid; 
    TextView tvname; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View vi = convertView; 
    ViewHolder vh; 
    if(vi==null) 
    { 
     vh = new ViewHolder(); 
     vi= Inflater.inflate(R.layout.row, null); 
     vh.tvid=(TextView)vi.findViewById(R.id.mainTextView); 

     vh.tvname=(TextView)vi.findViewById(R.id.subTextView); 
     vi.setTag(vh); 

    }else{ 
     vh=(ViewHolder)vi.getTag(); 
    } 
    Employee e =arrlst.get(position); 
    vh.tvid.setText(e.name); 
    vh.tvname.setText(e.username); 
    return vi; 
} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return arrlst.size(); 
} 
} 
} 

和員工類

Employee.java

public class Employee { 
    public String username; 
    public String name; 
    public String password; 
} 
+0

如果你有 「我卡住」 的評論,把'myadapter.notifyDataSetChanged()'。 – JonasCz

回答

0

試一試,一旦你把你的ELE在templist中,您需要等同templist中的arrlst並重新初始化適配器。 - >

@Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
      ArrayList<Employee> tempList = new ArrayList<Employee>(); 
      for(Employee emp : arrlst){ 
       if(emp.name.contains(s) || emp.username.contains(s)) 
        tempList.add(emp); 
      } 
      //I am stucked here... 
      //What to do here 

      arrlst = tempList; 
      myadapter = new myadapter(getActivity()); 
      employeeList.setAdapter(myadapter); 



     } 
+0

我把這個代碼,但仍ListVIew沒有得到過濾。 –

0

您必須implements Filterable您的適配器

然後調用

myFilter.addTextChangedListener(new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

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

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    dataAdapter.getFilter().filter(s.toString()); 
    } 
    }); 

欲瞭解更多信息請訪問此link

+0

仍然不能正常工作顯示錯誤'空指針期望值' –