“交换”适配器如何从Sqlite中填充回收站

时间:2018-12-14 14:34:23

标签: android-recyclerview adapter swap

我更改了密码。 我从列表视图切换到了recyclerview。 通过使用从数据库获取的数据填充视图,我设法实现了回收器而没有错误。 到目前为止,一切正常。 我想知道“ populateRecyclerLibretto”中的metoto是否可以通过单击按钮来更改顺序。

我有一个按钮,显示了一个微调框,在其中将确认保存为首选项的情况下选择“ orderByColumn”,并启动类populateRecyclerLibretto()。 这样做对吗?

Var

  RecyclerView recycler_libretto;
  RecyclerAdapterLibretto adapter_libretto;
  LinearLayoutManager linearLayoutManager;
  List<DataLibretto> data_libretto = new ArrayList<>();
  DBLibretto db;
  Cursor cursor_db;
  SharedPreferences preferences;
  String orderByColumn; //retun value from class getValuePreferences
  ........

  @Override
  protected void onResume() {
    super.onResume();

    db.read();
    populateRecyclerLibretto();

  }

读取数据库

public DBLibretto read() throws SQLException {
    db = dbLibrettoHelper.getReadableDatabase();
    return this;
  }

型号

public class DataLibretto {



  private int id;
  private String codice;
  private String esame;
  private String modo;
  private String anno;
  private String voto;
  private String lode;
  private String lode_ctrl;
  private String crediti;
  private String docente;
  private String data;
  private String note;






  public int getId(){
    return id;
  }

  public String getCodice(){
    return codice;
  }

  public String getEsame(){
    return esame;
  }

  public String getModo(){
    return modo;
  }

  public String getAnno(){
    return anno;
  }

  public String getVoto(){
    return voto;
  }

  public String getLode(){
    return lode;
  }

  public String getLode_ctrl(){
    return lode_ctrl;
  }

  public String getCrediti(){
    return crediti;
  }

  public String getDocente(){
    return docente;
  }

  public String getData(){
    return data;
  }

  public String getNote(){
    return note;
  }






  public void setId(int id){
    this.id = id;
  }

  public void setCodice(String codice){
    this.codice = codice;
  }

  public void setEsame(String esame){
    this.esame = esame;
  }

  public void setModo(String modo){
    this.modo = modo;
  }

  public void setAnno(String anno){
    this.anno = anno;
  }

  public void setVoto(String voto){
    this.voto = voto;
  }

  public void setLode(String lode){
    this.lode = lode;
  }

  public void setLodeCTRL(String lode_ctrl){
    this.lode_ctrl = lode_ctrl;
  }

  public void setCrediti(String crediti){
    this.crediti = crediti;
  }

  public void setDocente(String docente){
    this.docente = docente;
  }

  public void setData(String data){
    this.data = data;
  }

  public void setNote(String note){
    this.note = note;
  }




}

populateList

 private void populateRecyclerLibretto() {



        data_libretto.clear();




        switch (orderByColumn) {

          case "_id":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderById();
            }


            break;

          case "esame ASC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByEsameASC();
            }

            break;

          case "esame DESC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByEsameDESC();
            }

            break;

          case "data ASC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, " SUBSTR (data, 7, 10) ASC, SUBSTR(data, 4, 2) ASC, SUBSTR(data, 1, 2) ASC ");
            } else {
              cursor_db = db.orderByDataASC();
            }

            break;

          case "data DESC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, " SUBSTR (data, 7, 10) DESC, SUBSTR(data, 4, 2) DESC, SUBSTR(data, 1, 2) DESC ");
            } else {
              cursor_db = db.orderByDataDESC();
            }

            break;

          case "anno ASC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByAnnoASC();
            }

            break;

          case "anno DESC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByAnnoDESC();
            }

            break;

          case "voto ASC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByVotoASC();
            }

            break;

          case "voto DESC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByVotoDESC();
            }

            break;


          case "crediti ASC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByCreditiASC();
            }

            break;

          case "crediti DESC":

            if (value_esami_media) {
              //cursorLoader = new CursorLoader(getApplicationContext(),
              //ProviderLibretto.CONTENT_URI, allColumns_libretto, "voto NOT LIKE '0' AND voto NOT LIKE '1'", null, orderByColumn);
            } else {
              cursor_db = db.orderByCreditiDESC();
            }

            break;

          default:
            break;

        }






        while (cursor_db.moveToNext()) {
            DataLibretto item = new DataLibretto();
            item.setId(cursor_db.getInt(0));
            item.setCodice(cursor_db.getString(1));
            item.setEsame(cursor_db.getString(2));
            item.setModo(cursor_db.getString(3));
            item.setAnno(cursor_db.getString(4));
            item.setVoto(cursor_db.getString(5));
            item.setLode(cursor_db.getString(6));
            item.setLodeCTRL(cursor_db.getString(7));
            item.setCrediti(cursor_db.getString(8));
            item.setDocente(cursor_db.getString(9));
            item.setData(cursor_db.getString(10));
            item.setNote(cursor_db.getString(11));

            data_libretto.add(item); //POPULATE LIST
          }

          //SET ADAPTER TO RECYCLERVIEW
          if((data_libretto.size() !=0)) {
            recycler_libretto.setAdapter(adapter_libretto);
          }

      }

RecyclerViewAdapter

class RecyclerAdapterLibretto extends RecyclerView.Adapter<RecyclerAdapterLibretto.ViewHolder> {


    Context context;
    List<DataLibretto> dataset_libretto;
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener; //ItemClickListener


    RecyclerAdapterLibretto(Context context, List<DataLibretto> data_libretto) {
      this.context = context;
      this.dataset_libretto = data_libretto;
    }


    //ItemClickListener
    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
      this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }


    //onCreateViewHolder
    @Override
    public ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
      View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_libretto_advanced, parent, false);
      return new RecyclerAdapterLibretto.ViewHolder(row);
    }


    //viewHolder
    class ViewHolder extends RecyclerView.ViewHolder{


      //rowAdvanced
      LinearLayout Row_andamento_left;
      TextView Row_id;
      TextView Row_esame;




      ViewHolder(View itemView) {
        super(itemView);




        //findViewById variabili
        Row_andamento_left = itemView.findViewById(R.id.andamento_left_libretto);

        Row_id = itemView.findViewById(R.id.row_id_libretto);
        Row_id.setTypeface(font_primary, Typeface.BOLD);

        Row_esame = itemView.findViewById(R.id.row_esame_libretto);
        Row_esame.setTypeface(font_primary, Typeface.BOLD);




        //ItemClickListener
        itemView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            if (onRecyclerViewItemClickListener != null) {
              onRecyclerViewItemClickListener.onItemClick(view, getAdapterPosition());
            }
          }
        });


      }




    }


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


      //id Debug
      holder.Row_id.setText(String.valueOf(dataset_libretto.get(position).getId()));


      String esame = dataset_libretto.get(position).getEsame();


    }


    //getItemCount
    @Override
    public int getItemCount() {
      return dataset_libretto.size();
    }


  }

dialogSpinner

int return_sort = picker_set_sort.getValue();


        switch (return_sort) {


          case 0: //default
            orderByColumn = "_id";
            populateRecyclerLibretto();
            putValuePreferences();
            break;

          case 1: //esame ASC
            orderByColumn = "esame ASC";
            populateRecyclerLibretto();
            putValuePreferences();
            break;
            .........

谢谢大家。

0 个答案:

没有答案