mardi 25 octobre 2016

Android ListFragment multiple checkbox in a single row

I'm facing some problems, so I though maybe someone could help me as I'm at dead end and have no idea how to fix this. So I'm creating an app, which use List Fragment with multiple (exactly 5) checkbox'es and 1 text view in a single row. So the first problem is: I want to check only one of 5 checkbox'es(that means if I check first one, then other(second,third,fourth,fifth box) shouldn't be checked and so on...). And the second and most important problem is that there will be like 10-20 rows, so as I scroll down/up some rows gets recycled and checked boxes become unchecked or randomly checked, so how I'm supposed to store data, so that checkbox'es which were checked won't be unchecked or randomly checked after scrolling up or down. Also onListItemClick do not work too :(

ListFragment:

package com.android4dev.navigationview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static android.support.design.widget.Snackbar.*;


public class SymptomsSelectionFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_symptoms_selection, container, false);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2","shitPhone","testPhone", "boomPhone", "meskaPhone" };
        ArrayAdapter<Question> adapter = new InteractiveArrayAdapter(getActivity(),
                getQuestion());
        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        fab.hide();
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO implement some logic
        super.onListItemClick(l, v, position, id);
        String item = (String) getListAdapter().getItem(position);
        Snackbar.make(getListView(), item+" selected", Snackbar.LENGTH_LONG).show();
        Toast.makeText(getActivity(),position+" <--- Nr.",Toast.LENGTH_SHORT).show();
    }
    private List<Question> getQuestion() {
        List<Question> list = new ArrayList<Question>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));
        // Initially select one of the items
        //list.get(1).setSelected(true);
        return list;
    }

    private Question get(String s) {
        return new Question(s);
    }







}

ArrayAdapter:

import java.util.List;
package com.android4dev.navigationview;
import android.app.Activity;
import android.media.Image;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Question> {

    private final List<Question> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Question> list) {
        super(context, R.layout.row_layout_symptoms_selection, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox1;
        protected CheckBox checkbox2;
        protected CheckBox checkbox3;
        protected CheckBox checkbox4;
        protected CheckBox checkbox5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row_layout_symptoms_selection, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label_symptom);
            viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.button1);
            viewHolder.checkbox2 = (CheckBox) view.findViewById(R.id.button2);
            viewHolder.checkbox3 = (CheckBox) view.findViewById(R.id.button3);
            viewHolder.checkbox4 = (CheckBox) view.findViewById(R.id.button4);
            viewHolder.checkbox5 = (CheckBox) view.findViewById(R.id.button5);
            viewHolder.checkbox1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element = (Question) viewHolder.checkbox1
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox2
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element2 = (Question) viewHolder.checkbox2
                                    .getTag();
                            element2.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox3
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element3 = (Question) viewHolder.checkbox3
                                    .getTag();
                            element3.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox4
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element4 = (Question) viewHolder.checkbox4
                                    .getTag();
                            element4.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox5
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element5 = (Question) viewHolder.checkbox5
                                    .getTag();
                            element5.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox1.setTag(list.get(position));
            viewHolder.checkbox2.setTag(list.get(position));
            viewHolder.checkbox3.setTag(list.get(position));
            viewHolder.checkbox4.setTag(list.get(position));
            viewHolder.checkbox5.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox1.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox2.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox3.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox4.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox5.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox1.setChecked(list.get(position).isSelected());
        holder.checkbox2.setChecked(list.get(position).isSelected());
        holder.checkbox3.setChecked(list.get(position).isSelected());
        holder.checkbox4.setChecked(list.get(position).isSelected());
        holder.checkbox5.setChecked(list.get(position).isSelected());
        return view;
    }
}

QuestionClass:

 package com.android4dev.navigationview;

/**
 * Created by Home-PC on 10/19/2016.
 */

public class Question {
    private String name;
    private boolean selected;

    public Question(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

fragment_symptoms_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="58dp"
    >


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:text="No data" />
</LinearLayout>

row_layout_symptoms_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    xmlns:custom="http://ift.tt/GEGVYd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    tools:context=".SymptomsActivity"
    >

    <TextView
        android:id="@+id/label_symptom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/text"
        android:textSize="19dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:button="@drawable/checkbox_drawable"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:background="@color/CheckBox1"
            />
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:background="@color/CheckBox2"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:background="@color/CheckBox3"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:background="@color/CheckBox4"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:background="@color/CheckBox5"/>
    </LinearLayout>
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire