我有一个回收视图适配器,接受用户的编辑文本输入。我想将这些值作为列表传递回回收者视图所在的活动。我需要在活动中的这个列表作为参数发送到api通过改造。
发布于 2022-03-11 11:15:45
有个工作样本。
MainActivity.java
进口android.os.Bundle;
进口android.view.KeyEvent;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.inputmethod.EditorInfo;
进口android.widget.EditText;
进口android.widget.TextView;
进口androidx.annotation.NonNull;
进口androidx.appcompat.app.AppCompatActivity;
进口androidx.recyclerview.widget.RecyclerView;
进口java.util.ArrayList;
进口java.util.List;
公共类MainActivity扩展AppCompatActivity {
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Adapter adapter = new Adapter(); RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setAdapter(adapter); EditText editText = findViewById(R.id.editText); editText.setOnEditorActionListener((textView, actionId, keyEvent) -> { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_DONE) { // add item adapter.addItem(editText.getText().toString()); // clear text input textView.setText(""); handled = true; } return handled; });}}
activity_main.xml
http://schemas.android.com/apk/res/android“
xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="16dp"tools:context=".MainActivity"><EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Insert text.." android:imeOptions="actionDone" android:inputType="text" app:layout_constraintBottom_toTopOf="@+id/recyclerView" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" app:layout_constraintVertical_chainStyle="packed" /><androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText" />Adapter.java
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口androidx.annotation.NonNull;
进口androidx.recyclerview.widget.RecyclerView;
进口java.util.ArrayList;
进口java.util.List;
公共类适配器扩展RecyclerView.Adapter {
private List<String> items = new ArrayList<>();@NonNull@Overridepublic Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(android.R.layout.test_list_item, parent, false); return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) { String currentItem = items.get(position); holder.textView.setText(currentItem);}public void addItem(String item) { // add at first position - replace with your desired index int index = 0; this.items.add(index, item); notifyItemInserted(index);}@Overridepublic int getItemCount() { return items.size();}public class ViewHolder extends RecyclerView.ViewHolder { protected TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(android.R.id.text1); }}}
```javascript您可以使用键盘上的“完成”键通知适配器某个项已经添加。
要在xml中显示“完成”按钮,必须在编辑文本中使用这两个说明。
```javascriptandroid:imeOptions="actionDone"android:inputType="text"```javascripthttps://stackoverflow.com/questions/71408953
复制相似问题