lundi 23 novembre 2015

How to handle custom checkbox in listview and accroding to selected check the other listview with checkbox is populated

I have a signup screen in that I One field profession, i supposed to be clicked on profession I got to next activity with activity startActivtiy for result and their i have one custom listview with check box, i can select multiple checkboxes, and click on submit either on check box basis i go to another activity which is again STARTACTIVITYFORRESULT and there i have same lcustom listview and checkbox i can populate that on the checked box of previous one. And then move to my main activity with all data and how to i populate that and get The data of selected check boxes from the listview for both going child listview and back for main activity.
The code i tried is as follows Registeration Activity
Profession.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in =new Intent(RegisterActivity.this,Signup_category.class);
                in.putExtra("typeOfUser", typeOfUser);
                startActivityForResult(in, 2);
            }
        });

ProfessionCatgeoryParent.class

My Parent class where i come first and select checkbox from there and got o child class. public class Signup_category extends ActionBarActivity implements OnItemClickListener { ListView listView1; Button btn; ArrayList list; DatabaseHandler databaseHandler; String typeOfUser; Context context = null;

    CategoryAdapter categoryAdapter;

    ListView lv = null;
    ArrayList<String> selectedStrings = new ArrayList<String>();
    ArrayList<ProfessionEntity> listProfession = new ArrayList<ProfessionEntity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup_category);
        listView1 = (ListView) findViewById(R.id.listView1);
        btn = (Button) findViewById(R.id.button1);
        listView1.setOnItemClickListener(this);
        listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        InitDatabase();
        Intent in = getIntent();
        typeOfUser = in.getStringExtra("typeOfUser");

        // Log.d("typeOfUser",typeOfUser);
        loadProfessionData(typeOfUser);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = "hello";
                // TODO Auto-generated method stub
                getDataPrint();
                Intent intent = new Intent();
                intent.putExtra("MESSAGE", message);
                setResult(2, intent);
                finish();
            }
        });
    }

    private void loadProfessionData(String pos) {

        listProfession = databaseHandler.getProfession(pos);

        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < listProfession.size(); i++) {
            Log.d("prfoession:", listProfession.get(i).getProfessionName());
            list.add("" + listProfession.get(i).getProfessionName());
        }
        ArrayAdapter<String> dataProfession = new ArrayAdapter<String>(
                Signup_category.this, R.layout.spinner_profession_row,
                R.id.checkBox1, list);
        listView1.setAdapter(dataProfession);
    }

    private void InitDatabase() {
        // TODO Auto-generated method stub
        try {
            databaseHandler = new DatabaseHandler(Signup_category.this);
            databaseHandler.createdatabase();

        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            databaseHandler.opendatabase();
        } catch (SQLException sqle) {
            throw sqle;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        final CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
        Log.d("hii", "hii");
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    selectedStrings.add(cb.getText().toString());
                } else {
                    selectedStrings.remove(cb.getText().toString());
                }
            }
        });
    }
activity_category_parent.xml

The xml of parent class and child class is also going have similar xml file

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.pstalk.Signup_category" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_alignParentLeft="true" >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listView1"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="17dp"
        android:text="Submit" />

</RelativeLayout>

Category Adapter Parent for BaseAdapter extneds and implements for list in parent list class

public class CategoryAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater inflater;
    private List<ProfessionEntity> mainDataList = null;
    private ArrayList<ProfessionEntity> arraylist;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    static class ViewHolder{
        protected CheckBox check;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
         final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.spinner_profession_row, null);

                holder.check = (CheckBox) view.findViewById(R.id.checkBox1);

                view.setTag(holder);
                view.setTag(R.id.checkBox1, holder.check);

                holder.check
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(CompoundButton vw,
                                    boolean isChecked) {

                                int getPosition = (Integer) vw.getTag();
                                mainDataList.get(getPosition).setSelected(vw.isChecked());

                            }
                        });

            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.check.setTag(position);
            holder.check.setChecked(mainDataList.get(position).isSelected());

            return view;
    }

}

CategoryEntitiyParent.java

public class ProfessionEntity {
    String profId;
    String professionName;
    private boolean selected;

    public String getProfId() {
        return profId;
    }

    public void setProfId(String profId) {
        this.profId = profId;
    }

    public String getProfessionName() {
        return professionName;
    }

    public void setProfessionName(String professionName) {
        this.professionName = professionName;
    }

    public boolean isSelected() {
        return selected;
    }

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

}

rest classes I am not getting what to be generate for this...???




Aucun commentaire:

Enregistrer un commentaire