jeudi 21 juillet 2016

android listview with checkbox onclicklistener not working

i have a listview with custom adapter.on listview i have textview image and checkbox.its working fine. i have a arraylist when i click on item that item value store in arraylist and when i again click on item value remove from array list which work fine.issue is this when i click on checkbox value not store in arraylist what is the issue i try my best but not solve my problem. i want when i click on item or checkbox its value store in arraylist here is my custom adapter code.

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;
    }
}

here is my 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();
        }

    }

this is my custom layout

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="55dp"
        android:layout_height="55dp"

        android:button="@drawable/checkbox_selector"
        android:focusable="false"
        android:focusableInTouchMode="false"

        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:id="@+id/textView"
        android:textSize="11dp"
        android:layout_width="266dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignStart="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    <ImageView
        android:paddingLeft="9dp"
        android:id="@+id/imageView"
        android:layout_width="55dp"
        android:layout_height="55dp" />
</RelativeLayout>

this is listview

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list5"
        android:dividerHeight="0dp"
        android:listSelector="#00000000"
        android:smoothScrollbar="true"
        android:scrollingCache="false"
        android:animationCache="false"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/ed44" />

please help me with some code . i want when i click on item or checkbox its value store in arraylist. thanks in advance




Aucun commentaire:

Enregistrer un commentaire