简体   繁体   English

ListView最后一个条目重复单击后退

[英]ListView last entry repeat on back click

Why Listview repeats last entry on back click??????First time when that class launched with ListView, all the data It is showing is correct, now I click on one of the list Item and again press back from description, then It repeats the last entry. 为什么Listview在重复单击时重复最后一次输入???????首次使用ListView启动该类时,所有数据都显示正确,现在我单击列表项之一,然后再次从描述中按返回,然后重复最后一个条目。

eg 例如

ListItems are: A -> B -> C ListItems是:A-> B-> C

Now I click on B and go to the next page and from that I click on back, then the list is: 现在,我单击B并转到下一页,然后从该页面单击Back,则列表为:

A -> B -> C -> C A-> B-> C-> C

and again when I press A and from that A description page, when I click back, then the list is: 再按一次A,然后从该描述页面中,当我单击返回时,列表为:

A -> B -> C -> C -> C A-> B-> C-> C-> C

why it is repeating the last entry???? 为什么要重复最后一个条目????

The activity class code is as follow: 活动类代码如下:

         if(list.isEmpty())
                            {
                                if(adapter==null)
                                {
                                        adapter=new MyAdapter(context, list);
                                        ls.setAdapter(adapter);
                                }
                                adapter.notifyDataSetChanged();
                            }

     @Override
protected void onStop() {
    super.onStop();
   AppointmentListView.setVisibility(View.GONE);
}

@Override
protected void onRestart() {
    super.onRestart();
    AppointmentListView.setVisibility(View.VISIBLE);
} 

and MyAdapter class is: 和MyAdapter类是:

public class MyAdapter extends BaseAdapter{

        public ArrayList<HashMap<String,String>> list;  
        Context context; 
        private LayoutInflater mInflater;
        String appointType;


        public MyAdapter(Context context, ArrayList<HashMap<String,String>> list)
        {  
            super();  
              this.context=context;
            this.list = list;  
            mInflater = LayoutInflater.from(context);

        } 

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

        }



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

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

        static class ViewHolder {  
            TextView AppTime_List;  
            TextView CustomerName_List;  
            TextView CustomerAddress_List;  
            TextView AppID_List;
            Button MapButton;
            }


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

            ViewHolder holder;  

            if (convertView == null)  
            {  
                holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.listviewsample, parent, false);  

            holder.AppTime_List = (TextView) convertView.findViewById(R.id.time);
            holder.CustomerName_List = (TextView) convertView.findViewById(R.id.name);
            holder.CustomerAddress_List = (TextView) convertView.findViewById(R.id.Address);
               convertView.setTag(holder);

            }  
            else  
            {  
            holder = (ViewHolder) convertView.getTag();

            }  

            appointType=(String)list.get(position).get("Name");

            holder.AppID_List.setText((String)list.get(position).get("ID"));
            holder.AppTime_List.setText((String)list.get(position).get("Time"));
            holder.CustomerName_List.setText((String)list.get(position).get("Name"));
            holder.CustomerAddress_List.setText((String)list.get(position).get("Address"));



            return convertView;  

        }


    }

Can anybody pls help me on that?? 有人可以帮我吗?

here the problem is with MyAdapter getCount method. 这里的问题是MyAdapter的getCount方法。 now in my MyAdapter class, I have declare list_size at the top: 现在在MyAdapter类中,我在顶部声明了list_size:

 int list_size;

  public MyAdapter(Context context, ArrayList<HashMap<String,String>> list)
{  
    super();  
      this.context=context;
    this.list = list;  
    mInflater = LayoutInflater.from(context);
    list_size=list.size();

} 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM