我如何在更改SQLite中的数据的同时更改recycleview的内容?

时间:2018-07-31 09:52:56

标签: android-studio android-sqlite recycler-adapter

我在recycleview中有一个按钮(cardview的一部分,每次插入都会生成该按钮),该按钮会在SQLite中使用id递增特定的列,数据会更改,但我必须重新启动活动才能更改它。 更改后的数据必须显示在文本视图中。

这是我的新手,如何使它们动态变化?

var x = XElement.Parse(@"<Root type=""1"">
                            <A></A>
                            <B>
                                <B1>
                                    <B12/>
                                    <B13/>
                                </B1>
                                <B2>
                                    123
                                    <B21></B21>
                                </B2>
                               <B3 type=""3"">
                                   <B4/>
                               </B3>
                            </B>
                            <C/>
                            </Root>");

foreach(XElement child in x.Descendants().Reverse())
{
    if(!child.HasElements && string.IsNullOrEmpty(child.Value) && !child.HasAttributes) 
        child.Remove();
}

我的活动:

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

    private List<attendence> mattendence_list;
    private Context mcontext;
    public RecyclerView mrecycleview;
    @NonNull
    @Override
    public attendence_recycleadapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater= LayoutInflater.from(parent.getContext());
        View v=inflater.inflate(R.layout.attendence_recycleview_card,parent,false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull attendence_recycleadapter.ViewHolder holder, final int position) {
        final attendence attendence= mattendence_list.get(position);

        holder.subname.setText("NAME: " + attendence.getSubname());
        holder.subcredit.setText("CREDIT: " + attendence.getCredit());
        holder.bunks.setText("BUNKS: " + attendence.getBunks());
        holder.deletesub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Myhelper myhelper = new Myhelper(mcontext);
               myhelper.delete(attendence.getId(),mcontext);
               notifyItemRemoved(position);
               notifyItemRangeChanged(position,mattendence_list.size());
               notifyDataSetChanged();
                delete(position);
            }
        });
        holder.addbunk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Myhelper myhelper = new Myhelper(mcontext);
              myhelper.updatebunk(attendence.getId());
                Toast.makeText(mcontext,"+1 class bunked",Toast.LENGTH_SHORT).show();


            }
        });
        holder.deletebunk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Myhelper myhelper=new Myhelper(mcontext);
                myhelper.updatebunkdelete(attendence.getId());
                Toast.makeText(mcontext,"Bunk deleted",Toast.LENGTH_SHORT).show();
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView subname;
        public TextView subcredit;
        public TextView bunks;
        public View layout;
        public TextView addbunk;
public TextView deletebunk;
        public TextView deletesub;
        public ViewHolder(View itemView) {
            super(itemView);
            layout=itemView;
            subname=(TextView)itemView.findViewById(R.id.attendernce_subname);
            subcredit=(TextView)itemView.findViewById(R.id.attendernce_credit);
            bunks=(TextView)itemView.findViewById(R.id.attendernce_bunks);
deletebunk =(TextView)itemView.findViewById(R.id.attendence_deletebunk);
            addbunk = (TextView) itemView.findViewById(R.id.attendence_addbunk);
            deletesub = (TextView) itemView.findViewById(R.id.attendence_deletesub);

        }
    }
    public void add(int position,attendence attendence2){
        mattendence_list.add(position,attendence2);
        notifyItemInserted(position);
    }
    public  void delete(int position){
        mattendence_list.remove(position);
        notifyItemRemoved(position);
    }
    public attendence_recycleadapter(List<attendence> mydataset, Context context, RecyclerView recyclerView){
        mattendence_list =  mydataset;
        mcontext = context;
        mrecycleview= recyclerView;
    }


}

mysqlitehelper:

public class Manage_attendence extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private attendence_recycleadapter attendence_recycleadapter;
    String filter="";
    TextView add_subject;
    Dialog mycustomDialog;
    Myhelper myhelper;
    SQLiteDatabase sqLiteDatabase;
    Spinner nema_of_credit;
    EditText name_of_subject;
    String spinner_string = new String();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_attendence);
        add_subject=(TextView)findViewById(R.id.add_subject_dialog);
        mycustomDialog = new Dialog(this);
        myhelper = new Myhelper(this);
        sqLiteDatabase = myhelper.getWritableDatabase();
        add_subject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupDialog();
            }
        });
        recyclerView= (RecyclerView)findViewById(R.id.attendence_recycleview);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        attendence_recycleadapter= new attendence_recycleadapter(myhelper.list(filter),this,recyclerView);
        recyclerView.setAdapter(attendence_recycleadapter);
        name_of_subject = (EditText) mycustomDialog.findViewById(R.id.name_of_subject);
        nema_of_credit = (Spinner) mycustomDialog.findViewById(R.id.subject_credit);


    }

    public void setupDialog() {

        mycustomDialog.setContentView(R.layout.dialog_add_subject);
        TextView close = (TextView) mycustomDialog.findViewById(R.id.subject_delete);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mycustomDialog.dismiss();
            }
        });
     exqButton();
        mycustomDialog.setCancelable(false);
        mycustomDialog.show();
        mycustomDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void exqButton() {
        TextView add =(TextView)mycustomDialog.findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name_of_subject = (EditText) mycustomDialog.findViewById(R.id.name_of_subject);
                nema_of_credit = (Spinner) mycustomDialog.findViewById(R.id.subject_credit);
                String xValues=String.valueOf(name_of_subject.getText());
                spinner_string=nema_of_credit.getSelectedItem().toString();
                String yValues= ((String.valueOf(spinner_string)));


                boolean insertData = myhelper.insertData(xValues,yValues,0);

                if (insertData==true){
                    Toast.makeText(Manage_attendence.this,"data inserted sucessfully",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(Manage_attendence.this,"data inserted failed",Toast.LENGTH_SHORT).show();
                }
                name_of_subject.setText("");
                mycustomDialog.dismiss();

            }
        });
    }


}

1 个答案:

答案 0 :(得分:0)

更改updatebunk方法:

public int updatebunk(long id){
    SQLiteDatabase db = this.getWritableDatabase();
    int bunk = getbunk(id);
    int bunkinc= ++bunk;
    ContentValues contentValues= new ContentValues();
    contentValues.put(COL_3, bunkinc);
    db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
    db.close();

    return bunkinc;
}

然后在您的适配器中更改addbunk的侦听器

    holder.addbunk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Myhelper myhelper = new Myhelper(mcontext);
            int newbunk = myhelper.updatebunk(attendence.getId());
            attendence.setBunks(newbunk);
            mattendence_list.set(position, attendence);
            notifyDataSetChanged();
            Toast.makeText(mcontext,"+1 class bunked",Toast.LENGTH_SHORT).show();
        }
    });