当屏幕方向改变时,ListView 在列表中添加了重复项目

发布于 2024-12-27 23:04:06 字数 3671 浏览 2 评论 0原文

我尝试创建列表视图,当方向改变时我遇到一些问题。 问题是:当我更改屏幕列表视图的方向时,在列表中添加重复的列表项。如何限制此数据更改

代码是:

   public class DayPlannerActivity extends Activity {

    private TextView txtHeader;
    private Context mContext;
    private ListView lvDayplanner;
    private DayPlannerAdapter adapter;
    private Activity activity;
    private static Vector<DayPlanner> list = new Vector<DayPlanner>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dayplanner);
        mContext = this;    
        activity = this;
        txtHeader = (TextView) findViewById(R.id.txtHeader);
        txtHeader.setText(R.string.haivlate);
        String[] Checks = {"select","Check1","Check2"};
        DayPlanner dp = new  DayPlanner("11:00 PM", Checks);
        list.add(dp);
        dp = new  DayPlanner("12:00 PM", Checks);
        list.add(dp);
        lvDayplanner = (ListView) findViewById(R.id.lvDayplanner);
        adapter= new DayPlannerAdapter(activity,list);
        lvDayplanner.setAdapter(adapter);
   }

}

List Adapter:

   public class DayPlannerAdapter extends BaseAdapter {

    private Activity mActivity;
    private static Vector<DayPlanner> list;
    private static LayoutInflater inflater;
    public DayPlannerAdapter ( Activity _activity,Vector<DayPlanner> _list) {
        mActivity = _activity;
        list = _list;
        inflater = (LayoutInflater)mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder{
        public TextView txtScheduledTime;
        public Spinner spnrChecks;
        public Button btnGo;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

         View vi=convertView;
            ViewHolder holder;
            if(convertView==null){
                vi = inflater.inflate(R.layout.dayplanner_listitem, null);
                holder=new ViewHolder();
                holder.txtScheduledTime=(TextView)vi.findViewById(R.id.txtScheduledTime);
                holder.spnrChecks = (Spinner) vi.findViewById(R.id.spnrChecks);
                holder.btnGo = (Button) vi.findViewById(R.id.btnGo);
                vi.setTag(holder);
            }
            else
              holder=(ViewHolder)vi.getTag();

            holder.txtScheduledTime.setText(list.get(position).getScheduledTime());
            ArrayAdapter<String> spnrAdapter=new ArrayAdapter<String>(mActivity,
                    android.R.layout.simple_spinner_item, list.get(position).getChecks());
            spnrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
            holder.spnrChecks.setAdapter(spnrAdapter);

            holder.btnGo.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     Intent i = new Intent();
                     i.setClass(mActivity,DayPlannerFormActivity.class);
                     mActivity.startActivity(i);
                }
            });

        return vi;
    }

}

I trying to create List View i face some problem when orientation changes.
Problem is: when i changes orientation of screen list-view add duplicate list item in list. how to restrict this data change

Code Is:

   public class DayPlannerActivity extends Activity {

    private TextView txtHeader;
    private Context mContext;
    private ListView lvDayplanner;
    private DayPlannerAdapter adapter;
    private Activity activity;
    private static Vector<DayPlanner> list = new Vector<DayPlanner>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dayplanner);
        mContext = this;    
        activity = this;
        txtHeader = (TextView) findViewById(R.id.txtHeader);
        txtHeader.setText(R.string.haivlate);
        String[] Checks = {"select","Check1","Check2"};
        DayPlanner dp = new  DayPlanner("11:00 PM", Checks);
        list.add(dp);
        dp = new  DayPlanner("12:00 PM", Checks);
        list.add(dp);
        lvDayplanner = (ListView) findViewById(R.id.lvDayplanner);
        adapter= new DayPlannerAdapter(activity,list);
        lvDayplanner.setAdapter(adapter);
   }

}

List Adapter :

   public class DayPlannerAdapter extends BaseAdapter {

    private Activity mActivity;
    private static Vector<DayPlanner> list;
    private static LayoutInflater inflater;
    public DayPlannerAdapter ( Activity _activity,Vector<DayPlanner> _list) {
        mActivity = _activity;
        list = _list;
        inflater = (LayoutInflater)mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder{
        public TextView txtScheduledTime;
        public Spinner spnrChecks;
        public Button btnGo;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

         View vi=convertView;
            ViewHolder holder;
            if(convertView==null){
                vi = inflater.inflate(R.layout.dayplanner_listitem, null);
                holder=new ViewHolder();
                holder.txtScheduledTime=(TextView)vi.findViewById(R.id.txtScheduledTime);
                holder.spnrChecks = (Spinner) vi.findViewById(R.id.spnrChecks);
                holder.btnGo = (Button) vi.findViewById(R.id.btnGo);
                vi.setTag(holder);
            }
            else
              holder=(ViewHolder)vi.getTag();

            holder.txtScheduledTime.setText(list.get(position).getScheduledTime());
            ArrayAdapter<String> spnrAdapter=new ArrayAdapter<String>(mActivity,
                    android.R.layout.simple_spinner_item, list.get(position).getChecks());
            spnrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
            holder.spnrChecks.setAdapter(spnrAdapter);

            holder.btnGo.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     Intent i = new Intent();
                     i.setClass(mActivity,DayPlannerFormActivity.class);
                     mActivity.startActivity(i);
                }
            });

        return vi;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮酒 2025-01-03 23:04:06

由于 Android 会在方向更改时自动保存 View 状态,因此您需要一种方法来知道这是否不是第一次调用 onCreate。幸运的是,这很简单:覆盖 onSaveInstanceState,并存储 1 个值,以使您在 onCreate 中获得的包不为空。

@Override
public void onSaveInstanceState(Bundle outInstanceState) {
    outInstanceState.putInt("value", 1);
}

然后,当重新创建 Activity 时,onCreate 中的参数 savedInstanceState为空。因此,只需进行测试:

if(savedInstanceState != null)

在将数据添加到视图之前。

Since Android automatically saves Views states when the orientation changes, you need a way to know if it's not the first call to onCreate. Luckily, it's easy: Override onSaveInstanceState, and store even 1 value to make the bundle your get in onCreate not-null.

@Override
public void onSaveInstanceState(Bundle outInstanceState) {
    outInstanceState.putInt("value", 1);
}

Then, when the activity is recreated, the parameter savedInstanceState in onCreate will not be null. So just do the test:

if(savedInstanceState != null)

Before you add data to your views.

書生途 2025-01-03 23:04:06

这是因为您的 dayplanner 对象列表是静态的,因此当您更改视图的方向时,它会重新创建活动,但由于在 java 中不会重新创建静态对象,而是为该类型保存,因此它使列表有两个相同的对象。

its because your list of dayplanner objects is static, so when you change the orientation of the view it recreates the activity but since in java a static object is not recreated, but saved for that type, it makes the list have two of the same.

粉红×色少女 2025-01-03 23:04:06

这里的聚会太晚了,仍然会回答,因为它可能对其他人有用。我也遇到了同样的问题,通过正确实现视图持有者设计模式解决了这个问题。

Way too late the party here,will still answer as it might be useful to someone else. I was also having the same issue it was resolved by PROPERLY implementing the view holder design pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文