如何从RecyclerView中的EditText获取字符串值?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从RecyclerView中的EditText获取字符串值?相关的知识,希望对你有一定的参考价值。

我正试图在我的活动中从回收者视图中获取玩家名称,其中有X个edittexts。我已经尝试使用TextChangedListenerArrayList的玩家名称添加名称,但我得到了每一步(即:B,Bo,Bob,D,Da,Dav,Davi,David),我试图只获得最后的字符串值(只有全名?我怎么能修改TextChangedListener呢?还有其他的吗?

干杯;

package com.olirwin.spartacus.deutsch;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    RecyclerView mPlayerList;
    List<String> mPlayerNames;
    PlayerAdapter mAdapter;
    EditText mInput;
    Button mCreateButton;
    Button submitButton;
    ArrayList<String> names;


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

        mPlayerNames = new ArrayList<>();

        // setup recycler view
        mPlayerList = findViewById(R.id.player_list);
        mPlayerList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PlayerAdapter();
        mPlayerList.setAdapter(mAdapter);

        // setup input EditText
        mInput = findViewById(R.id.input);

        // setup Create button
        mCreateButton = findViewById(R.id.create_button);
        mCreateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // clear old player names
                mPlayerNames.clear();

                // read user input: number of player:
                String input = mInput.getText().toString();
                int numberOfPlayer;
                try {
                    numberOfPlayer = Integer.parseInt(input);
                } catch (NumberFormatException e) {
                    Toast.makeText(Main2Activity.this, "Invalid input!!!", Toast.LENGTH_SHORT).show();
                    return;
                }
                for (int i = 0; i < numberOfPlayer; ++i) {
                    mPlayerNames.add("Player #" + (i + 1));
                }

                // make change on recycler view
                mAdapter.notifyDataSetChanged();

                // dismiss keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mInput.getWindowToken(), 0);

            }
        });

        //setup submit

        submitButton = findViewById(R.id.submit);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                names = mAdapter.getNames();
                Intent intent = new Intent(v.getContext(), Score.class);
                intent.putStringArrayListExtra("names", names);
                v.getContext().startActivity(intent);
            }
        });

    }

    private class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {

        private ArrayList<String> names;
        private String string;

        public PlayerAdapter() {
            names = new ArrayList<>();
        }

        @Override
        public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(Main2Activity.this).inflate(R.layout.item_layout, parent, false);
            return new PlayerHolder(v);
        }


        @Override
        public void onBindViewHolder(final PlayerHolder holder, int position) {
            holder.bind(mPlayerNames.get(position));
            holder.mPlayerName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //blank
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //blank;
                }

                @Override
                public void afterTextChanged(Editable s) {
                    names.add(holder.mPlayerName.getText().toString());
                }
            });

        }

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

        public ArrayList<String> getNames(){
            return this.names;
        }

        public class PlayerHolder extends RecyclerView.ViewHolder {

            TextView mPlayerLabel;
            EditText mPlayerName;


            public PlayerHolder(View itemView) {
                super(itemView);
                mPlayerLabel = itemView.findViewById(R.id.player_label);
                mPlayerName = itemView.findViewById(R.id.player_name);
            }

            public void bind(String playerName) {
                mPlayerLabel.setText(playerName);
                mPlayerName.setHint("Name of " + playerName);
            }
        }
    }
}
答案

使用Item位置作为键创建HashMap

HashMap<Integer, String> map = new HashMap<>;

更改你的内容

@Override
public void afterTextChanged(Editable s) {
   String value = map.get(position);  // here position is the key
if (value != null) {
    map.put(position, holder.mPlayerName.getText().toString());
} else {
    // Key might be present...
    if (map.containsKey(key)) {
       // Okay, there's a key but the value is null
    } else {
       // Definitely no such key
    }
}
}

然后使用HashMap中的get()方法获取HashMap值。

另一答案

请更改适配器中的代码,如下所示:

String[] playerNames = new String[names.size];

@Override
public void onBindViewHolder(final PlayerHolder holder, int position)
{
    holder.bind(mPlayerNames.get(position));
    holder.mPlayerName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //blank
            }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                //blank;
        }

        @Override
        public void afterTextChanged(Editable s) {
            playerNames[i] = s.toString().trim();
        }
        });

    }

public ArrayList<String> getNames(){

    ArrayList<String> pNameList = new ArrayList<>();

    for(int i=0; i<playerNames.length; i++){
        pNameList.add(playerNames[i]);
    }        
   return pNameList;
}

以上是关于如何从RecyclerView中的EditText获取字符串值?的主要内容,如果未能解决你的问题,请参考以下文章

保留recyclelerview中的edittext内容

Recyclerview中的多个edittext

使用Recyclerview的searchview EditText

RecyclerView列表搜索(过滤)EditText问题

如何在具有多个edittext的recyclerview中验证和设置错误?

如何从RecyclerView项中获取Value