android - How to select only part of a listview based on different requirements by the user? -


i trying develop android mobile application whereby required display part of listviews has @ least 100 items. i,being admin of college,i has listed 100 subjects taught.(all listed in listview.all of subjects have point , in order strudent apply course he/she need have exact of higher points.an indicative sample below: english:24 spanish:16 maths:28 science:26 french:16 management:22 law:30 asian language:10

now student needs enter his/her point , based(exact or lower point)on that, he/she list of subjects he/she eligible apply for.

e.g enter points:24

the output in listview should give following: french:16 management:22 english:24 asian language:10

i tried stuck , not complete codes:

course.java  public class course {  private string name; private int points;  public course(string name, int points) {     this.name = name;     this.points = points; }  public string getname() {     return name; }  public int getpoints() {     return points; }  @override public string tostring() {     return getname(); } }  course[] courses = new course[]{     new course("medicine", 30),     new course("physics", 28),     new course("math", 24),     new course("english", 20)  };     button searchbutton = (button) findviewbyid(r.id.btn_search); searchbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { list<course> courselist = new arraylist<course>(arrays.aslist(courses)); // initialize adapter courselist... //  // code inside button's onclick                     int points = integer.parseint(edittext.gettext().tostring()); (course c : courses) { if (c.getpoints() <= points) {     adapter.add(c); } }     course.xml  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"           android:layout_width="match_parent"           android:layout_height="match_parent"           android:orientation="vertical">  <linearlayout android:layout_width="match_parent"               android:layout_height="wrap_content"               android:orientation="horizontal">     <edittext             android:layout_width="0dp"             android:layout_weight="1"             android:maxlines="1"             android:inputtype="number"             android:layout_height="wrap_content"             android:hint="enter points:"             android:id="@+id/edittext"/>     <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="search"             android:id="@+id/btn_search"/> </linearlayout>  <listview         android:id="@+id/coursenames"         android:layout_width="match_parent"         android:layout_height="wrap_content"/> </linearlayout> 

my solution below -

  1. mainactivity

    public class mainactivity extends appcompatactivity {  listview mylist; edittext edtsearch; button btnsearch;  listadapter adapter; arraylist<course> alsearchcourses; course[] courses;  @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main);  mylist = (listview) findviewbyid(r.id.mylist); edtsearch = (edittext) findviewbyid(r.id.edtsearch); btnsearch = (button) findviewbyid(r.id.btnsearch);  courses = new course[]{         new course("medicine", 30),         new course("physics", 28),         new course("math", 24),         new course("english", 20) };  adapter = new listadapter(this, courses); mylist.setadapter(adapter);  edtsearch.addtextchangedlistener(new textwatcher() {     @override     public void beforetextchanged(charsequence s, int start, int count, int after) {     }      @override     public void ontextchanged(charsequence s, int start, int before, int count) {         if (count == 0) {             adapter = new listadapter(mainactivity.this, courses);             mylist.setadapter(adapter);         }     }      @override     public void aftertextchanged(editable s) {     } });  btnsearch.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {          alsearchcourses = new arraylist<>();          int points = integer.parseint(edtsearch.gettext().tostring());         (int = 0; < courses.length; i++) {              course c2 = courses[i];              if (c2.getpoints() <= points) {                 alsearchcourses.add(c2);             }          }          course searchcourses [] = new course[alsearchcourses.size()];         searchcourses = alsearchcourses.toarray(searchcourses);          adapter = new listadapter(mainactivity.this, searchcourses);         mylist.setadapter(adapter);      }  });  } } 
  2. listadapter

    public class listadapter extends baseadapter {  context context; course[] courses; layoutinflater inflater;  public listadapter(context context, course[] courses) { this.context = context; this.courses = courses; inflater = layoutinflater.from(context); }  @override public int getcount() { return courses.length; }  @override public object getitem(int position) { return null; }  @override public long getitemid(int position) { return 0; }  @override public view getview(int position, view convertview, viewgroup parent) {  if(convertview == null){     convertview = inflater.inflate(r.layout.list_item_layout, null); }  textview txtcourse = (textview) convertview.findviewbyid(r.id.txtcourse); txtcourse.settext(courses[position].getname() + "-" +     courses[position].getpoints());  return convertview; } } 

hope helps :)


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -