如何清除我的机器人列表视图我的、视图、机器人、列表

2023-09-04 05:15:42 作者:待我长发及腰看我不勒死你

我已经列入我ListAdapter在我的员工class.And List1中包含Empname的值, 伊诺,工资在员工显示5条记录后,从webservices.Now进账 屏幕......当我点击Depatrment活动,回到员工...初始 5条记录添加到列表中,目前共有10条记录present等....这个过程是怎么回事像这样... 请帮我这样没有重复附加,它有刷新列表。

注:明确()或notifydatasetchanged(),无效()无法正常工作

  ListAdapter适配器=新SimpleAdapter(这一点,List1中,
R.layout.employee,新的String [] {EmpName,伊诺,工资},新的INT []
 {R.id.ename,R.id.eno,R.id.sal});

    setListAdapter(适配器);
列表视图LV = getListView();

    lv.setTextFilterEnabled(真正的);

    lv.setOnItemClickListener(新OnItemClickListener(){

        公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){
            POS =位置;
            registerForContextMenu(getListView());

        }

    });


    TextView的tvdept =(TextView中)findViewById(R.id.Department);
    tvdept.setOnClickListener(新View.OnClickListener(){

        公共无效的onClick(视图v){
        startActivity(新意图(Employee.this,Department.class));
        }
    });
 

解决方案

在没有看到更多的code,这是难以肯定,但我会大胆地猜测......

当你离开一个活动回来,该框架将尝试还原到你在哪里使用 savedInstanceState 捆绑。它使用它来重新创建您上次是该活动。这听起来像你已经设置了列表中的的onCreate 的方法,并没有检查一个 savedInstanceState 捆绑,所以当你回来的框架正在恢复您的列表,然后在活动进行到您的code和重新创建列表(在这种情况下,再次添加相同的数据)。

尝试包装你的列表创建code在如果这将检查 savedInstanceState 捆绑的存在

这样的:

  @覆盖
公共无效的onCreate(包savedInstanceState){
   super.onCreate(savedInstanceState);
   如果(savedInstanceState == NULL){
       //做你的列表的设置在这里
   }
 }
 
履带机器人完整设计

使用,如果你回来给活动和框架保存你的国家,它会简单地恢复它,而不是通过你的列表创建code运行。

我知道这并不能回答的实际问题,但要解决(在返回的活动列表复制数据)的根源问题。

I have included my ListAdapter in my EMPLOYEE class.And list1 contains the values of Empname, Eno,Salary fetched from webservices.Now after displaying the 5 records in the employee screen...when I click on Depatrment Activity and come back to Employee ... the initial 5 records are appended to the list and now 10 records are present and so on .... the process is going on like this... Please help me so as no duplicates are appended and it has to refresh the list.

Note : clear() or notifydatasetchanged(), invalidate() are not working.

ListAdapter adapter = new SimpleAdapter(this, list1,
R.layout.employee, new String[] { "EmpName","Eno", "Salary" }, new int[]
 {R.id.ename, R.id.eno,R.id.sal});

    setListAdapter(adapter);
Listview lv = getListView();

    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            pos = position;
            registerForContextMenu(getListView());

        }

    });


    TextView tvdept = (TextView) findViewById(R.id.Department); 
    tvdept.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        startActivity(new Intent(Employee.this, Department.class));
        }
    });

解决方案

Without seeing more code, it's difficult to be sure, but I'll hazard a guess...

When you leave an activity and come back, the framework tries to restore you to where you were using the savedInstanceState bundle. It uses this to re-create where you last were in that activity. It sounds like you have set up the list in the onCreate method and haven't checked for a savedInstanceState bundle, so when you come back to the activity the framework is restoring your list and then proceeds into your code and re-creates the list (in this case adding the same data again).

Try wrapping your list creation code in an if that checks for the existence of the savedInstanceState bundle.

Like this:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (savedInstanceState == null) {
       // do your list setup here
   }
 }

Using that, if you come back to the activity and the framework saved your state, it will simply restore it and not run through your list creation code.

I know this doesn't answer the actual question, but it should solve the root issue (duplicating list data on return to activity).