简体   繁体   中英

Android Studio - How to get data from an EditText in a RecyclerView and put them into an ArrayList in the main activity?

So I have a RecyclerView consisting of EditTexts that uses an ArrayList as its data set, when the EditTexts are edited I want that ArrayList to be updated as well and I want to be able to use it in the Main Activity currently I've tried implementing a TextChangedListener however I have no clue how to get the data and transfer it to my list. Here's the code:

Main Activity:

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
public ArrayList playerList;
private Button runItButton;

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

    playerList = new ArrayList();
    for(int i = 0; i < 4; i++){
        playerList.add("Player " + (i + 1));
    }

    runItButton = findViewById(R.id.buttonStartGame);
    runItButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, DrinktasksActivity.class);
            intent.putExtra("playerList", playerList);
            startActivity(intent);
        }
    });

    recyclerView = findViewById(R.id.recyclyerViewNames);
    MyAdapter myAdapter = new MyAdapter(this, playerList);
    recyclerView.setAdapter(myAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}


public void addPlayers(View view){
    playerList.add("Player " + (playerList.size() + 1));
    recyclerView.getAdapter().notifyDataSetChanged();
}

}

The adapter:

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

Context context;
ArrayList playerList;

public MyAdapter(Context ct, List players){
    context = ct;
    playerList = (ArrayList) players;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.enter_name, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String current = playerList.get(position).toString();
    holder.editText.setHint(current);

    holder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // The code I assume should go here but I don't know how to implement

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    EditText editText;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        editText = itemView.findViewById(R.id.editname);
    }
}

}

All you have to do is to implement a custom callback to your activity with the index in which the edittext is edited and what is the new text.

Create a callback interface inside adapter

    public interface TextChangeCallback {
        void textChangedAt(int position, String text);
    }

Pass the inteface implementation to Adapter via constructor

public MyAdapter(Context ct, List players,TextChangeCallback callback){
    context = ct;
    playerList = (ArrayList) players;
    this.callback = callback;
}

Trigger callback to activity whenever you edit the text using TextWatcher

holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("")){
                    return;
                }
                data.set(position, String.valueOf(s));
                callback.textChangedAt(position, String.valueOf(s));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Add this interface implementation to your adapter

    MyAdapter myAdapter = new MyAdapter(this, playerList, new MyAdapter.TextChangeCallback() {
            @Override
            public void textChangedAt(int position, String text) {
                playerList.set(position, text);
                Log.d("UPDATED LIST : ", String.valueOf(data));
            }
        });
    recyclerView.setAdapter(myAdapter);

This will now update the ArrayList in adapter and activity as you edit the edittext from recyclerview.

You can find the complete implementation from this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM