简体   繁体   English

如何在带有图像的列表视图中的自定义适配器中添加搜索过滤器?

[英]How to add search filter in custom adapter in listview with images?

Sorry I am new in Java and I used the following code in MainActivity and Custom adapter after researching how listview is created.抱歉,我是 Java 新手,在研究了如何创建列表视图后,我在MainActivity自定义适配器中使用了以下代码。 I have tried so many ways to create searchView in actionbar by doing a lot of research, maybe is because I am new in Java and I lack understanding of the language.通过大量研究,我尝试了很多方法来在 actionbar 中创建 searchView,可能是因为我是 Java 新手并且我对该语言缺乏了解。

enter image description here在此处输入图片说明

MainActivity code主活动代码


public class MainActivity extends AppCompatActivity {

    ListView lv;
    Context context;

    public static int[] prgmImages = {
            R.drawable.ic_f"
    };

    public static String[] prgmNameList = {
            "Bakoena"
    };

    public static String[] liphoofolo = {
            "Ba ana koena."
    };

    public static String[] phoofolo = {
            "Koena ke phoofolo e lulang metsing e phelang ka ho ja nama ea liphoofolo"
    };

    @SuppressLint("CutPasteId")
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Make sure this is before calling super.onCreate
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
        Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_menu);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        //custom statusbar for each activity
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.RED);
        }*/

        context = this;

        lv = findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, prgmNameList, liphoofolo, phoofolo, prgmImages));
}
}

Custom Adapter code自定义适配器代码



public class CustomAdapter extends BaseAdapter{

    String [] result;
    String [] results;
    String [] resultss;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, String[] liphoofolo, String[] phoofolo, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        results=liphoofolo;
        resultss=phoofolo;
        context=mainActivity;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static class Holder
    {
        TextView tv;
        TextView ts;
        TextView tss;
        ImageView img;
    }
    @SuppressLint({"ViewHolder", "InflateParams"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder= new Holder();
        View rowView;

        rowView = inflater.inflate(R.layout.program_list, null);
        holder.tv= rowView.findViewById(R.id.textView1);
        holder.ts= rowView.findViewById(R.id.textView2);
        holder.tss= rowView.findViewById(R.id.textView3);
        holder.img= rowView.findViewById(R.id.imageView1);
        holder.tv.setText(result[position]);
        holder.ts.setText(results[position]);
        holder.tss.setText(resultss[position]);
        holder.img.setImageResource(imageId[position]);

        return rowView;
    }


You need to create a new android resource directory called menu resource type menu in the res folder (lef click on res folder > new > android resource directory).您需要在 res 文件夹中创建一个名为menu resource type menu的新 android 资源目录(左键单击 res 文件夹 > 新建 > android 资源目录)。 Inside create menu resource file called menu_bar (again lef click on menu folder > new > menu resource file) in the file inside menu tag add the falling code:在菜单标签内的文件中创建名为menu_bar菜单资源文件(再次menu_bar击菜单文件夹>新建>菜单资源文件)添加下降代码:

 <item
    android:id="@+id/app_bar_search"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="Search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="always" />

Then in main activity add:然后在主要活动中添加:

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_bar, menu);

        MenuItem item = menu.findItem(R.id.app_bar_search);
        SearchView searchView = (SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
    return true;
}

or https://stackoverflow.com/a/41868020/7735363https://stackoverflow.com/a/41868020/7735363

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM