0

My scenario is I have a List<Object> and I have the detail Activity where I can edit / delete, I'm wondering if is there any other way that I'm doing.

I'm passing from the adapter an intent where I pass the position of that object, and the Object itself, on Activity2 I do stuff and when I press delete or edit I send an intent with that action and the object and the position of the array, can you tell to me another way to do that efficient?

I thought a static List but I do not like static stuff to be honest...

Without DB stuff

StuartDTO
  • 783
  • 7
  • 26
  • 72

3 Answers3

3

The good way will be to use some local database like Realm or ROOM. Then you won't pass whole object via Intent.

In Acitivity1 you load list of objects from local database. When users clicks on item, you pass itemId to Activity2 via bundle.

Then in Activity2 you can find item in local databse by id, then edit what you want, and save changes in database.

After returning to Activity1 you can reload list from database.

UPDATE

If you don't want to play with local databases, then read about MVVM pattern + Data Binding.

In MVVM you have ViewModel which keeps your data, like List<Object>.

Thanks to DataBinding you can bind your list with adapter. And then all changes made on list will be instant visible in UI.

This is how it looks in Kotlin:

Binding list of objects with adapter:

@BindingAdapter("contractorItems")
fun setContractors(recyclerView: RecyclerView, items: List<Contractor>) {
    with(recyclerView.adapter as ContractorAdapter) {
        replaceData(items)
    }
}

Binding setContractors method with RecyclerView

<android.support.v7.widget.RecyclerView
    android:id="@+id/contractors_recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    app:contractorItems="@{viewmodel.contractors}"
    tools:listitem="@layout/contractor_signature_item" />

ViewModel:

class ContractorsViewModel: ViewModel() {

    val contractors = ObservableArrayList<Contractor>()

}
RediOne1
  • 10,389
  • 6
  • 25
  • 46
  • Can you put a small example RediOne1? – StuartDTO Apr 17 '18 at 11:27
  • @StuartDTO I implement my application in Kotlin and it looks a bit different. Unfortunately, this can not be briefly presented. Look for information about ViewModel and DataBinding. – RediOne1 Apr 17 '18 at 11:38
0

The best way to do it is using onActivityResult.

Read documentations from here

When you starting the second activity use startActivityForResult and when you finished the task, you can set result and the new data.

After that, when you finish the second activity, your new intent bundle will return to your onActivityResult.

When you received the data, you can handle your updates.

Also there is a good example here: https://stackoverflow.com/a/10407371/3669559

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • Yes, I'm doing this way, but I do not know if is there any other way efficient instead of passing full object and position of the list – StuartDTO Apr 17 '18 at 11:20
  • @StuartDTO when you clicked the item, send position info with your object, and when you returned, update the position. Alternatively, you can search and find the data position by using uniqe value like "id". – Oğuzhan Döngül Apr 17 '18 at 11:23
0

You can do it in the same activity. If i understand you well you want to delete an item from list when it's clicked on.

ListView.setOnItemClickListener{ new adapterView.onItemClickListerner(){ @Override public void onItemClick (AdapterView<?> parent ,View view,int position,Long id){ parent.getItemAtPostion(postion);//save this} } Now you get the item, you can delete it and keep in the same activity.

Gerard
  • 15,418
  • 5
  • 30
  • 52
Sideeg MoHammed
  • 375
  • 2
  • 10