2015-12-08 109 views
0

嘿,我正在工作的應用程序,其中包含listview在其中安裝的應用程序填充一些按鈕,如卸載。刷新從這裏刪除項目的列表視圖

public class MainActivity extends ListActivity { 
PackageManager packageManager; 
List<ApplicationInfo> applist; 
Listadapter listadapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    packageManager = getPackageManager(); 

    new LoadApplications().execute(); 

} 

@Override 
protected void onListItemClick(ListView l, View v, final int position, long id) { 
    super.onListItemClick(l, v, position, id); 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    dialogBuilder.setTitle("Choose option") 
      .setItems(R.array.options, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which) { 
         case 0: 
          ApplicationInfo app = applist.get(position); 
          try { 
           Intent intent = packageManager.getLaunchIntentForPackage(app.packageName); 

           if (intent != null) { 
            startActivity(intent); 
           } 
          } catch (ActivityNotFoundException e) { 
           Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
          } catch (Exception e) { 
           Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
          } 
          break; 
         case 1: 
          ApplicationInfo app1 = applist.get(position); 
          Uri packageURI = Uri.parse("package:" + app1.packageName); 
          Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); 
          startActivity(uninstallIntent); 
          applist.remove(listadapter.getItem(position)); 
          listadapter.notifyDataSetChanged(); 
          break; 
         case 2: 
          break; 
        } 
       } 
      }); 
    dialogBuilder.setCancelable(true) 
      .show(); 

} 

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) { 
    ArrayList<ApplicationInfo> applist = new ArrayList<>(); 

    for (ApplicationInfo info : list) { 
     try { 
      if (packageManager.getLaunchIntentForPackage(info.packageName) != null) { 
       applist.add(info); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return applist; 
} 

private class LoadApplications extends AsyncTask<Void, Void, Void> { 
    private ProgressDialog progress = null; 

    @Override 
    protected Void doInBackground(Void... params) { 
     applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)); 

     listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     setListAdapter(listadapter); 
     progress.dismiss(); 
     super.onPostExecute(aVoid); 

    } 

    @Override 
    protected void onPreExecute() { 
     progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,"); 
     super.onPreExecute(); 
    } 

} 

@Override 
protected void onPause() { 
    super.onPause(); 
     listadapter.notifyDataSetInvalidated(); 
} 

當我點擊卸載時,應用程序的位置在卸載應用程序之前首先被刪除。

這是strings.xml中

<resources> 
    <string name="app_name">AppSharer</string> 
    <string-array name="options"> 
     <item>Open App</item> 
     <item>uninstall</item> 
    </string-array> 
</resources> 

我應該怎麼刷新列表視圖一旦我刪除的項目。

謝謝!

回答

0

我從問題中得到的結果是,如果用戶點擊yes進行卸載,但從設置屏幕用戶回到應用程序而不卸載它,則必須有問題。在這種情況下,創建一個變量布爾值,如果用戶在卸載時單擊是,並在實例級別的字符串變量中保存應用包名稱,並且恢復類檢查方法,如果用戶布爾值爲true,則檢查定義的包是否存在,如果它不存在,請更新您的列表視圖。請參考這個,我還添加了註釋,通知您更改了哪些

public class MainActivity extends ListActivity { 
    PackageManager packageManager; 
    List<ApplicationInfo> applist; 
    Listadapter listadapter; 
    // add three variables 
    String packageName=""; 
    boolean isUnInstallClicked; 
    int mPosition; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     packageManager = getPackageManager(); 

     new LoadApplications().execute(); 

    } 

    @Override 
    protected void onListItemClick(ListView l, View v, final int position, long id) { 
     super.onListItemClick(l, v, position, id); 
      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
     dialogBuilder.setTitle("Choose option") 
       .setItems(R.array.options, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         switch (which) { 
          case 0: 
           ApplicationInfo app = applist.get(position); 
           try { 
            Intent intent = packageManager.getLaunchIntentForPackage(app.packageName); 

            if (intent != null) { 
             startActivity(intent); 
            } 
           } catch (ActivityNotFoundException e) { 
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
           } catch (Exception e) { 
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
           } 
           break; 
          case 1: 
           ApplicationInfo app1 = applist.get(position); 
           Uri packageURI = Uri.parse("package:" + app1.packageName); 
           Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); 
           startActivity(uninstallIntent); 
           //remove applist.remove and notify from here and add it to on resume 
           packageName=app1.packageName; 
           isUnInstallClicked=true; 
           mPosition=position; 

           break; 
          case 2: 
           break; 
         } 
        } 
       }); 
     dialogBuilder.setCancelable(true) 
       .show(); 

    } 
    @Override 
     protected void onResume() { 
      // TODO Auto-generated method stub 
      super.onResume(); 
      // here check for whether unistall was clicked and was app unistalled 
      if(isUnInstallClicked && !appInstalledOrNot(packageName)){ 
       //check whther app unistalled or not 

       applist.remove(listadapter.getItem(position)); 
       listadapter.notifyDataSetChanged(); 
       } 
      } 
     } 
    private boolean appInstalledOrNot(String uri) { 
     PackageManager pm = getPackageManager(); 
     boolean app_installed; 
     try { 
      pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
      app_installed = true; 
     } 
     catch (PackageManager.NameNotFoundException e) { 
      app_installed = false; 
     } 
     return app_installed; 
    } 
    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) { 
     ArrayList<ApplicationInfo> applist = new ArrayList<>(); 

     for (ApplicationInfo info : list) { 
      try { 
       if (packageManager.getLaunchIntentForPackage(info.packageName) != null) { 
        applist.add(info); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return applist; 
    } 

    private class LoadApplications extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog progress = null; 

     @Override 
     protected Void doInBackground(Void... params) { 
      applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)); 

      listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      setListAdapter(listadapter); 
      progress.dismiss(); 
      super.onPostExecute(aVoid); 

     } 

     @Override 
     protected void onPreExecute() { 
      progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,"); 
      super.onPreExecute(); 
     } 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
      listadapter.notifyDataSetInvalidated(); 
+0

兄弟居然卸載工作正常...但應用程序卸載即使我看到在我的列表視圖該應用......此我試過adapter.notifydatasetcange,但這是nt工作,或者我可能把它放在一些錯誤的地方... – Rahul

+0

可以請你給我一些代碼刷新listview上resume.thanx – Rahul

+0

在applist.remove(listadapter.getItem(位置))applsit是listview對象還是arraylist對象? –

相關問題