java - How to customize an adapter in Android? -


i making simple android app takes name, email , contact number of user , saves these (including id) in phone's memory (via sqlite). "show all" page displays details of user in database in listview using custom adapter. runs this:

the "show all" page

it leaves these spaces in between rows. how remove these spaces? also, details mixed while retrieving database. first row displayed in correct format (as wanted) . second's details got mixed up. how correct this?

mydbhandler.java

public class mydbhandler extends sqliteopenhelper{      private static final int database_version = 1;     private static final string database_name= "user.db";     public static final string table_name = "data";     public static final string column_id = "id";     public static final string column_name = "name";     public static final string column_email = "email";     public static final string column_phno = "phno";      public mydbhandler(context context, string name, sqlitedatabase.cursorfactory factory, int version) {         super(context, database_name, factory, database_version);     }      @override     public void oncreate(sqlitedatabase db) {          string query = "create table "+table_name+"("+                 column_id + " integer primary key autoincrement,"+                 column_name + " varchar(20),"+                 column_email + " varchar(20),"+                 column_phno + " varchar(20)"+                 ");";          db.execsql(query);      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {           db.execsql("drop table if exists " + table_name);         oncreate(db);     }      public void adddata(data d){         contentvalues values = new contentvalues();         //values.put(column_id, d.getid());         values.put(column_name, d.getname());         values.put(column_email, d.getemail());         values.put(column_phno, d.getphno());          sqlitedatabase db = getwritabledatabase();         db.insert(table_name, null, values);         db.close();     }      public arraylist<string> retrievedata(){          arraylist<string> al = new arraylist<>();          sqlitedatabase db = getwritabledatabase();         string query = "select * "+table_name+";";          cursor c = db.rawquery(query,null);          c.movetofirst();          while(!c.isafterlast()){             if(c.getstring(c.getcolumnindex("id"))!=null){                  al.add(c.getstring(c.getcolumnindex("id")));                 al.add(c.getstring(c.getcolumnindex("name")));                 al.add(c.getstring(c.getcolumnindex("email")));                 al.add(c.getstring(c.getcolumnindex("phno")));              }             c.movetonext();         }         db.close();         return al;      }  } 

customadapter.java

public class customadapter extends arrayadapter<string>{       arraylist<string>a = new arraylist<>();       public customadapter(context context,   arraylist<string>a ){         super(context,r.layout.custom_row,a);     }      @override     public view getview(int position, view convertview, viewgroup parent) {          layoutinflater harshitsinflator = layoutinflater.from(getcontext());         view customview = harshitsinflator.inflate(r.layout.custom_row, parent, false);          textview textview3 = (textview) customview.findviewbyid(r.id.textview3);         textview textview4 = (textview) customview.findviewbyid(r.id.textview4);         textview textview5 = (textview) customview.findviewbyid(r.id.textview5);         button button4 = (button) customview.findviewbyid(r.id.button4);          string sid = getitem(position);          if(position%4==0 && position>0)         {             textview3.settext(a.get(1));             textview4.settext(a.get(2));             textview5.settext(a.get(3));             button4.settext(a.get(0));             a.clear();             customview.setvisibility(view.visible);         }         else {             a.add(sid);             customview.setvisibility(view.gone);         }         return customview;     } } 

listview.java

public class listview extends activity {      protected void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.list_view_layout);          arraylist<string> n;           mydbhandler db = new mydbhandler(this, null, null, 1);          n=db.retrievedata();          listadapter harshitsadapter = new customadapter(this, n);          listview harshitslistview = (listview) findviewbyid(r.id.harshitslistview);         harshitslistview.setadapter(harshitsadapter);         //((baseadapter)harshitsadapter).notifydatasetchanged();         }      public void backtomain(view view){          intent = new intent(this, mainactivity.class);         startactivity(i);         finish();     } } 

this app shows arrayindexoutofbounds exception:

process: com.example.h8pathak.dilliheart, pid: 21258     java.lang.indexoutofboundsexception: invalid index 3, size 3             @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255)             @ java.util.arraylist.get(arraylist.java:308) 

how rectify one?

modify these 2 files of yours:

customadapter.java

public class customadapter extends arrayadapter<string>{      arraylist<string> id, name, email, phno;     context c;       public customadapter(context context,   arraylist<string>id, arraylist<string>name , arraylist<string>email , arraylist<string> phno ){         super(context,r.layout.custom_row, id);          this.c=context;         this.id = id;         this.name=name;         this.email=email;         this.phno=phno;      }      @override     public view getview(int position, view convertview, viewgroup parent) {          layoutinflater harshitsinflator = layoutinflater.from(getcontext());         view customview = harshitsinflator.inflate(r.layout.custom_row, parent, false);          textview textview3 = (textview) customview.findviewbyid(r.id.textview3);         textview textview4 = (textview) customview.findviewbyid(r.id.textview4);         textview textview5 = (textview) customview.findviewbyid(r.id.textview5);         button button4 = (button) customview.findviewbyid(r.id.button4);               string sid = id.get(position);             string sname = name.get(position );             string semail = email.get(position );             string sphno = phno.get(position);              textview3.settext(sname);             textview4.settext(semail);             textview5.settext(sphno);             button4.settext(sid);          return customview;     } } 

listview.java

public class listview extends activity {      protected void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.list_view_layout);          arraylist<string> n;         arraylist<string> id  = new arraylist<>();         arraylist<string> name  = new arraylist<>();         arraylist<string> email  = new arraylist<>();         arraylist<string> phno  = new arraylist<>();          mydbhandler db = new mydbhandler(this, null, null, 1);          n=db.retrievedata();          for(int =0; i<n.size();i++){             if(i%4==0)                 id.add(n.get(i));             else if (i%4==1)                 name.add(n.get(i));             else if (i%4==2)                 email.add(n.get(i));             else                 phno.add(n.get(i));         }          system.out.println(n);           listadapter harshitsadapter = new customadapter(this, id, name, email, phno);          listview harshitslistview = (listview) findviewbyid(r.id.harshitslistview);         harshitslistview.setadapter(harshitsadapter);         }      public void backtomain(view view){          intent = new intent(this, mainactivity.class);         startactivity(i);         finish();     }      } 

adapting method not lead "arrayindexoutofbounds" exception


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -