How to add image to arrayList in recyclerview - java

I'm trying to add an image to a recyclerview after getting the image from either the gallery or the camera. Below is the code that I have.
Main.java
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
itemImage = findViewById(R.id.image_holder);
itemName = findViewById(R.id.add_item_name);
itemPrice = findViewById(R.id.add_price);
itemDesc = findViewById(R.id.add_desc);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
addPhotoButton = findViewById(R.id.getCameraBtn);
addFileButton = findViewById(R.id.getGalleryBtn);
imageView = findViewById(R.id.image_holder);
addPhotoButton.setOnClickListener(this);
addFileButton.setOnClickListener(this);
itemList = new ArrayList<Item>();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new Adapter(itemList);
adapter.notifyItemInserted(itemList.size()-1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
// log out back to start page
public void goToStart(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.getCameraBtn:
//post a photo from the camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, CAMERA_CODE);
break;
case R.id.getGalleryBtn:
//post an image
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*"); //anything that is image related
startActivityForResult(galleryIntent, GALLERY_CODE);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_CODE && resultCode == RESULT_OK) {
if (data != null) {
imageUri = data.getData(); //we have the actual path
imageView.setImageURI(imageUri); //show image
}
} else if (requestCode == CAMERA_CODE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
public void addItem(View view){
itemList.add(new Item(R.drawable.ic_logo, itemName.getText().toString(), itemPrice.getText().toString(), itemDesc.getText().toString()));
adapter = new Adapter(itemList);
recyclerView.setAdapter(adapter);
}
Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private ArrayList<Item> ItemList;
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView item_image;
public TextView item_name;
public TextView item_desc;
public TextView item_price;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.item_image);
item_name = itemView.findViewById(R.id.item_name);
item_desc = itemView.findViewById(R.id.desc);
item_price = itemView.findViewById(R.id.price);
}
}
public Adapter(ArrayList<Item> itemList) {ItemList = itemList;}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Item currentItem = ItemList.get(position);
holder.item_image.setImageResource(currentItem.getItemImage());
holder.item_name.setText(currentItem.getItemName());
holder.item_desc.setText(currentItem.getItemDesc());
holder.item_price.setText(currentItem.getItemPrice());
}
#Override
public int getItemCount() {
return ItemList.size();
}
}
Item.java
public class Item {
private int itemImage;
private String itemName;
private String itemDesc;
private String itemPrice;
public Item(int itemImage, String itemName, String itemDesc, String itemPrice){
this.itemImage = itemImage;
this.itemName = itemName;
this.itemDesc = itemDesc;
this.itemPrice = itemPrice;
}
public int getItemImage(){return itemImage;}
public String getItemName(){return itemName;}
public String getItemDesc(){return itemDesc;}
public String getItemPrice(){return itemPrice;}
}
I am able to get the image from the camera or gallery and display it on an image view. The problem is, I don't really know how I could add this image to the arrayList and the recyclerview. All examples I found were showing how to add images from the drawable file which is not what I'm looking for. Thank you in advance.

Related

Delete item in RecyclerView works only once

