1

I'm building a to do app and I'm trying to add the edit function. When an item is clicked, it takes the user to EditItemActivity.java where the user can edit that particular to do. For example, it could be edited from "Do Homework" to "Go to the gym".

So far I've been able to get the item that was clicked and have its text display in the EditText field that will be used for the editing. However, I couldn't figure out how to save the edit when the button is clicked and go back to the main screen.

So in my adapter, I'm calling the intent with extra:

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

    private List<ToDoData> toDoList;
    private Context context;

    public ToDoAdapter(List<ToDoData> todoList, Context context) {
        this.toDoList = todoList;
        this.context = context;
    }

    public ToDoAdapter(Context context){}

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView todoView;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            todoView = itemView.findViewById(R.id.to_do_display);
        }

        @Override
        public void onClick(View view) {
            Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, EditItemActivity.class);
            intent.putExtra("data", todoView.getText());
            intent.putExtra("position", getAdapterPosition());
            context.startActivity(intent);
        }
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ToDoData todoPosition = toDoList.get(position);

        holder.todoView.setText(todoPosition.getToDo());
    }

    @Override
    public int getItemCount() {
        return (toDoList == null) ? 0 : toDoList.size();
    }
}

And in my EditItemActivity.java, I'm retrieving the text and placing it in my EditText field:

public class EditItemActivity extends AppCompatActivity {

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

        final ToDoData toDoData = new ToDoData(this);
        final ToDoAdapter toDoAdapter = new ToDoAdapter(this);

        final EditText editToDo = (EditText)findViewById(R.id.edit_todo_item);
        Button button = (Button)findViewById(R.id.save_changes);
        final String text = getIntent().getExtras().getString("data");
        editToDo.setText(text);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editToDo.getText().toString() != text){
                    toDoData.setToDo(editToDo.getText().toString());
                    toDoAdapter.notifyDataSetChanged();
                    finish();

                }
            }
        });
    }
}

But I don't know how to go further than this unfortunately.

Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78

1 Answers1

2

what you will do is that :

1- send view position to edit activity

 @Override
    public void onClick(View view) {
        Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(context, EditItemActivity.class);
        intent.putExtra("data", todoView.getText());
        intent.putExtra("position", getAdapterPosition());
        context.startActivity(intent);
    }

2- in edit activity

final String position= getIntent().getExtras().getString("position");

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new intent(this, [PREVIOUS_ACTIVITY]);
        intent.putExtra("new data", todoView.getText().toString());
        intent.putExtra("position", position);
        startActivity(intent);
    }
});

4 - in activity which contains the adapter create hashmap hold the upcoming extras (position - text) as ( key - value )

HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(position, newText);

5- then pass this hash map to your adapter class when you create its instance. and in onBindViewHolder() check the upcoming view position is contained in hashmap if yes set your new data

if(hashMap.containsKey(position))
   view.todoView.setText(hashMap.get(position));
  • Why set a click listener in onBindViewHolder? – Onur-Andros Ozbek Aug 26 '17 at 23:33
  • what i mean is that when you set click listener in onBindViewHolder method you can access to view components easily – Mohamed Nagy Mostafa Aug 26 '17 at 23:38
  • I don't need a click listener for each view, I already have that. I'm looking to update the view after it is edited. – Onur-Andros Ozbek Aug 26 '17 at 23:43
  • if you want update the textview which you clicked to update then set listener to `onBindViewHolder()` then use the `int position` to determine which item is clicked and set it to bundle to the editActivity and after your edit return bundle with new text and position which you took before. – Mohamed Nagy Mostafa Aug 27 '17 at 11:40
  • How do I update `onBindViewHolder()` like you've mentioned in 3? – Onur-Andros Ozbek Aug 27 '17 at 21:57
  • Did not work at all. The code in the `onClick()` of my `EditItemActivity.java` actually maintained the list. The one you had completely wiped off my list. – Onur-Andros Ozbek Aug 28 '17 at 00:58
  • okey .. i think you have two choices - pass object to can modify adapter and list in `EditActivity`, you can check this link [link](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) or tell me are you get `List` data from database or something like that or no .. if no then i want to show onCreate/onCreateView() code inwhich you set `List` data to can find solution for this problem instead passing object – Mohamed Nagy Mostafa Aug 28 '17 at 02:21
  • Hi @Mohamed Nagy Mostafa. If you think that this question was well asked, do you mind giving me an upvote? – Onur-Andros Ozbek Mar 13 '18 at 14:13