删除行后,Android Listview不会更新

时间:2011-07-25 20:40:01

标签: android listview

好的,所以我对Android很新,我只想弄清楚事情是如何运作的,所以我找到了这个开源应用程序并试图学习它。除了单击列表视图中的应用程序并按确定卸载之外,一切正常。它将从手机中卸载应用程序,但应用程序图标和名称仍在列表视图中。我怎样才能得到它以便listivew将它删除?谢谢

public class UninstallerActivity extends Activity {

private EditText                      mEditText                 = null;
private ListView                      mListView                 = null;
private AppListAdapter                mAppListAdapter           = 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.PERMISSION_GRANTED);

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




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

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

public void remove( ApplicationInfo mApplicationInfo ) {
    // TODO
    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);

    mEditText = (EditText) findViewById( R.id.EditText );
    mEditText.setSingleLine();
    mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);

    mEditText.addTextChangedListener( new TextWatcher() {
        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.PERMISSION_GRANTED);

                for( ResolveInfo rInfo : aList ) {
                    String aName = rInfo.activityInfo.applicationInfo.loadLabel( aPackageManager ).toString().toLowerCase();
                    String aValue = s.toString().toLowerCase();

                    if( aName.contains( aValue ) ) {
                        mAppListAdapter.add( rInfo.activityInfo.applicationInfo );
                    }
                }

                if( mListView != null ) {
                    mListView.setAdapter( mAppListAdapter );
                }
            }
            else {
                UninstallerActivity.this.update();
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });

    mListView = (ListView) findViewById( R.id.ListView );
    mAppListAdapter = new AppListAdapter( this );

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


    this.update();

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


}

1 个答案:

答案 0 :(得分:1)

您应该使用UninstallerActivity.this.update();方法再次致电remove

但我更喜欢使用:

mAppListAdapter.remove(mApplicationInfo );
mAppListAdapter.notifyDataSetChanged();

remove方法中,这不会重新构建适配器,它只会刷新它,而不是像update方法