When I delete an item from the RecyclerView, I am no longer able to delete another item. When I make an item I am also unable to delete that item and any other item in the RecyclerView.
Below is the code.
Home.java:
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
itemImage = findViewById(R.id.image_holder);
itemName = findViewById(R.id.add_item_name);
itemPrice = findViewById(R.id.add_price);
itemDesc = findViewById(R.id.add_desc);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
addPhotoButton = findViewById(R.id.getCameraBtn);
addFileButton = findViewById(R.id.getGalleryBtn);
imageView = findViewById(R.id.image_holder);
addPhotoButton.setOnClickListener(this);
addFileButton.setOnClickListener(this);
itemList = new ArrayList<Item>();
// add item for testing
itemList.add(new Item(R.drawable.ic_logo, "Baby Stroller", "A stroller for baby", "59.99"));
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new ItemAdapter(itemList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
#Override
public void onDeleteClick(int position) {
removeItem(position);
}
#Override
public void onEditClick(int position) {
editItem(position);
}
});
}
// log out back to start page
public void goToStart(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.getCameraBtn:
//post a photo from the camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, CAMERA_CODE);
break;
case R.id.getGalleryBtn:
//post an image
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*"); //anything that is image related
startActivityForResult(galleryIntent, GALLERY_CODE);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_CODE && resultCode == RESULT_OK) {
if (data != null) {
imageUri = data.getData(); //we have the actual path
imageView.setImageURI(imageUri); //show image
}
} else if (requestCode == CAMERA_CODE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
public void addItem(View view){
if(itemName.getText().toString().trim().length() != 0 || itemPrice.getText().toString().trim().length() != 0 || itemDesc.getText().toString().trim().length() != 0){
itemList.add(new Item(R.drawable.ic_logo, itemName.getText().toString(), itemPrice.getText().toString(), itemDesc.getText().toString()));
adapter = new ItemAdapter(itemList);
recyclerView.setAdapter(adapter);
itemName.setText("");
itemPrice.setText("");
itemDesc.setText("");
}
else{
Toast.makeText(Home.this, "All fields must be filled when creating a new item.",
Toast.LENGTH_LONG).show();
}
}
public void removeItem(int position){
itemList.remove(position);
adapter.notifyItemChanged(position);
}
ItemAdapter.java:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{
private ArrayList<Item> ItemList;
private OnItemClickListener Listener;
public void setOnItemClickListener(OnItemClickListener listener) {Listener = listener;}
public interface OnItemClickListener{
void onDeleteClick(int position);
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView item_image;
public TextView item_name;
public TextView item_desc;
public TextView item_price;
public ImageView deleteBtn;
public ImageView editBtn;
public ViewHolder(#NonNull View itemView, OnItemClickListener listener) {
super(itemView);
item_image = itemView.findViewById(R.id.item_image);
item_name = itemView.findViewById(R.id.item_name);
item_desc = itemView.findViewById(R.id.desc);
item_price = itemView.findViewById(R.id.price);
deleteBtn = itemView.findViewById(R.id.delete_item);
editBtn = itemView.findViewById(R.id.edit_item);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onDeleteClick(position);
}
}
}
});
}
}
public ItemAdapter(ArrayList<Item> itemList) {ItemList = itemList;}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent, false);
ViewHolder vh = new ViewHolder(v, Listener);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Item currentItem = ItemList.get(position);
holder.item_image.setImageResource(currentItem.getItemImage());
holder.item_name.setText(currentItem.getItemName());
holder.item_desc.setText(currentItem.getItemDesc());
holder.item_price.setText(currentItem.getItemPrice());
}
#Override
public int getItemCount() {
return ItemList.size();
}
}
Item.java:
public class Item {
private int itemImage;
private String itemName;
private String itemDesc;
private String itemPrice;
public Item(int itemImage, String itemName, String itemDesc, String itemPrice){
this.itemImage = itemImage;
this.itemName = itemName;
this.itemDesc = itemDesc;
this.itemPrice = itemPrice;
}
public int getItemImage(){return itemImage;}
public String getItemName(){return itemName;}
public String getItemDesc(){return itemDesc;}
public String getItemPrice(){return itemPrice;}
}
When the user clicks the delete button, it will work the first time and remove that particular item but when another item needs to be removed, the button is not responding. The same happens when the user adds an item and is unable to delete any items. Not really sure what could cause this. Thanks.
Edit:
The delete function works correctly when I hardcode items into the RecyclerView. It is only when adding a new item, it doesn't work anymore including the hardcoded items.
In Home.java, you are setting the click listener:
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {...}
In addItem(), you are creating a new adapter:
adapter = new ItemAdapter(itemList);
The problem is that you never set a click listener on this new adapter.
I would consider not creating a new adapter but calling a notify method to alert the adapter to a new item.
Try to change adapter.notifyItemChanged(position); to adapter.notifyItemRemoved(position);
You are telling the adapter that one item is changed, instead of that use:
notifyDataSetChanged()
this should reload all items

How can I divide one image selected case from multiple image select available condition?

I can select multiple images (maximum 4) from the gallery and show them in recycler view.
When I select more than one image it works well, but when I select only one image it gives me data.getClipdata()==null case result.
Here is my Activity code:
btnPlusImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(PlusEventActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PlusEventActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, GET_GALLERY_IMAGE);
}
});
/.../
} //On Create End
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_GALLERY_IMAGE && resultCode == RESULT_OK && data != null) {
uriList = new ArrayList<>();
if (data.getClipData() == null) {
Toast.makeText(PlusEventActivity.this, "This device can't select multiple images", Toast.LENGTH_LONG).show();
} else {
ClipData clipData = data.getClipData();
if (clipData.getItemCount() > 4) {
Toast.makeText(PlusEventActivity.this, "Maximum 4 images can be selected", Toast.LENGTH_LONG).show();
} else if (clipData.getItemCount() > 1 && clipData.getItemCount() <= 4) {
for (int i = 0; i < clipData.getItemCount(); i++) {
uriList.add(clipData.getItemAt(i).getUri());
}
} else if (clipData.getItemCount() == 1) {
Toast.makeText(PlusEventActivity.this, "One image is selected", Toast.LENGTH_LONG).show();
uriList.add(clipData.getItemAt(0).getUri());
}
}
AdapterPlusEventImage adapter = new AdapterPlusEventImage(uriList, PlusEventActivity.this);
rvEventImage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
rvEventImage.setAdapter(adapter);
}
}
My Adapter code:
public class AdapterPlusEventImage extends RecyclerView.Adapter<AdapterPlusEventImage.ItemViewHolder> {
public ArrayList<Uri> eventImageList;
public Context context;
public AdapterPlusEventImage(ArrayList<Uri> eventImageList, Context context) {
this.eventImageList = eventImageList;
this.context = context;
}
#NonNull
#Override
public AdapterPlusEventImage.ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_plus_event_image, parent, false);
AdapterPlusEventImage.ItemViewHolder viewHolder = new AdapterPlusEventImage.ItemViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull AdapterPlusEventImage.ItemViewHolder holder, int position) {
holder.itemPlusEventImage.setImageURI(eventImageList.get(position));
}
#Override
public int getItemCount() {
return eventImageList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
ImageView itemPlusEventImage;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
itemPlusEventImage = itemView.findViewById(R.id.itemPlusEventImage);
}
}
}
When one image is selected, I want to show that one image in the recycler view.

Recyclerview multi view items with button

I want to create a multi-view on Recylcerview by clicking the button(different view for each button). how to write code in Adapter Class or Condition to view it differently.
this is my code.
Adapter Class
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.TripHolderOne> {
private List trips = new ArrayList<>();
#NonNull
#Override
public TripHolderOne onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_item, parent, false);
return new TripHolderOne(itemView);
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public void onBindViewHolder(#NonNull TripHolderOne holder, int position) {
Trip currentTrip = trips.get(position);
holder.textViewTripTitle.setText(currentTrip.getTripTitle());
holder.textViewDescription.setText(currentTrip.getDescription());
holder.textViewPriority.setText(String.valueOf(currentTrip.getPriority()));
holder.textViewStartDateTime.setText(currentTrip.getStartDateTime());
}
#Override
public int getItemCount() {
return trips.size();
}
public void setTrips(List<Trip> trips) {
this.trips = trips;
notifyDataSetChanged();
}
public Trip getTripAt(int position) {
return trips.get(position);
}
class TripHolderOne extends RecyclerView.ViewHolder {
private TextView textViewTripTitle;
private TextView textViewDescription;
private TextView textViewPriority;
private TextView textViewStartDateTime;
public TripHolderOne(#NonNull View itemView) {
super(itemView);
textViewTripTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
textViewStartDateTime = itemView.findViewById(R.id.text_view_start_date_time);
}
}
MainActivity Class
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final TripAdapter adapter = new TripAdapter();
recyclerView.setAdapter(adapter);
tripViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(TripViewModel.class);
tripViewModel.getAllTrips().observe(this, new Observer<List<Trip>>() {
#Override
public void onChanged(#Nullable List<Trip> trips) {
adapter.setTrips(trips);
}
});
And inside MainActivity also have function onActivityResult.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_TRANSPORTATION && resultCode == Activity.RESULT_OK){
String title = data.getStringExtra(AddTripActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddTripActivity.EXTRA_DESCRIPTION);
int priority = data.getIntExtra(AddTripActivity.EXTRA_PRIORITY, 1);
String startDate = data.getStringExtra(AddTripActivity.EXTRA_START_DATE);
String startTime = data.getStringExtra(AddTripActivity.EXTRA_START_TIME);
String startDateTime = data.getStringExtra(AddTripActivity.EXTRA_START_DATE_TIME);
Log.d("TAG", "Text from MainActivity2" + data.getStringExtra("text"));
Trip trip = new Trip(title, description, priority, startDate,startTime,startDateTime);
tripViewModel.insert(trip);
Toast.makeText(this, "trip saved", Toast.LENGTH_SHORT).show();
}

