【问题标题】:How to add search filter in custom adapter in listview with images?如何在带有图像的列表视图中的自定义适配器中添加搜索过滤器?
【发布时间】:2021-07-28 18:52:00
【问题描述】:

对不起,我是 Java 新手,在研究了如何创建列表视图后,我在 MainActivity自定义适配器 中使用了以下代码。通过大量研究,我尝试了很多在操作栏中创建 searchView 的方法,可能是因为我是 Java 新手,对语言缺乏了解。

enter image description here

MainActivity 代码


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

自定义适配器代码



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


【问题讨论】:

    标签: java android


    【解决方案1】:

    您需要在 res 文件夹中创建一个名为 menu 资源类型 menu 的新 android 资源目录(单击 res 文件夹 > 新建 > android 资源目录)。在菜单标签内的文件中创建名为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" />
    

    然后在主要活动中添加:

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

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

    【讨论】:

    • 谢谢。 SearchView 显示正常,但无法过滤我的列表,如何使用上面的代码使其过滤包含图像的列表?
    • 对不起,我不知道如何整合这两个代码,上面是自定义适配器中的代码和您提供的链接中的代码。关键是一切正常,但问题是使用上面的自定义适配器在操作栏中实现工作 searchView,因为如果我更改它,我的代码将影响整个项目。
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多