Android:当我启动一项新活动时,Listview会自行复制,然后按回去返回它

前端之家收集整理的这篇文章主要介绍了Android:当我启动一项新活动时,Listview会自行复制,然后按回去返回它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个列表视图,它们是视图分页器选项卡中的片段.单击列表视图中的项目时,它将启动一个新活动.但是,当我按下后退按钮返回选项卡列表视图时,列表视图加倍,如果我打开活动并再次返回,它会再次翻倍,它会继续这样做.此外,我还有另一个带有五个选项卡的选项卡列表视图,当我从其中一个视图中选择两个选项卡时.当我回到它们时,该视图中的项目会加倍,这对于所有其他选项卡都是相同的.两个listviews的代码完全相同.我已经尝试过list.clear(),它只是清除了所有列表项,并且在视图持有者中使用了“else”语句(由其他堆栈溢出答案推荐).但每次列表视图仍然重复.除非你没有注意到,否则我对 android很新.

这是列表视图片段(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.

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    myhomeworkdue = new ArrayList<homeworkdue>();
    populatehomeworkdueList();
    populateListView();
    registerClickCallback();
}

您的第二个ListView中的相同问题,因为它们是相同的.

希望这对你有用.如果有效,请给我一个反馈.

原文链接:/android/310060.html

猜你在找的Android相关文章