public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private ItemAdapter adapter; private ArrayList<Map<String, String>> itemsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); itemsList = new ArrayList<>(); adapter = new ItemAdapter(itemsList); recyclerView.setAdapter(adapter); // 添加按钮点击事件 Button addButton = findViewById(R.id.addButton); Button execute = findViewById(R.id.execute); EditText editText = findViewById(R.id.editText); EditText editTextwsign = findViewById(R.id.editTextwsign); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, String> stringStringMap = new HashMap<>(); String packagestr = editText.getText().toString(); String sign = editTextwsign.getText().toString(); stringStringMap.put("pack", packagestr); stringStringMap.put("sign", sign); itemsList.add(stringStringMap); adapter.notifyItemInserted(itemsList.size() - 1); editText.setText(""); editTextwsign.setText(""); } }); execute.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str = itemsList.toString(); Log.e("执行的数据", str); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:clickable="true" android:orientation="horizontal"> <TextView android:id="@+id/tv_01" style="@style/fragment_text" android:text="包名:" android:textSize="16sp" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:layout_marginBottom="7dp" android:layout_toRightOf="@+id/tv_01" android:background="@null" android:gravity="right|center_vertical" android:hint="请输入包名" android:textColor="#ff3d3d3d" android:textColorHint="#ffcccccc" android:textSize="12sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:clickable="true" android:orientation="horizontal"> <TextView android:id="@+id/tv_02" style="@style/fragment_text" android:text="签名:" android:textSize="16sp" /> <EditText android:id="@+id/editTextwsign" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:layout_marginBottom="7dp" android:layout_toRightOf="@+id/tv_02" android:background="@null" android:gravity="right|center_vertical" android:hint="请输入签名" android:textColor="#ff3d3d3d" android:textColorHint="#ffcccccc" android:textSize="12sp" /> </RelativeLayout> <Button android:id="@+id/addButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="add" /> <Button android:id="@+id/execute" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="execute" /> </LinearLayout>
package com.example.mytest; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageButton; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.Map; public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> { private ArrayList<Map<String, String>> itemsList; public ItemAdapter(ArrayList<Map<String, String>> itemsList) { this.itemsList = itemsList; } @NonNull @Override public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new ItemViewHolder(view); } @Override public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { Map<String, String> item = itemsList.get(position); holder.editText.setText(item.get("pack")); holder.editTexttwo.setText(item.get("sign")); } @Override public int getItemCount() { return itemsList.size(); } public class ItemViewHolder extends RecyclerView.ViewHolder { EditText editText; EditText editTexttwo; ImageButton deleteButton; public ItemViewHolder(@NonNull View itemView) { super(itemView); editText = itemView.findViewById(R.id.editText); editTexttwo = itemView.findViewById(R.id.editTexttwo); deleteButton = itemView.findViewById(R.id.deleteButton); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { itemsList.remove(position); notifyItemRemoved(position); } } }); } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:orientation="vertical"> <EditText android:id="@+id/editText" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="包名输入项" /> <EditText android:id="@+id/editTexttwo" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="签名输入项" /> <ImageButton android:id="@+id/deleteButton" android:layout_width="match_parent" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_menu_delete" /> </LinearLayout>