listview的子项不能一直删除,当然删除到只有7项的时候就会报数组溢出的问题。
求解答!以下是代码
packagecom.example.ex_listview;
importjava.util.arraylist;
importandroid.os.bundle;
importandroid.app.activity;
importandroid.view.menu;
importandroid.view.view;
importandroid.view.view.onclicklistener;
importandroid.view.viewgroup;
importandroid.widget.baseadapter;
importandroid.widget.button;
importandroid.widget.imageview;
importandroid.widget.listview;
importandroid.widget.relativelayout;
importandroid.widget.textview;
publicclassmainactivityextendsactivityimplementsonclicklistener{
int[]imgs=newint[]{r.drawable.cup,r.drawable.date,
r.drawable.drawer,r.drawable.folder,r.drawable.gear,
r.drawable.globe,r.drawable.heart,r.drawable.help,
r.drawable.inbox,r.drawable.info,r.drawable.leaf,
r.drawable.lock,r.drawable.mail,r.drawable.moleskine,
r.drawable.monitor,r.drawable.music,r.drawable.notebook,
r.drawable.pill,r.drawable.present,r.drawable.print};
arraylist<dataencap>list=newarraylist<dataencap>();
privatemybaseadaptermba;
@override
protectedvoidoncreate(bundlesavedinstancestate){
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
settitle("消息");
buttonbtndelete=(button)findviewbyid(r.id.button1);
buttonbtnadd=(button)findviewbyid(r.id.button2);
btndelete.setonclicklistener(this);
btnadd.setonclicklistener(this);
listviewlistview=(listview)findviewbyid(r.id.listview1);
mba=newmybaseadapter();
listview.setadapter(mba);
for(inti=0;i<imgs.length;i++){
dataencapdata=newdataencap(imgs[i],"昵称"+i,"已聊过的消息内容"+i,"20:"+i);
list.add(data);
}
publicvoiddelete(){
if(list.size()>0){
list.remove(0);
mba.notifydatasetchanged();
publicvoidadd(dataencapdata){
classmybaseadapterextendsbaseadapter{
publicintgetcount(){
returnimgs.length;
publicviewgetview(intposition,viewconvertview,viewgrouparg2){
relativelayoutlayout=null;
viewholderholder=null;
if(convertview==null){
layout=(relativelayout)getlayoutinflater().inflate(r.layout.listview_item,null);
holder=newviewholder();
holder.p_w_picpath=(imageview)layout.findviewbyid(r.id.p_w_picpathview1);
holder.tv_name=(textview)layout.findviewbyid(r.id.textview1);
holder.tv_text=(textview)layout.findviewbyid(r.id.textview2);
holder.tv_time=(textview)layout.findviewbyid(r.id.textview3);
layout.settag(holder);
}else{
layout=(relativelayout)convertview;
holder=(viewholder)layout.gettag();
dataencapdata=list.get(position);
holder.p_w_picpath.setimageresource(data.img);
holder.tv_text.settext(data.text);
holder.tv_name.settext(data.name);
holder.tv_time.settext(data.time);
returnlayout;
publicobjectgetitem(intposition){
returnnull;
publiclonggetitemid(intposition){
return0;
classdataencap{
intimg;
stringname;
stringtext;
stringtime;
publicdataencap(intimg,stringname,stringtext,stringtime){
this.img=img;
this.name=name;
this.text=text;
this.time=time;
classviewholder{
imageviewp_w_picpath;
textviewtv_name;
textviewtv_text;
textviewtv_time;
publicbooleanoncreateoptionsmenu(menumenu){
getmenuinflater().inflate(r.menu.main,menu);
returntrue;
publicvoidonclick(viewv){
switch(v.getid()){
caser.id.button1:
delete();
break;
caser.id.button2:
dataencapdata=newdataencap(r.drawable.ic_launcher,"昵称","已聊过的消息内容","20:20");
add(data);
default: