单击复选框时显示Edittext和Textview

时间:2018-10-17 17:06:17

标签: java android android-recyclerview android-room

我使用Room Database在Android中创建具有CRUD功能的应用程序。 当我像这样选中复选框时,Error Pictures 1我希望该应用程序显示textview和edittext。但是,在我按下“下一步”按钮后,它不会基于复选框Error Pictures 2中的选中状态显示textview和edittext,或者显示的不是textview&edittext,但不是基于isChecked复选框

这是代码:

AdapterTargetIbadah.java

package skripsi.com.ggr.managementibadah.adapter;

import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import skripsi.com.ggr.managementibadah.R;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;

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

    private ArrayList<Ibadah> targetIbadah;
    private Context context;
    private AppDatabase db;

    public AdapterTargetIbadah(ArrayList<Ibadah> targets, Context ctx) {
        targetIbadah = targets;
        context = ctx;

        db = Room.databaseBuilder(context.getApplicationContext(),
                AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_target;
        EditText et_Target;

        ViewHolder(View itemView) {
            super(itemView);
            tv_target = itemView.findViewById(R.id.tv_target);
            et_Target = itemView.findViewById(R.id.et_Target);
        }
    }

    @Override
    public AdapterTargetIbadah.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_target, parent, false);
        AdapterTargetIbadah.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterTargetIbadah.ViewHolder holder, int position) {
        Ibadah iba = targetIbadah.get(position);
        if (iba.getTargetIbadah()!=null) {
            holder.tv_target.setText(iba.getTargetIbadah());
            iba.setEtTarget(holder.et_Target.getText().toString());
        } else {
            targetIbadah.remove(position);
           }
        }

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

AdapterCheckboxTarget.java

package skripsi.com.ggr.managementibadah.adapter;

import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.annotation.NonNull;
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.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import skripsi.com.ggr.managementibadah.R;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;

public class AdapterCheckboxTarget extends RecyclerView.Adapter<AdapterCheckboxTarget.ViewHolder> {
    private ArrayList<Ibadah> targetIbadah;
    private Context context;
    private AppDatabase db;

    public AdapterCheckboxTarget(ArrayList<Ibadah> targets, Context ctx) {
        targetIbadah = targets;
        context = ctx;

        db = Room.databaseBuilder(context.getApplicationContext(),
                AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbTarget;

        ViewHolder(View itemView) {
            super(itemView);
            cbTarget = itemView.findViewById(R.id.cbTarget);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_targethome, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final String name = targetIbadah.get(position).getNamaIbadah();
        holder.cbTarget.setText(name);
        holder.cbTarget.setChecked(targetIbadah.get(position).getStatus());
        holder.cbTarget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.cbTarget.isChecked()) {
                    targetIbadah.get(position).setStatus(true);
                    targetIbadah.get(position).setTargetIbadah(targetIbadah.get(position).getNamaIbadah());
                    Toast.makeText(context, "checklist", Toast.LENGTH_SHORT).show();
                } else {
                    targetIbadah.get(position).setStatus(false);
                    Toast.makeText(context, "unchecklist", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

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

TargetHome.java ==> 使用AdapterTargetIbadah.java的活动

package skripsi.com.ggr.managementibadah;

import android.app.Activity;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import skripsi.com.ggr.managementibadah.adapter.AdapterCheckboxTarget;
import skripsi.com.ggr.managementibadah.adapter.AdapterTargetIbadah;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;

public class TargetHome extends AppCompatActivity {
    private AppDatabase db;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<Ibadah> targetIbadah;
    RecyclerView rvTarget;
    Button btSimpan;
    Context ctx;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.target_ibadah);
        setTitle("Target Ibadah");

        btSimpan = (Button)findViewById(R.id.btSimpanTarget);
        targetIbadah = new ArrayList<>();
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
        rvTarget = findViewById(R.id.rvTar);
        rvTarget.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        rvTarget.setLayoutManager(layoutManager);
        targetIbadah.addAll(Arrays.asList(db.ibadahDAO().selectAllStatus()));


        adapter = new AdapterTargetIbadah(targetIbadah, this);
        rvTarget.setAdapter(adapter);


        Ibadah ib = new Ibadah();
        if (ib.getTargetIbadah()!=null) {
            ib.setTvTarget(ib.getTargetIbadah());
            ib.setEtTarget("");
            db.ibadahDAO().insertCapai(ib);
        }
        btSimpan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MainActivity.getActIntent(TargetHome.this));
            }
        });
    }

    public static Intent getActIntent(Activity activity) {
        // kode untuk pengambilan Intent
        return new Intent(activity, TargetHome.class);
    }
}

