2013-10-25 195 views
0

上下文菜單和崩潰我有很多問題,這個代碼非常緩慢滾動,通過ProgressDialog

ApplicationAdapter

import java.util.List; 

import android.content.Context; 
import android.content.pm.ApplicationInfo; 
import android.content.pm.PackageManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> { 
    private List<ApplicationInfo> appsList = null; 
    private Context context; 
    private PackageManager packageManager; 

    public ApplicationAdapter(Context context, int textViewResourceId, 
      List<ApplicationInfo> appsList) { 
     super(context, textViewResourceId, appsList); 
     this.context = context; 
     this.appsList = appsList; 
     packageManager = context.getPackageManager(); 
    } 

    @Override 
    public int getCount() { 
     return ((null != appsList) ? appsList.size() : 0); 
    } 

    @Override 
    public ApplicationInfo getItem(int position) { 
     return ((null != appsList) ? appsList.get(position) : null); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (null == view) { 
      LayoutInflater layoutInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layoutInflater.inflate(R.layout.snippet_list_row, null); 
     } 

     ApplicationInfo data = appsList.get(position); 
     if (null != data) { 
      TextView appName = (TextView) view.findViewById(R.id.app_name); 
      TextView packageName = (TextView) view.findViewById(R.id.app_paackage); 
      ImageView iconview = (ImageView) view.findViewById(R.id.app_icon); 

      appName.setText(data.loadLabel(packageManager)); 
      packageName.setText(data.packageName); 
      iconview.setImageDrawable(data.loadIcon(packageManager)); 
     } 
     return view; 
    } 
}; 

MainActivity

import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.pm.ApplicationInfo; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Adapter; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.Button; 
import android.widget.ListView; 

public class MainActivity extends ListActivity { 
    private PackageManager packageManager = null; 
    private List<ApplicationInfo> applist = null; 
    private ApplicationAdapter listadaptor = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_eclair); 

     packageManager = getPackageManager(); 


     new LoadApplications().execute(); 


    Button bottone1 = (Button)findViewById(R.id.button1); 
    bottone1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      new LoadApplications().execute(); 


     } 
    });}; 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     ApplicationInfo app = applist.get(position); 
     /* 
     try { 
      Intent intent = packageManager 
        .getLaunchIntentForPackage(app.packageName); 

      if (null != intent) { 
       startActivity(intent); 
      } 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(Activity_Eclair.this, e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(Activity_Eclair.this, e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
     } 
     */ 
     Uri packageUri = Uri.parse("package:"+app.packageName); 
     Intent uninstallIntent = 
      new Intent(Intent.ACTION_DELETE, packageUri); 
     startActivity(uninstallIntent); 
    } 



    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) { 
     ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>(); 
     for (ApplicationInfo info : list) { 
      try { 
       if (null != packageManager.getLaunchIntentForPackage(info.packageName)) { 
        applist.add(info); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     return applist; 
    } 


    private class LoadApplications extends AsyncTask<Void, Void, Void> {   

     public ProgressDialog progress = null; 

     @Override 
     protected Void doInBackground(Void... params) { 
      applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)); 
      listadaptor = new ApplicationAdapter(Activity_Eclair.this, 
        R.layout.snippet_list_row, applist); 

      return null; 
     } 

     @Override 
     protected void onCancelled() { 
      super.onCancelled(); 
     } 

     protected void onDestroy() { 
      if(progress!=null) 
       if(progress.isShowing()){ 
       progress.dismiss(); 
       } 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      setListAdapter(listadaptor); 
      progress.dismiss(); 
      super.onPostExecute(result); 
     } 

     @Override 
     protected void onPreExecute() { 
      progress = ProgressDialog.show(Activity_Eclair.this, null, 
        "Caricamento applicazioni in corso..."); 
      super.onPreExecute(); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 
    } 


} 

首先,如果你正在查看ProgressDialog我旋轉手機,應用程序崩潰,即使我把onDestroy方法。其次,滾動速度非常慢,你不知道如何改善它並使其變得流暢?第三件也是最後一件事,我只是無法進入ContextMenu,我真的做了很多嘗試,但我總是得到錯誤。你知道幫助我嗎?

回答

0
  1. 當您切換方向時,Android會創建一個新的視圖。你可能會因爲後臺線程試圖改變舊版本的狀態而崩潰。 (這也可能會遇到問題,因爲你的後臺線程不在UI線程上)從logcat的

+0

  • 後的錯誤,我只是解決了這個問題。你知道幫助我解決另外兩個問題嗎? –