How to make data appear in recyclerView when updating data in SQLite database (Android Studio, SQLite)

  android-studio, c++, java, sqlite

When I fill the form it actually says ‘Update Successful!’, but when I go to the recyclerView nothing appears.

Here is my code for query

public Boolean updateData(String row_id, String firstname, String lastname, String age, String weight, String height)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put(COLUMN_FIRST_NAME, firstname);
    cv.put(COLUMN_LAST_NAME, lastname);
    cv.put(COLUMN_AGE, age);
    cv.put(COLUMN_WEIGHT, weight);
    cv.put(COLUMN_HEIGHT, height);

    long result = db.update(TABLE_PROFILE, cv, COLUMN_PROF_ID + " =? ", new String[]{row_id});

    if (result == -1)
    {
        Toast.makeText(context, "Failed to Update",  Toast.LENGTH_SHORT).show();
        return false;
    }
    else
    {
        Toast.makeText(context, "Successfully Updated!",  Toast.LENGTH_SHORT).show();
        return true;
    }

}

My Adapter

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

private Context context;
private ArrayList profile_id, profile_firstname, profile_lastname, profile_age,  profile_weight, profile_height;

CustomAdapter(Context context, ArrayList profile_id, ArrayList profile_firstname, ArrayList profile_lastname,
                ArrayList profile_age,  ArrayList profile_weight, ArrayList profile_height) {


    this.context = context;
    this.profile_id = profile_id;
    this.profile_firstname = profile_firstname;
    this.profile_lastname = profile_lastname;
    this.profile_age = profile_age;
    this.profile_weight = profile_weight;
    this.profile_height = profile_height;

}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.my_row, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

    holder.profile_id_txt.setText(String.valueOf(profile_id.get(position)));
    holder.profile_firstname_txt.setText(String.valueOf(profile_firstname.get(position)));
    holder.profile_lastname_txt.setText(String.valueOf(profile_lastname.get(position)));
    holder.profile_age_txt.setText(String.valueOf(profile_age.get(position)));
    holder.profile_weight_txt.setText(String.valueOf(profile_weight.get(position)));
    holder.profile_height_txt.setText(String.valueOf(profile_height.get(position)));

}

@Override
public int getItemCount() {

    return profile_id.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView profile_id_txt , profile_firstname_txt, profile_lastname_txt, profile_age_txt,  profile_weight_txt, profile_height_txt;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        profile_id_txt = (TextView) itemView.findViewById(R.id.profile_id_txt);
        profile_firstname_txt = (TextView) itemView.findViewById(R.id.profile_firstname_txt);
        profile_lastname_txt = (TextView) itemView.findViewById(R.id.profile_lastname_txt);
        profile_age_txt = (TextView) itemView.findViewById(R.id.profile_age_txt);
        profile_weight_txt = (TextView) itemView.findViewById(R.id.profile_weight_txt);
        profile_height_txt = (TextView) itemView.findViewById(R.id.profile_height_txt);

    }
}
}

Activity code with recyclerView

public class ProfileActivity extends AppCompatActivity {

RecyclerView recyclerView;
FloatingActionButton add_user, edit_profile;

DBHelper myDB;
ArrayList<String> profile_id, profile_firstname, profile_lastname, profile_age,  profile_weight, profile_height;
//ArrayList<String> user_id, user_name, user_password;
CustomAdapter customAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    recyclerView = findViewById(R.id.recyclerView);
    edit_profile = findViewById(R.id.edit_profile);


    edit_profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ProfileActivity.this, EditProfileActivity.class);

            intent.putExtra("stringFirst", String.valueOf(profile_firstname));
            intent.putExtra("stringLast", String.valueOf(profile_lastname));
            intent.putExtra("stringAge", String.valueOf(profile_age));
            intent.putExtra("stringWeight", String.valueOf(profile_weight));
            intent.putExtra("stringHeight", String.valueOf(profile_height));

            startActivity(intent);
        }
    });

    myDB = new DBHelper(ProfileActivity.this);

    profile_id = new ArrayList<>();
    profile_firstname = new ArrayList<>();
    profile_lastname = new ArrayList<>();
    profile_age = new ArrayList<>();
    profile_weight = new ArrayList<>();
    profile_height = new ArrayList<>();


    storeDataInArrays();

    customAdapter = new CustomAdapter(ProfileActivity.this, profile_id, profile_firstname, profile_lastname, profile_age,  profile_weight, profile_height);
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(ProfileActivity.this));

}

