jeudi 24 mars 2016

How to get Value of ID and text from checked value in recyclerview?

It may be dumb question how to get the value of text and id of checked item in recyclerview android here let me explain my requirement am binding recyclerview with sqlitedatabase everything is ok till that but i could not get the checked value and id from that recyclerview how can i do this let me share my code so far what i have done:

This is the adapter where i set to recyclerview:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class CardViewDataAdapter extends
        RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

    private List<Model_Account> stList;

    public CardViewDataAdapter(List<Model_Account> students) {
        this.stList = students;

    }

    // Create new views
    @Override
    public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        final int pos = position;

        viewHolder.tvName.setText(stList.get(position).getName());


        viewHolder.chkSelected.setChecked(stList.get(position).isSelected());

        viewHolder.chkSelected.setTag(stList.get(position));


        viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                Model_Account contact = (Model_Account) cb.getTag();

                contact.setSelected(cb.isChecked());
                stList.get(pos).setSelected(cb.isChecked());

                Toast.makeText(
                        v.getContext(),
                        "Clicked on Checkbox: " + cb.getText() + " is "
                               , Toast.LENGTH_LONG).show();
            }
        });

    }

    // Return the size arraylist
    @Override
    public int getItemCount() {
        return stList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName;
        public TextView tvEmailId;

        public CheckBox chkSelected;

        public Model_Account singlestudent;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

            tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
            chkSelected = (CheckBox) itemLayoutView
                    .findViewById(R.id.chkSelected);

        }

    }

    // method to access in activity after updating selection
    public List<Model_Account> getStudentist() {
        return stList;
    }

This is the activity where is set adapter to recyclerview and need to get value in onclick button :

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

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

public class ServiceCatlogActivity extends AppCompatActivity {
    private List<Model_Account> statelist;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    Button ok;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_catlog);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        statelist = new ArrayList<Model_Account>();
        ok = (Button) findViewById(R.id.btnShow);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        Account_SF_DB account_sf_db = new Account_SF_DB(getApplicationContext());
        statelist = account_sf_db.Statelist();

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        // create an Object for Adapter
        mAdapter = new CardViewDataAdapter(statelist);

        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Model_Account> stList = ((CardViewDataAdapter) mAdapter)
                        .getStudentist();
                 String[] data = new String[stList.size()];
                Integer []ints=new Integer[stList.size()];
                for (int i = 0; i < stList.size(); i++) {
                    Model_Account singleStudent = stList.get(i);
                    if (singleStudent.isSelected()) {


                  //Here i need to pick the id and text of the checked value how can i do this!!

                    }
                }


            }


        });
    }
}

}

This is sqliteonehelper class:

  public List<Model_Account> Statelist() {
        String countQuery = "SELECT  * FROM " + Model_Account.spinner_state_table;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
       List<Model_Account>listobj = new ArrayList<Model_Account>();
        if (cursor.moveToFirst()) {
            do {
                Model_Account modelobj = new Model_Account();
                modelobj.setStateid(cursor.getInt(cursor.getColumnIndex(Model_Account._id)));
                modelobj.setName(cursor.getString(cursor.getColumnIndex(Model_Account.StateName)));
                modelobj.setStateid(cursor.getInt(cursor.getColumnIndex(Model_Account.PairlinkID)));
                listobj.add(modelobj);

            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return listobj;


    }

Here need to pass the text and id to next activity through bundle this may be dumb question but as a beginner its quiet confusing

Aucun commentaire:

Enregistrer un commentaire