操作栏中的Android搜索框,用于listView数组适配器中的对象

时间:2018-09-12 06:19:50

标签: android listview android-arrayadapter search-box

我想在操作栏中插入一个自动完成的搜索框,有点像whatsapp中的那个,它可以从不同的活动中搜索列表视图中的对象。这些活动中有一些片段,但全部都包含列表视图,其中的对象包含单词,图像和音频。我曾尝试寻找解决方案,但我只为简单的listViews烦恼,而我却听不懂。仍然是android中的新功能。这是我的listItems活动代码段之一

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

    word.add(new Word("beat","rova",R.raw.rova));
    word.add(new Word("buy","tenga",R.raw.tenga));
    word.add(new Word("come","huya",R.raw.huya));
    word.add(new Word("begin","taga",R.raw.tanga));
    word.add(new Word("bite","ruma",R.raw.ruma));
    word.add(new Word("break","gura",R.raw.gura));
    word.add(new Word("burn","pisa",R.raw.pisa));
    word.add(new Word("catch","bata",R.raw.bata));

这是我的适配器类

public class WordAdapter extends ArrayAdapter<Word> {

private int mColorResourseId;

private static final String LOG_TAG = WordAdapter.class.getSimpleName();

public WordAdapter(Activity context, ArrayList<Word> word,int colorResourseId){
    super(context,0,word);
    mColorResourseId =  colorResourseId;
}



@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItemView = convertView;
    if(listItemView == null){
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_view,parent,false);
    }
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID version_name
    TextView shona = (TextView) listItemView.findViewById(R.id.shona);
    // Get the version name from the current Word object and
    // set this text on the name TextView
    shona.setText(currentWord.getShonaTranslation());

    // Find the TextView in the list_item.xml layout with the ID version_number
    TextView english = (TextView) listItemView.findViewById(R.id.english);
    // Get the version number from the current word object and
    // set this text on the number TextView
    english.setText(currentWord.getDefaultTranslation());

    // Find the ImageView in the list_item.xml layout with the ID list_item_icon
    ImageView image = (ImageView) listItemView.findViewById(R.id.image);
    if(currentWord.hasImage()){
        image.setImageResource(currentWord.getImageResourceId());
        image.setVisibility(View.VISIBLE);
    }else{
        image.setVisibility(View.GONE);
    }

    View linear = listItemView.findViewById(R.id.linear);
    int color = ContextCompat.getColor(getContext(), mColorResourseId);
    linear.setBackgroundColor(color);



    return listItemView;
}

如您所见,每个对象有两个单词,我希望用户使用第一个单词进行搜索,但要获取整个对象

0 个答案:

没有答案
相关问题