我正在创建我的第一个应用。我想在我的应用中添加搜索功能。我在互联网上尝试了许多解决方案,但它没有给出理想的结果。应用程序具有回收列表视图在应用程序中搜索后,它应该过滤单词,并应显示具有相同单词的列表项。 这是我的主要活动
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 :(得分:0)
这里我提供了一个标准的简单示例,它代表了带有搜索机制的Recycle Listview。
https://github.com/Wrdlbrnft/Searchable-RecyclerView-Demo
我在我的应用程序中应用了相同的内容,并且它可以更新。
我希望它对你有用。