2014-06-15 5 views
0

searchview acitonbar 위젯을 사용하여 목록보기를 필터링하려고하는데 edittext에 뭔가를 입력하면 검정 상자가 화면에 나타납니다. 그 블랙 박스없이 검색하는 법. enter image description hereSearchView 위젯이 제대로 작동하지 않습니다.

여기 내 코드입니다. 이렇게하려면 목록보기를 설정하기 때문에

public class MainActivity extends ListActivity implements OnQueryTextListener{ 

    private Button favListBtn; 
    private ListView mListView; 


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

     String[] categories = {"Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture", 
           "Food", "Cloth", "Shoes","Agriculture"}; 

     mListView = (ListView) findViewById(android.R.id.list); 
     mListView = getListView(); 

     ArrayAdapter<String> mAdapte = new ArrayAdapter<String>(MainActivity.this, 
       android.R.layout.simple_list_item_1, categories); 
     mListView.setAdapter(mAdapte); 
     mListView.setTextFilterEnabled(true); 

     mListView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

         new SearchAsynTask(MainActivity.this).execute(); 
         // selected item 
         String category = ((TextView) view).getText().toString(); 
         Intent intent = new Intent(MainActivity.this, BusinessListActivity.class); 
         startActivity(intent); 
         Toast.makeText(MainActivity.this, category, Toast.LENGTH_LONG).show(); 
         } 
      });  
     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 

     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     searchView.setSubmitButtonEnabled(true); 
     searchView.setOnQueryTextListener(this); 

     return super.onCreateOptionsMenu(menu); 

    } 

    @Override 
    public boolean onQueryTextSubmit(String query) {   

     return false; 
    } 

    @Override 
    public boolean onQueryTextChange(String query) { 
     if (TextUtils.isEmpty(query)) 
      { 
       mListView.clearTextFilter(); 
      } 
      else 
      { 
       mListView.setFilterText(query.toString()); 
      } 
     return true; 
    } 
    } 
+0

후 일부 코드 :

그런 다음 당신은 Filter 개체의 검색을 수행한다! –

+0

@Tim 제 질문을 업데이트했습니다. – FAISAL

+0

나는 그것이 당신 때문이라고 생각한다. Toast.makeText (MainActivity.this, category, Toast.LENGTH_LONG) .show(); – Cjames

답변

0

그것은 그 블랙 박스를 보여주고있다 : mListView.setTextFilterEnabled(true);

편집 : 당신은 위의 라인없이 검색을 할 수 없다고 말했다. 검색 목적으로 텍스트 필터링을 사용하기 때문입니다. 이것은 좋은 방법이 아닙니다. onQueryTextChange() 콜백에 mListView.setFilterText(query.toString()); (으)로 전화하고 있습니다. 따라서 텍스트 필터링을 사용하지 않으면 검색 메커니즘이 작동하지 않습니다.

적절한 방법은 ListView 어댑터가 Filterable 인터페이스를 구현하고 거기에서 필터링을 처리하는 것입니다. Filter.performFiltering(CharSequence)

+0

이 줄이 없으면 내 목록보기에서 검색 할 수 없습니다. – FAISAL

+0

이 답변을 업데이트했습니다. –

관련 문제