lundi 1 juin 2015

Check all checkboxes in custom list view

I currently have ListFragment with a ArrayAdapter class The codes are below.

1)I want to implement a checkbox from listview main xml to check all the checkboxes that are present in custom list row xml which is inflated using Array adapter.

2)how to refresh view in this code by clearing all checkboxes when a button is clicked.

listview_main.xml

         <?xml version="1.0" encoding="utf-8"?>
         <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:weightSum="10"
         android:background="#fff2fff2" >

         <ListView
         android:id="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="10"
         android:choiceMode="multipleChoice" >
         </ListView>
         <TextView
         android:id="@android:id/empty"
         android:layout_width="wrap_content"
         />

         <LinearLayout
         android:layout_alignParentBottom="true"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >
         <Button
         android:id="@+id/btnin"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="In" />

         <Button
         android:id="@+id/btndelete"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Delete" />

         <CheckBox
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:id="@+id/checkAll"
         android:checked="false" />
         </LinearLayout>

         </RelativeLayout>

custom_row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
    android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="50dp" />

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/paackage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="" />
    </LinearLayout>
    </LinearLayout>

    </LinearLayout>

Adapter class

    public class FileAdapter extends ArrayAdapter<String>{

    private List<String> filelist = null;
    private Context context;
    CheckBox checkAll;

    public FileAdapter(Context _context, int _resource,List<String> _filelist) {
    super(_context,_resource,_filelist);
    this.context = _context;
    this.filelist = _filelist;
    this.itemChecked = new boolean[_filelist.size()];
    }

    private class ViewHolder {
    TextView Name;
    TextView pName;
    CheckBox ck1;
    ImageView iconview;
    }

    @Override
    public int getCount() {

    return ((null != filelist) ? filelist.size() : 0);

    }

    @Override
    public String getItem(int position) {

    return ((null != filelist) ? filelist.get(position) : null);
    }

    @Override
    public long getItemId(int position) {

    return position;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    View view = convertView;

    if (null == view) {

        LayoutInflater layoutInflater = (LayoutInflater) context

                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = layoutInflater.inflate(R.layout.custom_row, null);

        holder = new ViewHolder();

        holder.Name = (TextView) view.findViewById(R.id.name);
        holder.pName = (TextView) view
                .findViewById(R.id.paackage);
        holder.ck1 = (CheckBox) view.findViewById(R.id.checkBox1);
        holder.iconview = (ImageView) view.findViewById(R.id.app_icon);

        view.setTag(holder);


    } else {

        holder = (ViewHolder) convertView.getTag();
    }




    holder.appName.setText();

    holder.packageName.setText();

    holder.iconview.setImageDrawable();

    holder.ck1.setChecked(false);

    if (itemChecked[position])
        holder.ck1.setChecked(true);
    else
        holder.ck1.setChecked(false);



    holder.ck1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (holder.ck1.isChecked()) {
                itemChecked[position] = true;


            } else {
                itemChecked[position] = false;

            }
        }
    });



    return view;

    }
    }

ListFragment class

    public class FragmentA extends ListFragment implements View.OnClickListener {    
    public ArrayList<String> filelist;
    Button in, delete;
    ListView lv;
    CheckBox cb;
    private FileAdapter listadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    filelist = new ArrayList<String>();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View inflatedView = inflater.inflate(R.layout.fragment_app_restore, container, false);

    in = (Button) inflatedView.findViewById(R.id.btnin);
    delete= (Button) inflatedView.findViewById(R.id.btndelete);

    lv = (ListView) inflatedView.findViewById(android.R.id.list);
    empty = inflatedView.findViewById(android.R.id.empty);

        //calling asynctask to load files here
    delete.setOnClickListener(this);
    in.setOnClickListener(this);

    return inflatedView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);



    cb = (CheckBox) v.findViewById(R.id.checkBox1);//this is the checkbox in row only
    cb.performClick();
    if (cb.isChecked()) {

    } else {

    }
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btndelete:
            break;
        case R.id.btnin:
            break;
    }
    }

    private class LoadFiles extends AsyncTask<Void, Void, Void> {



    @Override
    protected Void doInBackground(Void... params) {

        filelist = GetFiles(root_path);

        if (filelist != null) {
            listadaptor = new AppRestoreFileAdapter(getActivity(),

                    R.layout.custom_row, filelist);
        } else {
            // lv.setEmptyView(empty);

        }

        return null;

    }
    @Override
    protected void onPostExecute(Void result) {

        lv.setAdapter(listadaptor);

        progress.dismiss();

        super.onPostExecute(result);

    }
    }
    }




Aucun commentaire:

Enregistrer un commentaire