简体   繁体   中英

How to set id for card view?

I have created list of time tables. I have used card view and recycler view for this. Now I have set id for card view to link it with records of database.

But Id for card is not getting set properly. If I try to delete the record. Some are getting delete and some dose not get delete.

TableListAdapter:

    public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {


    public static TimeTableHelper db;
    public static TimeTableList timeTableList;

    public static int cardId,id;
    public static boolean editMode;
    private List<TimeTable> tableList;

    public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
        this.tableList = tableList;
        this.timeTableList = timeTableList;
    }
    private Context context;

    public TableListAdapter(Context context) {
        this.context = context;
    }
    @Override
    public int getItemCount() {
          return tableList.size();
    }

    @Override
    public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {

        TimeTable ci = tableList.get(i);
        contactViewHolder.tableTitle.setText(ci.getTitle());
        contactViewHolder.color.setBackgroundColor(ci.getTableColor());

   }

   @Override
   public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

       cardId++;
        View itemView = LayoutInflater.
                    from(viewGroup.getContext()).
                    inflate(R.layout.table_card, viewGroup, false);

        return new TableViewHolder(itemView);
   }

  public static class TableViewHolder extends RecyclerView.ViewHolder {

      protected TextView tableTitle;
      protected CardView cv;
      protected SwitchCompat aSwitch;
      protected Button color;
      private int status =0;


      public TableViewHolder(View v) {
          super(v);
          tableTitle = (TextView) v.findViewById(R.id.tableTitle);
          cv = (CardView) v.findViewById(R.id.card_view);
          aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
          color = (Button) v.findViewById(R.id.selectColor);

          db = new TimeTableHelper(timeTableList);


          cv.setId(cardId);


          Log.d("cardId", String.valueOf(cardId));

          cv.setOnLongClickListener(new View.OnLongClickListener() {

              @Override
              public boolean onLongClick(View v) {
                  // TODO Auto-generated method stub

                  final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);

                  builder.setTitle("Delete entry")
                          .setMessage("Are you sure you want to Delete this Time Table?")
                          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {

                                  id = cv.getId();
                                  TimeTable t = new TimeTable();
                                  t = db.getTable(id);
                                  db.deleteTable(t);


                                  Intent i = new Intent(timeTableList,TimeTableList.class);
                                  timeTableList.startActivity(i);


                              }

                          })


                          .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  // do nothing
                              }
                          })

                          .setIcon(R.drawable.ic_warning_black_36dp)
                          .show();
                  return true;
              }

          });



          cv.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {

                  id = cv.getId();
                  editMode = true;
                  Intent i = new Intent(timeTableList, NewTimeTable.class);
                  i.putExtra("editMode", editMode);
                  i.putExtra("tableId", id);
                  timeTableList.startActivity(i);
              }
          });

          aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                  id = cv.getId();

                  if (isChecked) {

                      status = 1;

                      TimeTable t = new TimeTable();
                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);

                      Log.d("status", String.valueOf(status));

                      final List<TimeTable> events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }

                  } else {

                      status = 0;

                      TimeTable t = new TimeTable();
                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);
                      final List<TimeTable> events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }
                      Log.d("status", String.valueOf(status));
                  }

              }
          });

      }
  }

    public void updateAdapaterList(List<TimeTable> newTimeTableList) {
        //Replace the current list with new list
        this.tableList = newTimeTableList;
        //notify the adapter that the data set has changed
        notifyDataSetChanged();
    }
}

What's going wrong??

Set up a tag to your card view and get the tag when someone clicks on it. Try doing something like this

public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {


public static TimeTableHelper db;
public static TimeTableList timeTableList;

public static int cardId,id;
public static boolean editMode;
private List<TimeTable> tableList;

public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
    this.tableList = tableList;
    this.timeTableList = timeTableList;
}
private Context context;

public TableListAdapter(Context context) {
    this.context = context;
}
@Override
public int getItemCount() {
      return tableList.size();
}

@Override
public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {
    contactViewHolder.cv.setTag(i);
    TimeTable ci = tableList.get(i);
    contactViewHolder.tableTitle.setText(ci.getTitle());
    contactViewHolder.color.setBackgroundColor(ci.getTableColor());
}

@Override
 public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)  {  

   cardId++;
    View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.table_card, viewGroup, false);

    return new TableViewHolder(itemView);
 }

public static class TableViewHolder extends RecyclerView.ViewHolder {

  protected TextView tableTitle;
  protected CardView cv;
  protected SwitchCompat aSwitch;
  protected Button color;
  private int status =0;


  public TableViewHolder(View v) {
      super(v);
      tableTitle = (TextView) v.findViewById(R.id.tableTitle);
      cv = (CardView) v.findViewById(R.id.card_view);
      aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
      color = (Button) v.findViewById(R.id.selectColor);

      db = new TimeTableHelper(timeTableList);

      cv.setOnLongClickListener(new View.OnLongClickListener() {

          @Override
          public boolean onLongClick(View v) {
              // TODO Auto-generated method stub
              id = (int) v.getTag();

              final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);

              builder.setTitle("Delete entry")
                      .setMessage("Are you sure you want to Delete this Time Table?")
                      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              TimeTable t = new TimeTable();
                              t = db.getTable(id);
                              db.deleteTable(t);


                              Intent i = new Intent(timeTableList,TimeTableList.class);
                              timeTableList.startActivity(i);


                          }

                      })


                      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              // do nothing
                          }
                      })

                      .setIcon(R.drawable.ic_warning_black_36dp)
                      .show();
              return true;
          }

      });



      cv.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

              id = (int) v.getTag();
              editMode = true;
              Intent i = new Intent(timeTableList, NewTimeTable.class);
              i.putExtra("editMode", editMode);
              i.putExtra("tableId", id);
              timeTableList.startActivity(i);
          }
      });
      //And your other stuff

Instead of CardView, I used total view for click Listener, You can try it with cv but first check with view.

Just rearranged your code-

public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {


    public static TimeTableHelper db;
    public static TimeTableList timeTableList;

    public static int cardId,id;
    public static boolean editMode;
    private List<TimeTable> tableList;

    public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
        this.tableList = tableList;
        this.timeTableList = timeTableList;
    }
    private Context context;

    public TableListAdapter(Context context) {
        this.context = context;
    }
    @Override
    public int getItemCount() {
          return tableList.size();
    }


   @Override
   public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

       cardId++;
        View itemView = LayoutInflater.
                    from(viewGroup.getContext()).
                    inflate(R.layout.table_card, viewGroup, false);             



        return new TableViewHolder(itemView);
   }

    @Override
    public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {

        TimeTable ci = tableList.get(i);
        contactViewHolder.tableTitle.setText(ci.getTitle());
        contactViewHolder.color.setBackgroundColor(ci.getTableColor());
        contactViewHolder.view.setOnLongClickListener(new View.OnLongClickListener() {

              @Override
              public boolean onLongClick(View v) {
                  // TODO Auto-generated method stub

                  final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);

                  builder.setTitle("Delete entry")
                          .setMessage("Are you sure you want to Delete this Time Table?")
                          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {

                                  id = cv.getId();
                                  TimeTable t = new TimeTable();
                                  t = db.getTable(id);
                                  db.deleteTable(t);
                                  notifyDataSetChanged();

                                  Intent i = new Intent(timeTableList,TimeTableList.class);
                                  timeTableList.startActivity(i);


                              }

                          })


                          .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  // do nothing
                              }
                          })

                          .setIcon(R.drawable.ic_warning_black_36dp)
                          .show();
                  return true;
              }

          });

          contactViewHolder.view.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {

                  id = cv.getId();
                  editMode = true;
                  Intent i = new Intent(timeTableList, NewTimeTable.class);
                  i.putExtra("editMode", editMode);
                  i.putExtra("tableId", id);
                  timeTableList.startActivity(i);
              }
          });

          contactViewHolder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                  id = cv.getId();

                  if (isChecked) {

                      status = 1;

                      TimeTable t = new TimeTable();
                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);

                      Log.d("status", String.valueOf(status));

                      final List<TimeTable> events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }

                  } else {

                      status = 0;

                      TimeTable t = new TimeTable();
                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);
                      final List<TimeTable> events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }
                      Log.d("status", String.valueOf(status));
                  }

              }
          });



           contactViewHolder.view.

   }
 }


  public static class TableViewHolder extends RecyclerView.ViewHolder {

      protected TextView tableTitle;
      protected CardView cv;
      protected SwitchCompat aSwitch;
      protected Button color;
      private int status =0;
      private View view;


      public TableViewHolder(View v) {
          super(v);
          tableTitle = (TextView) v.findViewById(R.id.tableTitle);
          cv = (CardView) v.findViewById(R.id.card_view);
          aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
          color = (Button) v.findViewById(R.id.selectColor);
          view = v;
          db = new TimeTableHelper(timeTableList);
          cv.setId(cardId);     



      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM