i have a custom listview with checkbox . its working fine. when i click on any item its value store to arraylist and when i again click value remove from array list. its working but when i click on checkbox its value not store to the arraylist.i want to use checkbox setOnCheckedChangeListener to handle checkbox but issue is this i have only one adapter and i want to use this adapter for more than on activity. please tell me can i use setOnCheckedChangeListener in activity. if i can than how to can i get position and textview and checkbox in activity please tell me with some code or edit my code thanks in adavnce. here is my adapter
public class Listadapter extends BaseAdapter {
CheckBox checkBox;
boolean index[];
boolean[] itemChecked;
ResolveInfo entry;
String[] itempkg;
private Context mContext;
private List<ResolveInfo> mListAppInfo;
private PackageManager mPackManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
public Listadapter(Context applicationContext, List<ResolveInfo> 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.activityInfo.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;
} else {
itemChecked[position] = false;
}
}
});
return convertView;
}
private class ViewHolder
{
ImageView ivAppIcon;
TextView tvAppName;
TextView tvPkgName;
CheckBox checkBox;
}
}
this is activity code.
public class Profile5Activity extends Activity implements AdapterView.OnItemClickListener {
ListView apps5;
PackageManager packageManager5;
static ArrayList<String> checkedValue5;
Button bt5;
ResolveInfo pi5 = new ResolveInfo();
Context context = this;
CheckBox cb5;
Listadapter Adapter5 = null;
boolean[] itemChecked5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile5);
itemChecked5 = new boolean[AllApps.getInstalledApplication(this).size()];
checkedValue5 = new ArrayList<String>();
}
apps5 = (ListView) findViewById(R.id.list5);
apps5.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
packageManager5 = getPackageManager();
List<String> list = new ArrayList<String>();
Adapter5 = new Listadapter(this, AllApps.getInstalledApplication(this), packageManager5);
apps5.setAdapter(Adapter5);
apps5.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, final int position, long arg3)
{
// TODO Auto-generated method stub
cb5 = (CheckBox) v.findViewById(R.id.checkBox1);
final TextView tv5 = (TextView) v.findViewById(R.id.textView);
final TextView tvv5 = (TextView) v.findViewById(R.id.textView1);
pi5 = (ResolveInfo) arg0.getItemAtPosition(position);
cb5.performClick();
if (cb5.isChecked()) {
checkedValue5.add(tv5.getText().toString());
itemChecked5[position] = true;
Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();
} else if (!cb5.isChecked()) {
checkedValue5.remove(tv5.getText().toString());
Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();
}
}
Aucun commentaire:
Enregistrer un commentaire