Android notifydatasetchanged is not updating?
view does not get updated. Where can i add notifydatasetchanged(); I think this is what i need so the listview gets updated
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 aPackag开发者_StackOverflow社区eManager = 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);
}
});
}
}
You are never setting the adapter as mlistview is always null from what I see in your code. Plus why would you want to set the adapter again and again? You should refresh the data and call notifydatasetchanged
Edit : Could not see all the code on the phone , got to see all your code now : Here is what you need to do.
Have an adapterUpdate function in your adapter which takes in your mAppListAdapter as an argument uses it to display the data , basically replace the data the adapter has with what is passed to the adapterUpdate function , once that is done you can call notifyDatasetChanged in the adapter itself to update your listview.
精彩评论