Anyone please clarify my doubts in this program : –
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);
}
}
It’s showing the results not properly, if i’m going to select the checkall button, it’ll not work properly.
You basically need some data structure to keep track of checked items.
you can use boolean array in your custom adapter.
for more info refer this link Custom listview with checkbox problem
listview with checkbox
if you are looking for an example try this http://appfulcrum.com/?p=281 tutorial which solves the checkbox problem in listview using shared preferences.