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

时间:2018-03-01 10:55:44

标签: android android-recyclerview android-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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

创建一个HashMap,其中Item位置为键

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值。

答案 1 :(得分:1)

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

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;
}