简体   繁体   中英

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. 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.

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). 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:

 <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/7735363

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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