我有一个listview
的自定义适配器,它的工作正常。但是将数据加载到listview
需要15秒以上,并使用60mb的ram内存。请告诉我这是什么问题以及为什么花费太多时间?请帮我一些代码或编辑我的代码。这是我的代码。提前致谢!
自定义适配器
public class Listadapter extends BaseAdapter {
CheckBox checkBox;
boolean index[];
boolean[] itemChecked;
ApplicationInfo entry;
String[] itempkg;
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
public Listadapter(Context applicationContext, List<ApplicationInfo> installedApplication, PackageManager packageManager)
{
//super(applicationContext,textViewResourceId,installedApplication);
super();
this.mContext = applicationContext;
this.mListAppInfo = installedApplication;
index = new boolean[installedApplication.size()];
this.mPackManager = packageManager;
for (int i = 0; i < installedApplication.size(); i++) {
checkList.add(false);
itemChecked = new boolean[installedApplication.size()];
itempkg = new String[installedApplication.size()];
}
}
@Override
public int getCount()
{
return mListAppInfo.size();
//return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
}
@Override
public Object getItem(int position)
{
// index = new boolean[mListAppInfo.size()];
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// get the selected entry
final ViewHolder holder;
// LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
// reference to convertView
holder.tvAppName = (TextView) convertView
.findViewById(R.id.textView1);
holder.tvPkgName = (TextView) convertView
.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.ivAppIcon = (ImageView) convertView
.findViewById(R.id.imageView);
convertView.setTag(holder);
// holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
entry = mListAppInfo.get(position);
holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
holder.tvAppName.setText(entry.loadLabel(mPackManager));
holder.tvPkgName.setText(entry.packageName);
holder.checkBox.setChecked(false);
if (itemChecked[position])
holder.checkBox.setChecked(true);
else
holder.checkBox.setChecked(false);
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkBox.isChecked()) {
itemChecked[position] = true;
// SharedPreferences preferences = mContext.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
// SharedPreferences.Editor edit = preferences.edit();
// edit.putBoolean("checkbox_" + position, true);
// edit.commit();
// editor.putBoolean("CheckBoxState" + position, true);
} else {
itemChecked[position] = false;
// editor.putBoolean("CheckBoxState" + position, false);
// SharedPreferences preferences = mContext.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
// SharedPreferences.Editor edit = preferences.edit();
// edit.putBoolean("checkbox_" + position, false);
// edit.commit();
}
}
});
return convertView;
}
public void setItemChecked5(boolean[] items5) {
itemChecked = items5;
}
private class ViewHolder
{
ImageView ivAppIcon;
TextView tvAppName;
TextView tvPkgName;
CheckBox checkBox;
}
}
这是活动代码
public class Profile5Activity extends Activity implements AdapterView.OnItemClickListener {
ListView apps5;
PackageManager packageManager5;
ArrayList<String> checkedValue5;
Button bt5;
ApplicationInfo pi5 = new ApplicationInfo();
Context context = this;
static String currentApp5 = "NULL";
CheckBox cb5;
Listadapter Adapter5 = null;
boolean[] itemChecked5;
//static String currentApp5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile5);
itemChecked5 = new boolean[AllApps.getInstalledApplication(this).size()];
//needPermissionForBlocking(MainActivity.mainContext);
SharedPreferences preferencess2 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
Set<String> seta = preferencess2.getStringSet("pkgname5", null);
if (seta != null && !seta.isEmpty() && !seta.equals("null")) {
checkedValue5 = new ArrayList<String>(seta);
}
else
{
checkedValue5 = new ArrayList<String>();
}
// bt1 = (Button) findViewById(R.id.button1);
apps5 = (ListView) findViewById(R.id.listViewm);
apps5.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
packageManager5 = getPackageManager();
List<String> list = new ArrayList<String>();
final boolean[] items5 = new boolean[AllApps.getInstalledApplication(this).size()];
SharedPreferences preferences5 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
for (int i = 0; i < AllApps.getInstalledApplication(this).size(); ++i) {
items5[i] = preferences5.getBoolean("checkbox_5" + i, false);
}
Adapter5 = new Listadapter(this, AllApps.getInstalledApplication(this), packageManager5);
Adapter5.setItemChecked5(items5);
apps5.setAdapter(Adapter5);
apps5.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
// TODO Auto-generated method stub
cb5 = (CheckBox) v.findViewById(R.id.checkBox1);
TextView tv5 = (TextView) v.findViewById(R.id.textView);
pi5 = (ApplicationInfo) arg0.getItemAtPosition(position);
cb5.performClick();
if (cb5.isChecked()) {
checkedValue5.add(tv5.getText().toString());
itemChecked5[position] = true;
String pname = (tv5.getText().toString());
// Toast.makeText(MainActivity.this, "all" + position, Toast.LENGTH_LONG).show();
SharedPreferences preferences1 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences1.edit();
Set<String> setttt1 = new HashSet<String>();
setttt1.addAll(checkedValue5);
edit.putBoolean("checkbox_5" + position, true);
edit.putStringSet("pkgname5", setttt1);
edit.commit();
} else if (!cb5.isChecked()) {
checkedValue5.remove(tv5.getText().toString());
String pname = (tv5.getText().toString());
SharedPreferences preferences1 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences1.edit();
Set<String> setttt1 = new HashSet<String>();
setttt1.addAll(checkedValue5);
edit.putBoolean("checkbox_5" + position, false);
edit.putStringSet("pkgname5", setttt1);
edit.commit();
}
}