如何从recyclerview中的已检查值中获取ID和文本的值?

时间:2016-03-24 12:31:17

标签: android sqlite android-recyclerview

如何在recyclelerview android中获取文本的值和已检查项目的id可能是一个愚蠢的问题让我解释一下我的要求是使用sqlitedatabase绑定recyclerview一切正常,但是我无法从中获取已检查的值和id那个recyclerview我怎么能这样做让我分享我的代码到目前为止我所做的:

这是我设置为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;
    }

这是将适配器设置为recyclerview并需要在onclick按钮中获取值的活动:

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!!

                    }
                }


            }


        });
    }
}

}

这是sqliteonehelper类:

  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;


    }

这里需要通过bundle将text和id传递给下一个活动这可能是一个愚蠢的问题,但作为一个初学者,它的安静令人困惑

1 个答案:

答案 0 :(得分:1)

<强> DataAdapter.class

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

    private final ArrayList<String> countries;

    Context v;
    private CheckBox isSelect;

    public DataAdapter(ArrayList<String> countries, MainActivity mainActivityClass) {
        this.countries = countries;
        this.v = mainActivityClass;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int i) {

        holder.tv_country.setText(countries.get(i));

        isSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(v, "" + countries.get(i), Toast.LENGTH_SHORT).show();
                } else {

                }
            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return countries.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView tv_country;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_country = (TextView) itemView.findViewById(R.id.tv_country);
            isSelect = (CheckBox) itemView.findViewById(R.id.checkbox);

            itemView.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            tv_country = (TextView) v.findViewById(R.id.tv_country);
        }
    }
}

按照我的课程示例,你得到答案。