2012-11-26 2 views
1

ListView에 BaseAdapter가 있고 필자의 tipdoc 후에 필터를 만들고 코드가 작동하지 않습니다. EditText에 쓰기 시작하면 아무것도 표시되지 않습니다. 이유를 찾을 수 없습니다, 이건 내 코드입니다 : 당신이 있기 때문에 코드에서, 누메 VAR의 값을 변경해야기본 어댑터를 사용하여 목록 필터링

listView = (ListView) findViewById(android.R.id.list); 
    adapter = new ImageAndTextAdapter(Documente.this, R.layout.list_row,nume,tipdoc,formatdoc); 
    listView.setAdapter(adapter); 
    edt = (EditText) findViewById(R.id.search_box); 

    edt.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      ArrayList<String> nume_sort = new ArrayList<String>(); 
      ArrayList<String> tipdoc_sort = new ArrayList<String>(); 
      ArrayList<String> formatdoc_sort = new ArrayList<String>(); 

      int textlength = edt.getText().length(); 
      nume_sort.clear(); 
      tipdoc_sort.clear(); 
      formatdoc_sort.clear(); 

      for (int i = 0; i < tipdoc.size(); i++) 
      { 
      if (textlength <= tipdoc.get(i).length()) 
      { 
       if (edt.getText().toString().equalsIgnoreCase((String)  tipdoc.get(i).subSequence(0, textlength))) 
       { 
       tipdoc_sort.add(tipdoc.get(i)); 
       } 
      } 
      } 

      listView.setAdapter(new ImageAndTextAdapter 
      (Documente.this, R.layout.list_row,nume_sort,tipdoc_sort,formatdoc_sort)); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

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


    private class ImageAndTextAdapter extends BaseAdapter{ 
private Context adContext; 
public int getCount() { 
    return nume.size(); 
}    
public ImageAndTextAdapter(Context context,int layout,ArrayList<String> nume,ArrayList<String> tipdoc,ArrayList<String> formatdoc) 
    { 
    super(); 
     this.adContext = context; 
    } 

public View getView(int pos, View inView, ViewGroup parent){ 
    View v = inView; 
    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater)adContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.list_row, null); 
    } 

    String num = String.format(nume.get(pos)); 
    ((TextView)v.findViewById(R.id.Nume)).setText(num);  
    String tdoc = String.format(tipdoc.get(pos)); 
    ((TextView)v.findViewById(R.id.TDoc)).setText(tdoc); 
    if (formatdoc.get(pos).equals("doc")){ 
    ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.doc); 
    } 
    else if (formatdoc.get(pos).equals(".xlsx") || formatdoc.get(pos).equals("xls")) ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.xls); 
    else ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.pdf); 


    return v; 
} 
@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 
@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 
} 
+0

여전히 ArrayAd을 사용하지 않는 이유를 이해하지 못합니다. – njzk2

+0

도 어댑터에서 실제로 nume 변수의 내용을 변경하는 것처럼 보이지 않습니다. – njzk2

+0

감사의 njzk2, 그것에 대해 잊지 –

답변

3

그 같은

0

이 코드를 시도 ...

et.addTextChangedListener(new TextWatcher() 
     { 
      public void afterTextChanged(Editable s) 
      { 
        // Abstract Method of TextWatcher Interface. 
      } 
      public void beforeTextChanged(CharSequence s, 
        int start, int count, int after) 
      { 
       // Abstract Method of TextWatcher Interface. 
      } 
      public void onTextChanged(CharSequence s,int start, int before, int count) 
      { 
       textlength = et.getText().length(); 
       array_sort.clear(); 
       for (int i = 0; i < listview_array_location.length; i++) 
       { 
        if (textlength <= listview_array_location[i].length()) 
        { 
         if(et.getText().toString().equalsIgnoreCase((String)listview_array_location[i].subSequence(0,textlength))) 
         { 
          array_sort.add(listview_array[i]); 
         } 
         } 
       } 
       AppendList(array_sort); 
       } 
       }); 
    } 

    public void AppendList(ArrayList<String> str) 
    { 
     listview_arr = new String[str.size()]; 
     listview_arr = str.toArray(listview_arr); 

     setListAdapter(new bsAdapter(this)); 
    } 

    public class bsAdapter extends BaseAdapter 
    { 
     Activity cntx; 
     public bsAdapter(Activity context) 
     { 
      // TODO Auto-generated constructor stub 
      this.cntx=context; 

     } 

     public int getCount() 
     { 
      // TODO Auto-generated method stub 
      return listview_arr.length; 
     } 

     public Object getItem(int position) 
     { 
      // TODO Auto-generated method stub 
      return listview_arr[position]; 
     } 

     public long getItemId(int position) 
     { 
      // TODO Auto-generated method stub 
      return listview_array.length; 
     } 

     public View getView(final int position, View convertView, ViewGroup parent) 
     { 
      View row=null; 

      LayoutInflater inflater=cntx.getLayoutInflater(); 
      row=inflater.inflate(R.layout.search_list_item, null); 

      TextView tv=(TextView)row.findViewById(R.id.title); 

      tv.setText(listview_arr[position]); 



     return row; 
     } 
    }