Sending array list of object between activities with Parcelable

I want to send an array list of object from one activity to another. I am extending my object class with Parcelable and transferred list using intent onActivityResult.
But the list shows null in second activity.
first activity :
public class PlanEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener,
DatePickerDialog.OnDateSetListener{
private boolean mHoursMode;
RelativeLayout chooseEvent,time,date;
EditText eventName;
TextView timeTextView,dateTextView,chooseEventText;
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
private ImageView addOrganizer;
static final int PICK_CONTACT_REQUEST = 1;
private ArrayList<contact> mSelectedContacts;
private ListViewAdapter mAdapter;
private ListView mContactsList;
private ArrayList<Integer> selectedItemsPositions;
private boolean mContactListActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plan_event);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setText("MeaVita");
setSupportActionBar(toolbar);
setUpUI();
mAdapter = new ListViewAdapter(this,mSelectedContacts);
mContactsList.setAdapter(mAdapter);
mAdapter.setMode(Attributes.Mode.Single);
mContactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
((SwipeLayout)(mContactsList.getChildAt(position - mContactsList.getFirstVisiblePosition()))).open(true);
}
});
}
public void setUpUI()
{
chooseEvent = (RelativeLayout)findViewById(R.id.chooseEventLayout);
time = (RelativeLayout)findViewById(R.id.timeLayout);
date = (RelativeLayout)findViewById(R.id.dateLayout);
eventName = (EditText)findViewById(R.id.editTextEventName);
timeTextView = (TextView)findViewById(R.id.timeTextView);
dateTextView = (TextView)findViewById(R.id.dateTextView);
chooseEventText = (TextView)findViewById(R.id.chooseEventTextView);
addOrganizer = (ImageView)findViewById(R.id.addOrganizer);
mContactsList = (ListView)findViewById(R.id.selectedContactsList);
mSelectedContacts = new ArrayList<>();
contact contact = new contact();
contact.setContactid("1");
contact.setContactName("sid");
mSelectedContacts.add(contact);
contact.setContactid("2");
contact.setContactName("ssss");
mSelectedContacts.add(contact);
addOrganizer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(PlanEventActivity.this,ContactList.class);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
mContactListActivity = bundle.getBoolean("contactListActivity",true);
mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");
}
if(mContactListActivity)
{
addOrganizer.setVisibility(View.INVISIBLE);
mContactsList.setVisibility(View.VISIBLE);
}
else {
mContactsList.setVisibility(View.GONE);
}
}
}
Second activity :
public class ContactList extends AppCompatActivity {
private ArrayList<contact> contact_list = null;
private contactAdapter mContactAdapter = null;
private ArrayList<contact> items;
private ArrayList<contact> selectedContacts;
boolean[] isChecked;
Cursor mCursor;
ListView lv;
public int RQS_PICK_CONTACT = 1;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
ArrayList<Integer> selectedItemsPositions;
private ImageView done;
private boolean mContactListActivity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setText("Select Contacts");
setSupportActionBar(toolbar);
done = (ImageView)findViewById(R.id.done);
contact_list = new ArrayList<contact>();
selectedContacts = new ArrayList<contact>();
lv = (ListView)findViewById(R.id.list);
showContacts();
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("selectd",String.valueOf(selectedItemsPositions));
mContactListActivity = true;
selectedContacts = new ArrayList<>();//to store selected items
for (Integer pos : selectedItemsPositions) {
selectedContacts.add(items.get(pos));
}
Intent i = new Intent(ContactList.this,PlanEventActivity.class);
Bundle b = new Bundle();
b.putSerializable("selectedContacts",(Serializable) selectedContacts);
i.putExtras(b);
setResult(RESULT_OK, i);
finish();
}
});
}
#SuppressWarnings("unused")
private void getContacts() {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);
while (mCursor.moveToNext()) {
contact contact = new contact();
String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactid(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID)));
contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contact_list.add(contact);
}
isChecked = new boolean[mCursor.getCount()];
for (int i = 0; i < isChecked.length; i++) {
isChecked[i] = false;
}
this.mContactAdapter = new contactAdapter(this, R.layout.contact_list_item, contact_list);
lv.setAdapter(this.mContactAdapter);
// mCursor.close();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RQS_PICK_CONTACT) {
if (resultCode == RESULT_OK) {
getContacts();
}
}
}
public class contactAdapter extends ArrayAdapter<contact> {
public contactAdapter(Context context, int textViewResourceId, ArrayList<contact> items1) {
super(context, textViewResourceId, items1);
items = items1;
selectedItemsPositions = new ArrayList<>();
}
//to store all selected items position
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mViewHolder;
if (convertView == null) {
mViewHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.contact_list_item, parent, false);
mViewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
mViewHolder.name = (TextView) convertView.findViewById(R.id.name);
mViewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
int position = (int) mViewHolder.cb.getTag();
if (ischecked) {
//check whether its already selected or not
if (!selectedItemsPositions.contains(position))
selectedItemsPositions.add(position);
} else {
//remove position if unchecked checked item
selectedItemsPositions.remove((Object) position);
}
}
});
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
contact contacts = items.get(position);
mViewHolder.cb.setTag(position);
if (selectedItemsPositions.contains(position))
mViewHolder.cb.setChecked(true);
else
mViewHolder.cb.setChecked(false);
mViewHolder.name.setText(contacts.getContactName());
return convertView;
}
public class ViewHolder {
CheckBox cb;
TextView name;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
getContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}
private void showContacts()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
}
else {
getContacts();
}
}
}
Object class:
public class contact implements Parcelable {
private String contactName;
private String contactId;
contact(){}
contact(String contactId,String contactName)
{
this.contactId = contactId;
this.contactName = contactName;
}
public contact(Parcel in) {
this();
contactId = in.readString();
contactName = in.readString();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(contactId);
dest.writeString(contactName);
}
public static final Parcelable.Creator<contact> CREATOR = new Parcelable.Creator<contact>()
{
public contact createFromParcel(Parcel in)
{
return new contact(in);
}
public contact[] newArray(int size)
{
return new contact[size];
}
};
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactid() {
return contactId;
}
public void setContactid(String contactId) {
this.contactId = contactId;
}
}
Not getting what's going wrong. Can anyone help please. Thank you..
When you receive the results from a startActivityForResult() call, the result Intent is passed into the onActivityResult() method as the last parameter. You're using the Intent returned from getIntent(), which is the Intent used to start the current Activity, so it will not have the extras you're looking for.
In onActivityResult(), get the Bundle from the data Intent passed into the method.
Bundle bundle = data.getExtras();
You'll also need to remove this line in onActivityResult():
mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");
And replace it with:
ArrayList<contact> newContacts = bundle.getParcelableArrayList("selectedContacts");
mSelectedContacts.addAll(newContacts);
mAdapter.notifyDataSetChanged();
And make sure you've changed the b.putSerializable() call in ContactList to b.putParcelableArrayList("selectedContacts", selectedContacts).
USE THIS
Bundle b = this.getIntent().getExtras();
mSelectedContacts = b.getParcelableArrayList("selectedContacts");
INSTEAD OF
Intent i = getIntent();
Bundle bundle = i.getExtras();
mSelectedContacts = i.getParcelableArrayListExtra("selectedContacts");
I have checked your code and get some minor issues,
So may be by fixing it you can get results.
Follow below steps.
(1) In PlanEventActivity file, change onActivityResult by below,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
mContactListActivity = bundle.getBoolean("contactListActivity",true);
mSelectedContacts = (ArrayList<contact>) bundle.getSerializable("selectedContacts");
mAdapter.notifyDataSetChanged();
}
if(mContactListActivity)
{
addOrganizer.setVisibility(View.INVISIBLE);
mContactsList.setVisibility(View.VISIBLE);
}
else {
mContactsList.setVisibility(View.GONE);
}
}
As you are getting result properly but without converting it in proper type you will get null, so you need to convert your result parcelable array list in your contact arraylist other wise it will not work. And also after getting list to display in list view you need to notify your adapter for dataset changed. Also you are paassing Serializable object then also get it by getSerializable method.
(2) Implement Serializable instead of parcelable for object, like below
public class contact implements Serializable.
And now try to run your app, almost done you will get result.
Do it like this:
Bundle b = new Bundle();
b.putSerializable("selectedContacts", selectedContacts);
i.putExtras(b);
EDIT 2: Retrieve it by :
Bundle bundle = i.getExtras();
mContactListActivity = i.getBooleanExtra("contactListActivity",true);
mSelectedContacts = (ArrayList<contact>) i.getSerializableExtra("selectedContacts");
And don't forget :
public class contact implements Serializable {...}
EDIT:
REMOVE (Serializable) :
Don't write this :
b.putSerializable("selectedContacts",(Serializable) selectedContacts);
write this :
b.putSerializable("selectedContacts",selectedContacts);

