gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:28 27 4
gpt4 key购买 nike

我正在尝试从我的 Activity 中的回收站 View 中获取玩家名称,其中有 X 个编辑文本。我曾尝试使用 TextChangedListener 将名称添加到播放器名称的 ArrayList 中,但我得到了每一步(即:B、Bo、Bob、D、Da、Dav , Davi, David),我试图只获取 final 字符串值(只有全名?如何修改 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位置为key的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
}
}
}

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

关于android - 如何从 RecyclerView 中的 EditText 获取字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com