2015-05-23 4 views
1

Так что я пытаюсь сделать просмотр с возможностью поиска с помощью адаптера настраиваемого массива.Searchable List View

Наконец-то я получил поиск, но результаты не совсем точны.

Его фактическая находка и пропустить что-то интересное, например, поиск Drupal дает мне Google Plus, вы знаете, что я говорю?

Так вот мой адаптер

public class CustomListAdapter extends ArrayAdapter<String> implements Filterable { 

private final Activity context; 
private final String[] web; 
private final String[] imageIdUrl; 


public CustomListAdapter(Activity context, String[] web, String[] imageId) { 
    super(context, R.layout.list_single, web); 
    this.context = context; 
    this.web = web; 
    this.imageIdUrl = imageId; 

} 

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    //find row view 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 
    //find text 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
    //find image 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
    //set text 
    txtTitle.setText(web[position]); 
    //set image traditional 
    //imageView.setImageResource(imageIdUrl[position]); 
    //set image with picasso 
    Picasso.with(this.context).load(imageIdUrl[position]).into(imageView); 
    //return row 
    return rowView; 
} 
} 

И Heres мой Основной класс активности

public class MainActivity extends ActionBarActivity { 

ListView list; 
EditText editsearch; 

String[] web = { 
     "Google Plus", 
     "Twitter", 
     "Windows", 
     "Bing", 
     "Itunes", 
     "Wordpress", 
     "Drupal"} ; 
String[] imageIdUrl = { 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg", 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg" , 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg", 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg", 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg", 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg", 
     "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, web, imageIdUrl); 
    list=(ListView)findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    // Locate the EditText in listview_main.xml 
    editsearch = (EditText) findViewById(R.id.editText); 

    editsearch.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      String text = editsearch.getText().toString().toLowerCase(Locale.getDefault()); 
      adapter.getFilter().filter(text); 
     } 
     @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 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

Я буду grately благодарен за любую помощь. Это доказывает, что основной головной болью

ответ

1

изменить код в OnTextChanged() метод для afterTextChanged() метод, как показано ниже:

@Override 
public void afterTextChanged(Editable s) { 

    String str=s.toString(); 
    getSearchItems(str); 
} 

В getSearchItems использовать фильтр. Я использовал другой способ фильтрации, как,

public void getSearchItems(String searchText){ 
    List<Employee> searchList=new ArrayList<Employee>(); 
    List<Employee> employeeList=getEmployeeList(); 
    if(employeeList2!=null){ 
     for(Employee e:employeeList){ 
      if(e.name.toLowerCase().contains(searchText.toLowerCase())){ 
      searchList.add(e); 
      } 
     } 
    } 
    adapter.refresh(searchList); 
} 

Затем адаптер использовать другой метод Refresh(), как показано ниже:

public void refresh(List<Employee> employeeList){ 
    this.employeeList=employeeList; 
    notifyDataSetChanged(); 
} 

На самом деле я использовал класс Employee, который содержит его имя и адрес, и я сделал поиск используя имя сотрудника. Таким образом, вы меняете код, который подходит для вашего поиска.