任何人请澄清我对此计划的疑虑: -
BackUpActivity.java: -
public class BackUpActivity extends Activity
{
//Use ArrayList to store the installed non-system apps
ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
//ListView app_listView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0);
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0; i<packages.size(); i++)
{
PackageInfo packageInfo = packages.get(i);
AppInfo tmpInfo = new AppInfo();
tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName = packageInfo.packageName;
tmpInfo.versionName = packageInfo.versionName;
tmpInfo.versionCode = packageInfo.versionCode;
tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(getPackageManager());
// Only display the non-system app info
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)
{
appList.add(tmpInfo);
}
}
for(int i=0;i<appList.size();i++)
{
appList.get(i).print();
}
//Populate data to listView
ListView app_listView=(ListView)findViewById(R.id.listview1);
AppAdapter appAdapter=new AppAdapter(BackUpActivity.this,appList);
//app_listView.setAdapter(appAdapter);
app_listView.setDividerHeight(5);
if(app_listView!=null)
{
app_listView.setAdapter(appAdapter);
}
}
public class AppAdapter extends BaseAdapter
{
Context context;
ArrayList<AppInfo> dataList=new ArrayList<AppInfo>();
public AppAdapter(Context context,ArrayList<AppInfo> inputDataList)
{
this.context=context;
dataList.clear();
for(int i=0;i<inputDataList.size();i++)
{
dataList.add(inputDataList.get(i));
}
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return dataList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v=convertView;
final AppInfo appUnit=dataList.get(position);
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
}
TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.icon);
if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.BackUp:
try
{
backup();
}catch(Exception e){}
// finish();
break;
case R.id.CheckAll:
try
{
checkall();
}catch(Exception e){e.notify();}
break;
case R.id.Cancel:
try
{
cancel();
}catch(Exception e){}
break;
}
return true;
}
public void backup()
{
Toast.makeText(this, "Backup Data", Toast.LENGTH_SHORT).show();
}
public void checkall()
{
CheckBox c = (CheckBox)this.findViewById(R.id.checkBox1);
if (c.isChecked())
{
c.setChecked(false);
}
else
{
c.setChecked(true);
}
}
public void cancel()
{
finish();
}
}
AppInfo.java
package com.android.backup;
import android.graphics.drawable.Drawable;
import android.util.Log;
public class AppInfo
{
public String appName="";
public String packageName="";
public String versionName="";
public int versionCode=0;
public Drawable appIcon=null;
public void print()
{
Log.v("app","Name:"+appName+" Package:"+packageName);
Log.v("app","Name:"+appName+" versionName:"+versionName);
Log.v("app","Name:"+appName+" versionCode:"+versionCode);
}
}
它显示结果不正确,如果我要选择checkall按钮,它将无法正常工作。
答案 0 :(得分:0)
您基本上需要一些数据结构来跟踪已检查的项目。 您可以在自定义适配器中使用布尔数组。
有关详细信息,请参阅此链接Custom listview with checkbox problem
如果您正在寻找示例,请尝试使用共享首选项解决listview中的复选框问题的http://appfulcrum.com/?p=281教程。