03-23 15:07:45.899: E/AndroidRuntime(12482): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
public class AppManagerActivity extends Activity implements OnClickListener {
private List mAppInfoList;
private ListView lv_app_list;
private MyAdapter mAdapter;
private ArrayList<AppInfo> mSystemList;
private ArrayList<AppInfo> mCustomerList;
private TextView tv_des;
private AppInfo mAppInfo;
private Handler mHandler =new Handler(){
public void handleMessage(android.os.Message msg){
mAdapter = new MyAdapter();
lv_app_list.setAdapter(mAdapter);
if (tv_des!=null&&mCustomerList!=null) {
tv_des.setText("用户应用("+mCustomerList.size()+")");
}
};
};
class MyAdapter extends BaseAdapter{
//获取适配器中条目类型的总数(改成两种,纯文字,文件+图片)
@Override
public int getViewTypeCount(){
return super.getViewTypeCount()+1;
}
//指定索引指向条目的类型,条目类型状态码指定(0(复用),1)
@Override
public int getItemViewType(int position){
if (position==0||position==mCustomerList.size()+1) {
//返回0,代表纯文本条目的状态码
return 0;
}
else {
//返回1,代表文本+图片条目的状态码
return 1;
}
}
@Override
public int getCount() {
//listView中增加两个描述项目
return mSystemList.size()+mCustomerList.size()+2;
}
@Override
public AppInfo getItem(int position) {
if (position==0||position==mCustomerList.size()+1) {
return null;
}else {
if (position<mCustomerList.size()+1) {
return mCustomerList.get(position-1);
}else {
//返回应用对应条目的对象
return mSystemList.get(position-mCustomerList.size()-2);
}
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (type==0) {
//展示纯文本灰色条目
ViewTitleHolder holder=null;
if (convertView==null) {
convertView=View.inflate(getApplicationContext(),R.layout.listview_app_item_title,null);
holder=new ViewTitleHolder();
holder.tv_title=(TextView)convertView.findViewById(R.id.tv_title);
convertView.setTag(holder);
}else {
holder=(ViewTitleHolder)convertView.getTag();
}
if (position==0) {
holder.tv_title.setText("用户应用("+mCustomerList.size()+")");
}else {
holder.tv_title.setText("系统应用("+mSystemList.size()+")");
}
return convertView;
}else {
//展示文本加图片条目
ViewHolder holder=null;
if (convertView==null) {
convertView=View.inflate(getApplicationContext(),R.layout.listview_app_item,null);
holder=new ViewHolder();
holder.iv_icon=(ImageView)convertView.findViewById(R.id.iv_icon);
holder.tv_name=(TextView)convertView.findViewById(R.id.tv_name);
holder.tv_path=(TextView)convertView.findViewById(R.id.tv_path);
convertView.setTag(holder);
}else {
holder=(ViewHolder)convertView.getTag();
}
holder.iv_icon.setBackgroundDrawable(getItem(position).icon);
holder.tv_name.setText(getItem(position).name);
if (getItem(position).isSdCard) {
holder.tv_path.setText("sd卡应用");
}else {
holder.tv_path.setText("手机应用");
}
return convertView;
}
}
}
static class ViewHolder{
ImageView iv_icon;
TextView tv_name;
TextView tv_path;
}
static class ViewTitleHolder{
TextView tv_title;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_manager);
initTitle();
initList();
}
private void initList() {
tv_des = (TextView)findViewById(R.id.tv_des);
lv_app_list = (ListView)findViewById(R.id.lv_app_list);
new Thread(){
public void run(){
mAppInfoList = AppInfoProvider.getAppInfoList(getApplicationContext());
mSystemList = new ArrayList<AppInfo>();
mCustomerList = new ArrayList<AppInfo>();
for (AppInfo appInfo : mAppInfoList) {
if (appInfo.isSystem) {
//系统应用
mSystemList.add(appInfo);
}else {
//非系统应用
mCustomerList.add(appInfo);
}
}
mHandler.sendEmptyMessage(0);
};
}.start();
lv_app_list.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//滚动过程中调用方法
//AbsListView中view就是listView对象
//firstVisibleItem第一个可见条目索引值
//visibleItemCount当前一个屏幕的可见条目数
//总共条目总数
if (mCustomerList!=null&&mSystemList!=null) {
if (firstVisibleItem>=mCustomerList.size()+1) {
//滚动到了系统应用
tv_des.setText("系统应用("+mSystemList.size()+")");
}else {
//滚动到了用户条目
tv_des.setText("用户应用("+mCustomerList.size()+")");
}
}
}
});
lv_app_list.setOnItemClickListener(new OnItemClickListener(){
//view点中条目指向view的对象
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position==0||position==mCustomerList.size()+1) {
return;
}else {
if (position<mCustomerList.size()+1) {
mAppInfo = mCustomerList.get(position-1);
}else {
//返回应用对应条目的对象
mAppInfo=mSystemList.get(position-mCustomerList.size()-2);
}
showPopupWindow(view);
}
}
});
}
protected void showPopupWindow(View view) {
View popupView = View.inflate(this,R.layout.popupwindow_layout,null);
TextView tv_uninstall =(TextView)popupView.findViewById(R.id.tv_uninstall);
TextView tv_start=(TextView)popupView.findViewById(R.id.tv_start);
TextView tv_share=(TextView)popupView.findViewById(R.id.tv_share);
tv_uninstall.setOnClickListener(this);
tv_start.setOnClickListener(this);
tv_share.setOnClickListener(this);
//1,创建窗体对象,指定宽高
PopupWindow popupWindow = new PopupWindow(view,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,true);
//2,设置一个透明背景,如果不设置,回退按钮没反应
popupWindow.setBackgroundDrawable(new ColorDrawable());
//3,指定窗体位置
popupWindow.showAsDropDown(view,50,-view.getHeight());
}
/**
*
*/
private void initTitle() {
//1,获取磁盘(区分于运存)可用大小,磁盘路径
String path = Environment.getDataDirectory().getAbsolutePath();
//2,获取sd卡可用大小,sd卡路径
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//3,获取以上两个路径下文件夹的可用大小
String memoryAvailSpace= Formatter.formatFileSize(this,getAvailSpace(path));
String sdMemoryAvailSpace = Formatter.formatFileSize(this,getAvailSpace(sdPath));
TextView tv_memory = (TextView) findViewById(R.id.tv_memory);
TextView tv_sd_memory = (TextView) findViewById(R.id.tv_sd_memory);
tv_memory.setText("磁盘可用:"+memoryAvailSpace);
tv_sd_memory.setText("sd卡可用:"+sdMemoryAvailSpace);
}
private long getAvailSpace(String path) {
//statFs获取可用磁盘大小的一个类
StatFs statFs = new StatFs(path);
//获取可用区块的个数
long count = statFs.getAvailableBlocks();
//获取区块的大小
long size = statFs.getBlockSize();
//区块大小*可用区块个数=可用空间大小
return count*size;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}