user818610
user818610

Reputation: 3

Android Listview will not update after removing row

Ok so I am very new to android and I am just trying to figure out how things worked so I found this open source app and trying to learn it. Everything is working besides when you click on an app in the listview and press ok to uninstall. It will uninstall the app from the phone but the app icon and name is still in place in the listview. How can I get it so the listivew will remove it? Thank you

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 );
        }
    });
}


}

Upvotes: 0

Views: 1511

Answers (1)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

you should call UninstallerActivity.this.update(); again in in remove method,

but i prefer to use :

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

in remove method, this will not re-build the adapter, it will just refresh it, not like update method

Upvotes: 1

Related Questions