lundi 19 septembre 2016

how to store values of checked check box in database mysql or sqlite any

I have created custom listview with image and text and check box. Now i want to store values of string array in database which are checked. I tried to store position of checked item in integer array and returned that array in activity but it's not working.

This the main activity

public class Industry extends AppCompatActivity {

    ListView listView;
    Toolbar toolbar;
    ArrayList<HashMap<String, Object>> topicname;
    ListTopic adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_industry);

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setTitle("Industries");

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }

        listView = (ListView)findViewById(R.id.listView);
        //these arrays are just the data that
        //I'll be using to populate the ArrayList
        //You can use our own methods to get the data
        String names[]={"Advanced Industries",
                "Automotive & Assembly",
                "Consumer & Retails",
                "Energy, Resources & Materials",
                "Financial Services",
                "Health System & Services",
                "High Tech, Telecoms & Internet",
                "Infrastructure",
                "Pharmaceuticals & Medical Products"};

        Integer[] photos={R.drawable.corporate,
                R.drawable.corporate,
                R.mipmap.consumerandretail,
                R.drawable.corporate,
                R.drawable.corporate,
                R.mipmap.healthsystemandservices,
                R.mipmap.hightechtelecomsandinternet,
                R.mipmap.infrastructure,
                R.drawable.corporate,};

        topicname=new ArrayList<HashMap<String,Object>>();

        //temporary HashMap for populating the
        //Items in the ListView
        HashMap<String , Object> temp;

        //total number of rows in the ListView
        int noOfPlayers=names.length;

        //now populate the ArrayList players
        for(int i=0; i<noOfPlayers; i++)
        {
            temp=new HashMap<String, Object>();

            temp.put("name", names[i]);
            temp.put("photo", photos[i]);

            //add the row to the ArrayList
            topicname.add(temp);
        }

 /*create the adapter
 *first param-the context
 *second param-the id of the layout file
  you will be using to fill a row
 *third param-the set of values that
   will populate the ListView */
        adapter=new ListTopic(this, R.layout.list_adapter,topicname);

        //finally,set the adapter to the default ListView
        listView.setAdapter(adapter);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);

        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save:
                save();
                Toast.makeText(Industry.this, "SAVE CLICKED", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }



    public void save(){

        int[] position ;
        position = adapter.getPos();

        for(int i=0; i<position.length; i++){

            Log.d("Industries","Success"+position.length);
        }
    }
}

this

rivate Activity context;

    boolean[] checkBoxState;
    ArrayList<HashMap<String, Object>> topicNames;

    ViewHolder holder;
    int[] pos;

    public ListTopic(Activity context,  int imageid, ArrayList<HashMap<String, Object>> topicNames) {
        super(context, imageid,topicNames );
        this.context= context;
        this.topicNames= topicNames;
        checkBoxState=new boolean[topicNames.size()];
    }

    private class ViewHolder
    {
        ImageView photo;
        TextView name;
        CheckBox checkBox;
    }

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


        ViewHolder holder = null;

        if(convertView == null){
            LayoutInflater inflater = context.getLayoutInflater();
            convertView=inflater.inflate(R.layout.list_adapter, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.imageView);
             holder.name = (TextView) convertView.findViewById(R.id.textViewName);
             holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(holder);

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

        int photoId=(Integer) topicNames.get(position).get("photo");

        //set the data to be displayed
        holder.photo.setImageDrawable(getContext().getResources().getDrawable(photoId));
        holder.name.setText(topicNames.get(position).get("name").toString());

        //VITAL PART!!! Set the state of the
        //CheckBox using the boolean array
        holder.checkBox.setChecked(checkBoxState[position]);

        //for managing the state of the boolean
        //array according to the state of the
        //CheckBox

        holder.checkBox.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(((CheckBox)v).isChecked()) {
                    checkBoxState[position] = true;


                    for (int i=0; i<1; i++){
                        pos = new int[i++];
                        int x = 0;
                        x = position;
                        Log.d("Soulsystem","Added........................."+position);
                        setPos(x);
                    }
                }
                else
                    checkBoxState[position]=false;

            }
        });

        //return the view to be displayed
        return convertView;
    }

    public void setIndustries(int i){

    }

    public int[] getPos() {
        return pos;
    }

    public void setPos(int j) {
        for(int i = 0; i<1; i++){
            pos[i] = j;
            Log.d("Soulsystem","Set.........................."+j);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire