Android notifydatasetchanged没有更新?

时间:2011-06-28 06:22:04

标签: android listview

视图未更新。我在哪里可以添加notifydatasetchanged();我认为这就是我需要的,因此listview得到了更新

public class UninstallActivity extends Activity {
    private AppAdapter mAppListAdapter = null;
    private EditText mEditText = null;
    private ListView mListView = null;





    public void update() {
        // TODO

        mAppListAdapter.clear();


        Intent aIntent = new Intent(Intent.ACTION_MAIN, null);
        aIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        PackageManager aPackageManager = getPackageManager();
        List <ResolveInfo> aList = aPackageManager.queryIntentActivities(aIntent, PackageManager.GET_UNINSTALLED_PACKAGES);


        for( ResolveInfo rInfo : aList ) {
             if (!isSystemPackage(rInfo)) 
            mAppListAdapter.add(rInfo.activityInfo.applicationInfo);


            System.out.println("Installed Applications "  +  rInfo.activityInfo.applicationInfo.loadLabel(aPackageManager).toString()); 
        }




        if( mListView != null ) {
            mListView.setAdapter( mAppListAdapter );

           }

    } 






        private boolean isSystemPackage(ResolveInfo ri) {
        return ((ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;

        }

    public void remove( ApplicationInfo mApplicationInfo ) {

        Intent aIntent = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + mApplicationInfo.packageName));
        startActivity(aIntent);
     }






    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



        mEditText = (EditText) findViewById( R.id.EditText );
        mEditText.setSingleLine();
        mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        mEditText.addTextChangedListener( new TextWatcher() {

            @Override 
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if( s.length() > 0 ) {
                    // TODO
                    mAppListAdapter.clear();




                    Intent aIntent = new Intent(Intent.ACTION_MAIN, null);
                    aIntent.addCategory(Intent.CATEGORY_LAUNCHER);

                    PackageManager aPackageManager = getPackageManager();
                    List<ResolveInfo>aList = aPackageManager.queryIntentActivities(aIntent, PackageManager.GET_UNINSTALLED_PACKAGES);

                    for( ResolveInfo rInfo : aList ) {

                        String aName = rInfo.activityInfo.applicationInfo.loadLabel( aPackageManager ).toString().toLowerCase();
                        String aValue = s.toString().toLowerCase();
                        if (!isSystemPackage(rInfo))
                        if( aName.contains( aValue ) ) {
                            mAppListAdapter.add( rInfo.activityInfo.applicationInfo );

                        }
                    }

                    if( mListView != null ) {
                        mListView.setAdapter(mAppListAdapter);


                    }
                }
                else {
                    UninstallActivity.this.update();

                }
            }




            @Override 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override 
            public void afterTextChanged(Editable s) {
            }
        });

        mListView = (ListView) findViewById(android.R.id.list);
        mAppListAdapter = new AppAdapter(this);


        this.update();

        mListView.setOnItemClickListener( new OnItemClickListener() {
            @Override 
            public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
                ApplicationInfo mApplicationInfo = (ApplicationInfo) mAppListAdapter.getItem(position);
                UninstallActivity.this.remove(mApplicationInfo);





            }




        });
    }

}

1 个答案:

答案 0 :(得分:0)

您永远不会设置适配器,因为mlistview始终为null,这与我在您的代码中看到的一样。另外,为什么要一次又一次地设置适配器?您应刷新数据并调用notifydatasetchanged

编辑:无法看到手机上的所有代码,现在可以查看所有代码:这是您需要做的。

在你的适配器中有一个adapterUpdate函数,它接受你的mAppListAdapter作为参数使用它来显示数据,基本上用适配器的数据替换传递给adapterUpdate函数的数据,一旦完成,你可以调用notifyDatasetChanged in适配器本身用于更新列表视图。