Passing Images between activities using Picasso and RecyclerAdapter

I'm trying to pass an Image/Poster between activities through intent. I have used Picasso library and Recycler Adapter. but I'm unable to set the image in bundle extras after getting intent. after Year and title.
public class ShowMovieDetails extends AppCompatActivity implements OnNetworkCallHandled,RecyclerViewAdapter.OnClickListener {
private RecyclerViewAdapter adapter;
public RecyclerView recyclerView;
private DataWrapper list;
public Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_movie_details);
context=this;
Intent intent = getIntent();
String movieName = intent.getExtras().getString("moviename", "");
if (movieName != null) {
new MovieTask(movieName, this).execute();
}
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
public void onNetworkcallSuccess(Object object) {
//json to gson
String result = (String) object;
Gson gson = new Gson();
list = gson.fromJson(result, DataWrapper.class);
adapter = new RecyclerViewAdapter(list.getSearch(),this, this);
recyclerView.setAdapter(adapter);
System.out.print(result);
System.out.println("On success");
}
#Override
public void onNetworkcallFailure(Object object) {
System.out.println("On failure");
}
#Override
public void onClick(View view, int position, long itemId) {
startActivity(position);
System.out.println();
}
private void startActivity(int position) {
Intent intent = new Intent(this, DetailActivity.class);
Bundle extras = new Bundle();
extras.putString("Title", list.getSearch().get(position).getTitle());
extras.putString("Year", list.getSearch().get(position).getYear());
extras.putString("Poster",list.getSearch().get(position).getPoster());
intent.putExtras(extras);
startActivity(intent);
}
}
and the other class where m setting things in extras
public class DetailActivity extends AppCompatActivity {
private TextView Title;
private TextView Year;
private ImageView Image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Title = (TextView) findViewById(R.id.Title);
Year = (TextView) findViewById(R.id.Year);
Image = (ImageView) findViewById(R.id.iVPoster);
if (intent != null) {
String title = extras.getString("Title");
String year=extras.getString("Year");
String image=extras.getString("Poster");
Title.setText(title);
Year.setText(year);
System.out.println();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
my recyclerAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MovieDetailsHolder> {
private ArrayList<MovieDetails> movieDetails;
private Context context;
private OnClickListener onClick;
RecyclerViewAdapter(ArrayList<MovieDetails> movieDetails, Context context, OnClickListener onClick) {
this.onClick = onClick;
this.movieDetails = movieDetails;
this.context = context;
}
#Override
public MovieDetailsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new MovieDetailsHolder(view);
}
#Override
public void onBindViewHolder(MovieDetailsHolder holder, final int position) {
holder.tVMovieName.setText(movieDetails.get(position).getTitle());
Picasso
.with(context)
.load(movieDetails.get(position).getPoster())
.into(holder.iVPoster);
holder.setOnClickListener(onClick);
}
#Override
public int getItemCount() {
return movieDetails.size();
}
public static class MovieDetailsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private OnClickListener onClickListener;
CardView cv;
TextView tVMovieName;
ImageView iVPoster;
MovieDetailsHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
tVMovieName = (TextView) itemView.findViewById(R.id.tVMovieName);
iVPoster = (ImageView) itemView.findViewById(R.id.iVPoster);
itemView.setOnClickListener(this);
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
#Override
public void onClick(View v) {
onClickListener.onClick(v, getPosition(), getItemId());
}
}
public interface OnClickListener {
public void onClick(View view, int position, long itemId);
}
}
In your startActivity() function, just as you got Title,Year and Poster; also get the url to the image.
This answer will help you.

Categories