void storeDataInArrays() {
    Cursor cursor = myDB.readProfileInfo();  // Cursor cursor = myDB.readAllData();

    if (cursor.getCount() == 0)
    {
        Toast.makeText(ProfileActivity.this, "No Data", Toast.LENGTH_SHORT).show();
    }
    else
    {
        while (cursor.moveToNext()) {

            profile_id.add(cursor.getString(0));
            profile_firstname.add(cursor.getString(1));
            profile_lastname.add(cursor.getString(2));
            profile_age.add(cursor.getString(3));
            profile_weight.add(cursor.getString(4));
            profile_height.add(cursor.getString(5));


        }
    }
}
}

Activity code where user can edit/update the information

public class EditProfileActivity extends AppCompatActivity {

EditText firstname, lastname, age, weight, height;
Button confirm;

String Id, First, Last, Age, Weight, Height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_profile);

    firstname = findViewById(R.id.firstname_input);
    lastname = findViewById(R.id.lastname_input);
    age = findViewById(R.id.age_input);
    weight = findViewById(R.id.weight_input);
    height = findViewById(R.id.height_input);

    confirm = findViewById(R.id.profile_infoBtn);

    // first we call this
    //getAndSetIntentData();

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

            String name = firstname.getText().toString().trim();
            String last = lastname.getText().toString().trim();
            String old = age.getText().toString().trim();
            String kg = weight.getText().toString().trim();
            String cm = height.getText().toString().trim();

            if (name.isEmpty())
            {
                firstname.setError("First Name is required!");
                firstname.requestFocus();
                return;
            }
            if (last.isEmpty())
            {
                lastname.setError("Last Name is required!");
                lastname.requestFocus();
                return;
            }
            if (old.isEmpty())
            {
                age.setError("Age is required!");
                age.requestFocus();
                return;
            }
            if (kg.isEmpty())
            {
                weight.setError("Weight is required!");
                weight.requestFocus();
                return;
            }
            if (cm.isEmpty())
            {
                height.setError("Height is required!");
                height.requestFocus();
                return;
            }

            if ( Integer.parseInt(old) < 1 || Integer.parseInt(old) > 99 )
            {
                age.setError("Please enter valid age");
                age.requestFocus();
                return;
            }

            if (Integer.parseInt(kg) < 10)
            {
                weight.setError("Please enter valid weight");
                weight.requestFocus();
                return;
            }

            DBHelper myDB = new DBHelper(EditProfileActivity.this);
            First = firstname.getText().toString().trim();
            Last = lastname.getText().toString().trim();
            Age = age.getText().toString().trim();
            Weight = weight.getText().toString().trim();
            Height = height.getText().toString().trim();

            myDB.updateData(Id, First, Last, Age,  Weight, Height);

            Intent intent = new Intent(EditProfileActivity.this, ProfileActivity.class);
            startActivity(intent);
            //recreate();



        }
    });

}

void getAndSetIntentData() {
    if (getIntent().hasExtra("id") && getIntent().hasExtra("first") && getIntent().hasExtra("last") && getIntent().hasExtra("age")
            && getIntent().hasExtra("weight") && getIntent().hasExtra("height"))
    {
        // Getting data from Intent
        First = getIntent().getStringExtra("First");
        Last = getIntent().getStringExtra("Last");
        Age = getIntent().getStringExtra("Age");
        Weight = getIntent().getStringExtra("Weight");
        Height = getIntent().getStringExtra("Height");

        // Setting Intent Data
        firstname.setText(First);
        lastname.setText(Last);
        age.setText(Age);
        weight.setText(Weight);
        height.setText(Height);

    }
    else
    {
        Toast.makeText(EditProfileActivity.this, "No Data", Toast.LENGTH_SHORT).show();
    }
}

}

I apologise if I posted too much code. It’s my first time in stackoverflow.

Source: Windows Questions C++

LEAVE A COMMENT