2011-06-28 100 views
0

視圖不會更新。我在哪裏可以添加notifydatasetchanged();我想,這是我需要這樣的列表視圖得到更新Android notifydatasetchanged不更新?

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





      } 




     }); 
    } 

} 

回答

0

你永遠設置適配器mlistview總是從我在你的代碼看空。另外爲什麼你想要一次又一次地設置適配器?你應該刷新數據,並呼籲notifydatasetchanged

編輯:無法看到手機上的所有代碼,現在看到你所有的代碼:這是你需要做的。

在你的適配器中有一個adapterUpdate函數,它接受你的mAppListAdapter作爲參數,用它來顯示數據,基本上用傳遞給adapterUpdate函數的數據替換適配器所具有的數據,一旦完成,你可以調用notifyDatasetChanged在適配器本身來更新你的列表視圖。

+0

這就是我想知道我在哪裏設置和調用notifydatasetchanged? – user818610

相關問題