2016-09-26 54 views
0

我正在创建我的第一个应用程序。我想添加搜索功能到我的应用程序。我尝试了很多互联网上的解决方案,但并没有给出预期的结果。应用程序已回收listview。在应用程序中搜索后,它应该过滤单词,并应显示具有相同单词的列表项。 这是我的MainActivity如何在recycleview listview中搜索

public class MainActivity extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.word_list); 


    final ArrayList<Word> words = new ArrayList<Word>(); 



    words.add(new Word("nine" , R.string.nine, R.drawable.a)); 
    words.add(new Word("one" , R.string.one , R.drawable.a)); 
    words.add(new Word("two" , R.string.two , R.drawable.a)); 
    words.add(new Word("three" , R.string.three,R.drawable.a)); 
    words.add(new Word("four" , R.string.four , R.drawable.a)); 
    words.add(new Word("five" , R.string.five, R.drawable.a)); 
    words.add(new Word("six" , R.string.six , R.drawable.a)); 
    words.add(new Word("seven" , R.string.seven ,R.drawable.a)); 
    words.add(new Word("eight" , R.string.eight , R.drawable.a)); 
    words.add(new Word("nine" , R.string.nine, R.drawable.a)); 
    words.add(new Word("one" , R.string.one , R.drawable.a)); 
    words.add(new Word("two" , R.string.two , R.drawable.a)); 
    words.add(new Word("three" , R.string.three,R.drawable.a)); 
    words.add(new Word("four" , R.string.four , R.drawable.a)); 
    words.add(new Word("five" , R.string.five, R.drawable.a)); 
    words.add(new Word("six" , R.string.six , R.drawable.a)); 
    words.add(new Word("seven" , R.string.seven ,R.drawable.a)); 
    words.add(new Word("eight" , R.string.eight , R.drawable.a)); 
    words.add(new Word("nine" , R.string.nine, R.drawable.a)); 


    final WordAdapter adapter = new WordAdapter(this , words); 

    ListView listView = (ListView) findViewById(R.id.list); 

    listView.setAdapter(adapter); 



    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView , View view, 
           int position, long l) { 
      Word word = words.get(position); 
      int ray = word.getStringId(); 
      Intent i = new Intent(MainActivity.this , DisplayActivity.class); 
      i.putExtra("my key" , getString(ray)); 
      startActivity(i); 


     } 
    }); 


} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_home, menu); 
    // Retrieve the SearchView and plug it into SearchManager 
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 


    return true; 


} 






} 

这是适配器

 public class WordAdapter extends ArrayAdapter<Word> { 
    public WordAdapter(Context context, ArrayList<Word> words) { 
    super(context, 0, words); 


} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Check if an existing view is being reused, otherwise inflate the view 
    View listItemView = convertView; 
    if (listItemView == null) { 
     listItemView = LayoutInflater.from(getContext()).inflate(
       R.layout.list_item, parent, false); 
    } 


    Word currentWord = getItem(position); 


    TextView searchTextView = (TextView) listItemView.findViewById(R.id.text1); 

    searchTextView.setText(currentWord.getWordSearch()); 

    ImageView imageView = (ImageView) listItemView.findViewById(R.id.list_item_icon); 

    imageView.setImageResource(currentWord.getImageId()); 




    return listItemView; 
} 
} 

,这是word.java

public class Word { 


private int mStringId; 
private int mImageId; 



private String mWordSearch; 


public Word(String wordSearch, int stringId , int imageId) { 

    mWordSearch = wordSearch; 
    mStringId = stringId; 
    mImageId = imageId; 

} 


public String getWordSearch() { 
    return mWordSearch; 
} 

public int getImageId(){ 
    return mImageId; 

} 

public int getStringId(){ 
    return mStringId; 

} 


} 

这是list_itemxml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:orientation="horizontal" 

android:minHeight="70dp" 
> 




<ImageView 
    android:id="@+id/list_item_icon" 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:layout_marginLeft="15dp"/> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="50dp" 
    android:id="@+id/textContainer" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:paddingLeft="16dp"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:paddingTop="15dp" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:textAppearance="?android:textAppearanceMedium" 

     /> 




</LinearLayout> 

+0

当您尝试执行搜索时会发生什么?你说它是“没有给出想要的结果” - 预期的功能是什么,你遇到什么错误/问题。这些细节比发布整个项目源代码更有帮助。 – ishmaelMakitla

+0

我不知道如何实现searchView。有解决方案,但我无法理解它们。你可以看看代码,并告诉我如何添加searchview,谢谢 – NicolasWRefn

回答

0

这里我提供了一个标准和简单的例子,它代表了带有搜索机制的回收列表视图。

https://github.com/Wrdlbrnft/Searchable-RecyclerView-Demo

我在我的应用程序应用相同的,它的工作原理是最新的。

我希望它对你有用。

+0

哦,从两天我试图实现它。但没用。我是编程新手,我无法理解上面的github代码及其解释。它太长和复杂,有没有其他方式可以添加搜索功能 – NicolasWRefn

+0

这是我给你的唯一最简单的例子... –