有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在recycleview列表视图中搜索

我正在创建我的第一个应用程序。我想向我的应用程序添加搜索功能。我在网上尝试了很多解决方案,但都没有达到预期的效果。应用程序具有回收列表视图。在应用程序中搜索后,它应该过滤单词,并显示具有相同单词的列表项。 这是我的主要活动

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;
}
}

这是一个单词。爪哇

    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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center_vertical"
安卓:orientation="horizontal"

安卓:minHeight="70dp"
>




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

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

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

        />




</LinearLayout>


共 (1) 个答案