Home > Software design >  How to show app default image and user selected image at a same time in recyclerView
How to show app default image and user selected image at a same time in recyclerView

Time:12-27

I am creating an Contact app with SQLITE. If anyone want to store a person contact, he will fill up the name, number and person image. If he does not select an image, the app will set default image. But how i can show these two types of picture i mean selected image and default image because my app show only default image.

Database

`public class DatabaseHelper extends SQLiteOpenHelper {

final static String DB_NAME = "contactList.db", TABLE_NAME = "contactDetails";
final static int VERSION = 1;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("create table contactDetails "  
            "(id integer primary key autoincrement, "  
            "defaultIMG int, "  
            "uploadIMG text, "  
            "name text, "  
            "number text)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}




public ArrayList<ImageNameNumberModel> getContacts() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<ImageNameNumberModel> contacts = new ArrayList<>();

    Cursor cursor = db.rawQuery("SELECT * FROM contactDetails", null);

    if (cursor.moveToFirst()) {
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            int defIMG = cursor.getInt(1);
            String upIMG = cursor.getString(2);
            String name = cursor.getString(3);
            String number = cursor.getString(4);

            ImageNameNumberModel model = new ImageNameNumberModel(id, defIMG,
                    Uri.parse(upIMG), name, number);
            contacts.add(model);
        }
    }


    return contacts;
}


public void insert(int defaultIMG, Uri uploadIMG, String name, String number) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("defaultIMG", defaultIMG);
    contentValues.put("uploadIMG", String.valueOf(uploadIMG));
    contentValues.put("name", name);
    contentValues.put("number", number);

    db.insert(TABLE_NAME, null, contentValues);
}

} `

RecyclerView

public class ImageNameNumberAdapter extends RecyclerView.Adapter<ImageNameNumberAdapter.viewHolder>{

Context context;
ArrayList<ImageNameNumberModel> imageNameNumberModels;

public ImageNameNumberAdapter(Context context, ArrayList<ImageNameNumberModel> imageNameNumberModels) {
    this.context = context;
    this.imageNameNumberModels = imageNameNumberModels;
}

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

@Override
public void onBindViewHolder(@NonNull ImageNameNumberAdapter.viewHolder holder, int position) {
    ImageNameNumberModel inm = imageNameNumberModels.get(position);


    //Help here 


    holder.userName.setText(inm.getName());
    holder.userNumber.setText(inm.getNumber());
}

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

public class viewHolder extends RecyclerView.ViewHolder {
    ImageView userIMG;
    TextView userName, userNumber;
    public viewHolder(@NonNull View itemView) {
        super(itemView);
        userIMG = itemView.findViewById(R.id.imageSampleContact);
        userName = itemView.findViewById(R.id.nameSampleContact);
        userNumber = itemView.findViewById(R.id.numberSampleContact);
    }
}

}

AddContactActivity.java

public class AddContact extends AppCompatActivity {

ActivityAddContactBinding binding;
Uri uploadIMG = null;
public static boolean UPLOAD = false;
public static int DEFAULT_IMG = R.drawable.noimgae;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityAddContactBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    addImage();
    saveContact();
}


private void saveContact() {
    binding.contactSaveButtonAddContactActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(AddContact.this, MainActivity.class);
            DatabaseHelper dbh = new DatabaseHelper(AddContact.this);
            if (UPLOAD) {
                DEFAULT_IMG = 0;
                dbh.insert(DEFAULT_IMG, uploadIMG, binding.contactNameAddContactActivity.getText().toString(), binding.contactPhoneNumberAddContactActivity.getText().toString());
                DEFAULT_IMG = R.drawable.noimgae;
                UPLOAD = false;
            }
            else {
                dbh.insert(DEFAULT_IMG, uploadIMG, binding.contactNameAddContactActivity.getText().toString(), binding.contactPhoneNumberAddContactActivity.getText().toString());
            }
            startActivity(intent);
            finish();
        }
    });
}


private void addImage() {
    binding.contactImageAddContactActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ImagePicker.with(AddContact.this)
                    .galleryOnly()
                    .crop()
                    .compress(1024)
                    .maxResultSize(1080, 1080)
                    .start();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        uploadIMG = data.getData();
        UPLOAD = true;
        binding.contactImageAddContactActivity.setImageURI(uploadIMG);
    }
}

}

CodePudding user response:

All you need to do is check if the ImageNameNumberModel contains uploadImage path. If it has then load the selected image from disk else load the default image from resources.

For Image loading in android you can use any of the common image handling libraries like Glide, Picasso etc

CodePudding user response:

check like this

 If(!ImageNameNumberModel[position].uploadImage.isNullOrEmpty() $$ 
         !ImageNameNumberModel[position].uploadImage=="null"){
     
Glide.with(context).load(ImageNameNumberModel[position].uploadImage).into(imageView)

}else{
   imageView.setBackgroundResource(R.drawable.image)
}

Note:- Here User as per your model class and image view please use glide library also

Basically above code do check if your model class have an image URL or not if yes then set that URL image to your image view else default one

  • Related