这是列表视图片段(Due_Today_Fragment)的代码,它位于双标签活动中.
import java.util.ArrayList;import java.util.List;import androID.content.Intent;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class Due_Today_Fragment extends Fragment { private List<homeworkdue> myhomeworkdue; static class VIEwHolder { public ImageVIEw imageVIEw; public TextVIEw HomeworkDueText; public TextVIEw DescriptionText; public TextVIEw TeacherText; } public static final String KEY_HOMEWORK="homework"; public static final String KEY_DESC="desc"; public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState){ return inflater.inflate(R.layout.activity_main,container,false); } @OverrIDe public voID onResume() { // Todo auto-generated method stub super.onResume(); myhomeworkdue = new ArrayList<homeworkdue>(); populatehomeworkdueList(); populateListVIEw(); registerClickCallback(); } private voID registerClickCallback() { ListVIEw List = (ListVIEw)getVIEw().findVIEwByID(R.ID.ListVIEw1); List.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEwClicked,int position,long ID) { homeworkdue clickedhomeworkdue = myhomeworkdue.get(position); Intent intent = new Intent(getActivity(),homeworkdueDetailsActivity.class); intent.putExtra(KEY_HOMEWORK,clickedhomeworkdue.getHomeworkdue()); intent.putExtra(KEY_DESC,clickedhomeworkdue.getDesciption()); startActivity(intent); } }); } private voID populatehomeworkdueList() { myhomeworkdue.add(new homeworkdue("History Homework","Description 1",R.drawable.global_studIEs,"Anderson")); myhomeworkdue.add(new homeworkdue("Math Homework","Description 2",R.drawable.mathematics,"Klein")); myhomeworkdue.add(new homeworkdue("English Homework","Description 3",R.drawable.english,"Reed")); myhomeworkdue.add(new homeworkdue("Spanish Homework","Description 4",R.drawable.spanish,"Joya")); myhomeworkdue.add(new homeworkdue("ScIEnce Homework","Description 5",R.drawable.scIEnce,"Poole")); } private voID populateListVIEw() { ArrayAdapter<homeworkdue> adapter = new Mylistadapter(); ListVIEw List = (ListVIEw)getVIEw().findVIEwByID(R.ID.ListVIEw1); List.setAdapter(adapter); } public class Mylistadapter extends ArrayAdapter<homeworkdue> { public Mylistadapter() { super(getActivity(),R.layout.item_vIEw,myhomeworkdue); } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder holder; if(convertVIEw==null){ convertVIEw = getActivity().getLayoutInflater().inflate(R.layout.item_vIEw,parent,false); holder = new VIEwHolder(); holder.imageVIEw = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.item_iconclass); holder.HomeworkDueText = (TextVIEw) convertVIEw.findVIEwByID(R.ID.item_texthomeworkdue); holder.DescriptionText = (TextVIEw) convertVIEw.findVIEwByID(R.ID.item_textdescription); holder.TeacherText = (TextVIEw) convertVIEw.findVIEwByID(R.ID.item_textteacher); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } homeworkdue currenthomeworkdue = myhomeworkdue.get(position); holder.imageVIEw.setimageResource(currenthomeworkdue.getIconID()); holder.HomeworkDueText.setText(currenthomeworkdue.getHomeworkdue()); holder.DescriptionText.setText(currenthomeworkdue.getDesciption()); holder.TeacherText.setText(currenthomeworkdue.getTeacher()); return convertVIEw; } }}
以下是单击列表视图项时启动的活动的代码
import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.Menu;import androID.Widget.TextVIEw;import static com.bernard.beaconportal.Due_Today_Fragment.KEY_HOMEWORK;import static com.bernard.beaconportal.Due_Today_Fragment.KEY_DESC;public class homeworkdueDetailsActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_details); String homework = ""; String desc = ""; Intent intent = getIntent(); if (null != intent) { homework = intent.getStringExtra(KEY_HOMEWORK); desc = intent.getStringExtra(KEY_DESC); } TextVIEw headlineTxt = (TextVIEw) findVIEwByID(R.ID.texthomeworkdue); headlineTxt.setText(homework); TextVIEw pubdateTxt = (TextVIEw) findVIEwByID(R.ID.textdescription); pubdateTxt.setText(desc); }}
这是tabs片段的代码
import java.lang.reflect.FIEld;import com.actionbarsherlock.app.SherlockFragment;import com.astuetz.PagerSlIDingTabStrip;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.support.v4.vIEw.VIEwPager;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class Fragments2 extends SherlockFragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.vIEwpager_main,false); // Locate the VIEwPager in vIEwpager_main.xml VIEwPager pager = (VIEwPager) vIEw.findVIEwByID(R.ID.vIEwPager); // Set the VIEwPagerAdapter into VIEwPager pager.setAdapter(new VIEwPagerAdapter(getChildFragmentManager())); PagerSlIDingTabStrip tabs = (PagerSlIDingTabStrip) vIEw.findVIEwByID(R.ID.pagerTabStrip); tabs.setVIEwPager(pager); return vIEw; } @OverrIDe public voID onDetach() { super.onDetach(); try { FIEld childFragmentManager = Fragment.class .getDeclaredFIEld("mChildFragmentManager"); childFragmentManager.setAccessible(true); childFragmentManager.set(this,null); } catch (NoSuchFIEldException e) { throw new RuntimeException(e); } catch (illegalaccessexception e) { throw new RuntimeException(e); } }}
这里是为家庭作业定义字符串的地方
public class homeworkdue { private String homework; private String desc; private int IconID; private String teacher; public homeworkdue(String homework,String desc,int IconID,String teacher) { super(); this.homework = homework; this.desc = desc; this.IconID = IconID; this.teacher = teacher; } public String getHomeworkdue() { return homework; } public String getDesciption() { return desc; } public int getIconID() { return IconID; } public String getTeacher() { return teacher; } }
这是片段的代码,它包含在五个选项卡中
import java.util.ArrayList;import java.util.List;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class Thursday extends Fragment { public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState){ return inflater.inflate(R.layout.schedule_List_vIEw,false); } private List<schedule> myschedule = new ArrayList<schedule>(); static class VIEwHolder { public TextVIEw HomeworkDueText; } public voID onStart() { super.onStart(); populatescheduleList(); populateListVIEw(); } private voID populatescheduleList() { myschedule.add(new schedule("E Band")); myschedule.add(new schedule("G Band")); myschedule.add(new schedule("F Band")); myschedule.add(new schedule("H Band")); myschedule.add(new schedule("A Band")); myschedule.add(new schedule("C Band")); myschedule.add(new schedule("D Band")); } private voID populateListVIEw() { ArrayAdapter<schedule> adapter = new Mylistadapter(); ListVIEw List = (ListVIEw)getVIEw().findVIEwByID(R.ID.ListVIEw2); List.setAdapter(adapter); } public class Mylistadapter extends ArrayAdapter<schedule> { public Mylistadapter() { super(getActivity(),myschedule); } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { VIEwHolder holder; if(convertVIEw==null){ convertVIEw = getActivity().getLayoutInflater().inflate(R.layout.schedule_List_item,false); holder = new VIEwHolder(); holder.HomeworkDueText = (TextVIEw) convertVIEw.findVIEwByID(R.ID.bandText); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } schedule currenthomeworkdue = myschedule.get(position); holder.HomeworkDueText.setText(currenthomeworkdue.Band()); return convertVIEw; } }}
这里是为计划定义字符串的地方
public class schedule { private String band; public schedule(String band) { super(); this.band = band; } public String Band() { return band; }}
如果您需要更多信息,请提前询问并感谢您的帮助
我一直试图修复这个错误两天,我的智慧结束了.
解决方法 我们在创建片段时一次声明一个新List的原因,所以当你回到你的片段时它不会声明一个新片段,而是使用旧片段.所以为了解决这个问题,你需要将以下代码分成两部分:private List<homeworkdue> myhomeworkdue = new ArrayList<homeworkdue>();
部分在你的片段中进入onResume,你还需要删除onStart和onStart中的所有内容到onResume.但首先保持这样的声明:
private List<homeworkdue> myhomeworkdue;
并将其余部分移至onResume.
@OverrIDeprotected voID onResume() { // Todo auto-generated method stub super.onResume(); myhomeworkdue = new ArrayList<homeworkdue>(); populatehomeworkdueList(); populateListVIEw(); registerClickCallback();}
您的第二个ListVIEw中的相同问题,因为它们是相同的.
希望这对你有用.如果有效,请给我一个反馈.
总结以上是内存溢出为你收集整理的Android:当我启动一项新活动时,Listview会自行复制,然后按回去返回它全部内容,希望文章能够帮你解决Android:当我启动一项新活动时,Listview会自行复制,然后按回去返回它所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)