CheckboxTarget.java ==> 使用AdapterCheckboxTarget的活动

package skripsi.com.ggr.managementibadah;

import android.app.Activity;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import skripsi.com.ggr.managementibadah.adapter.AdapterCatatIbadah;
import skripsi.com.ggr.managementibadah.adapter.AdapterCheckboxTarget;
import skripsi.com.ggr.managementibadah.data.factory.AppDatabase;
import skripsi.com.ggr.managementibadah.model.Ibadah;

public class CheckboxTarget extends AppCompatActivity {
    private AppDatabase db;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<Ibadah> targetIbadah;
    RecyclerView rvCb;
    Button btNext;
    Context ctx;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.target_home);
        setTitle("Target Ibadah");

        btNext = (Button) findViewById(R.id.btNextTarget);
        targetIbadah = new ArrayList<>();
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "ibadahdb").allowMainThreadQueries().build();
        rvCb = findViewById(R.id.rvTarHome);
        rvCb.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        rvCb.setLayoutManager(layoutManager);
        targetIbadah.addAll(Arrays.asList(db.ibadahDAO().selectAllStatus()));

        adapter = new AdapterCheckboxTarget(targetIbadah, this);
        rvCb.setAdapter(adapter);

        final Ibadah ibadah = (Ibadah) getIntent().getSerializableExtra("data");
        if (ibadah != null) {
            Ibadah ib = new Ibadah();
            ib.setStatus(ib.status);
            db.ibadahDAO().insertTarget(ib);
        }
    }

    public static Intent getActIntent(Activity activity) {
        // kode untuk pengambilan Intent
        return new Intent(activity, CheckboxTarget.class);
    }
}

Ibadah.java ==>模型

package skripsi.com.ggr.managementibadah.model;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

import java.io.Serializable;

@Entity(tableName = "tibadah")
public class Ibadah implements Serializable {

    @PrimaryKey(autoGenerate = true)
    public int idIbadah;

    @ColumnInfo(name = "nama_ibadah")
    public String namaIbadah;

    @ColumnInfo(name = "deskripsi_ibadah")
    public String deskripsiIbadah;

    @ColumnInfo(name = "target_ibadah")
    public String targetIbadah;

    @ColumnInfo(name = "tv_target")
    public String tvTarget;

    @ColumnInfo(name = "et_target")
    public String etTarget;

    @ColumnInfo(name = "et_capaian")
    public String etCapaian;

    @ColumnInfo(name = "status")
    public boolean status;

    public String getTvTarget() {
        return tvTarget;
    }

    public void setTvTarget(String tvTarget) {
        this.tvTarget = tvTarget;
    }

    public String getTargetIbadah() {
        return targetIbadah;
    }

    public void setTargetIbadah(String targetIbadah) {
        this.targetIbadah = targetIbadah;
    }

    public String getEtTarget() {
        return etTarget;
    }

    public void setEtTarget(String etTarget) {
        this.etTarget = etTarget;
    }

    public String getEtCapaian() {
        return etCapaian;
    }

    public void setEtCapaian(String etCapaian) {
        this.etCapaian = etCapaian;
    }

    public String getDeskripsiIbadah() {
        return deskripsiIbadah;
    }

    public void setDeskripsiIbadah(String deskripsiIbadah) {
        this.deskripsiIbadah = deskripsiIbadah;
    }

    public int getIdIbadah() {
        return idIbadah;
    }

    public void setIdIbadah(int idIbadah) {
        this.idIbadah = idIbadah;
    }

    public String getNamaIbadah() {
        return namaIbadah;
    }

    public void setNamaIbadah(String namaIbadah) {
        this.namaIbadah = namaIbadah;
    }

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }
}

我希望你能帮助我。非常感谢您,我的英语不好。

1 个答案:

答案 0 :(得分:0)

首先,您必须通过将TextView添加到EditTextandroid:visibility="invisible"的XML中来设置TextViewEditText在XML中不可见。 / p>

然后,您必须在设置复选框后使用setVisibility(View.VISIBLE),如下所示:

yourcheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
               //if checkbox is checked (b == true)
               yourTextView.setVisibility(View.VISIBLE); 
               yourEditText.setVisibility(View.VISIBLE); 
            }
            if (!b) {
               //if checkbox is not checked (b == false)
               yourTextView.setVisibility(View.INVISIBLE); 
               yourEditText.setVisibility(View.INVISIBLE); 
            }
        }
    });

如果您首先要单击button,然后检查天气checkbox,请将该天气添加到您的OnClickListenter

            if (yourcheckbox.isChecked) {
               yourTextView.setVisibility(View.VISIBLE); 
               yourEditText.setVisibility(View.VISIBLE); 
            }