替换而不是将项目添加到 Arraylist
Posted
技术标签:
【中文标题】替换而不是将项目添加到 Arraylist【英文标题】:Replace Instead Of Adding Item To Arraylist 【发布时间】:2021-08-23 07:30:09 【问题描述】:我正在构建一个应用程序来练习 arraylist 和 recyclerView。
在主要活动中,我有“添加”按钮,而不是可以将内容添加到 arraylit 以显示在 recyclerView 中,但是,第一次添加新行时它工作正常,但是当我尝试添加另一个时,它用新行替换我添加的第一行。
如果你能指出我做错了什么,我会很高兴,谢谢。
添加线路活动:
public void AddButton(View view)
if (imageUri != null && !mEditText.getText().toString().isEmpty())
Intent intent = new Intent(AddCompany.this,MainActivity.class);
intent.putExtra("isAddNewCompany", true);
intent.putExtra("CompanyImage", imageUri.toString());
intent.putExtra("CompanyName", mEditText.getText().toString());
startActivity(intent);
MainActivity OnCreate 与视图:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
ArrayList<String> listTitle = new ArrayList<>();
ArrayList<String> listPicture = new ArrayList<>();
if (intent.getBooleanExtra("isAddNewCompany",false))
String CompanyImage = intent.getStringExtra("CompanyImage");
String companyName = intent.getStringExtra("CompanyName");
listTitle.add(companyName);
listPicture.add(CompanyImage);
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerAdapter adapter = new RecyclerAdapter(this,listPicture,listTitle);
adapter.setClickListener(new RecyclerAdapter.ItemClickListener()
@Override
public void onItemClick(View view, int position)
Log.e("Position Number - ", String.valueOf(position));
Toast.makeText(MainActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
);
recyclerView.setAdapter(adapter);
RecyclerView 适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
private final ArrayList<String> mData;
private final ArrayList<String> mImageView;
private final LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
RecyclerAdapter(Context context, ArrayList<String> imageView, ArrayList<String> data)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mImageView = imageView;
// inflates the row layout from xml when needed
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position)
String animal = mData.get(position);
String picture = mImageView.get(position);
Picasso.get().load(picture).into(holder.myImageView);
holder.myTextView.setText(animal);
// total number of rows
@Override
public int getItemCount()
return mData.size();
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
TextView myTextView;
ImageView myImageView;
ViewHolder(View itemView)
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
myImageView = itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
// convenience method for getting data at click position
String getItem(int id)
return mData.get(id);
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
【问题讨论】:
你在 recyclerview 中只看到一个值吗?@Mr.Goomer 是的,当我尝试添加另一个时,它只是替换我已经添加的内容 【参考方案1】:当您开始活动时,每次都会创建新的列表实例,这就是您只有一个值的原因。如果您想要已经添加的值,请使用相同的列表实例。
List<String> listTitle;
List<String> listPicture;
public List<String> getLisPictureInstance()
if(listPicture ==null)
listPicture = new ArrayList<>();
return listPicture ;
public List<String> getListTitleInstance()
if(listTitle ==null)
listTitle = new ArrayList<>()
return listTitle ;
if (intent.getBooleanExtra("isAddNewCompany",false))
String CompanyImage = intent.getStringExtra("CompanyImage");
String companyName = intent.getStringExtra("CompanyName");
getListTitleInstance().add(companyName);
getLisPictureInstance().add(CompanyImage);
【讨论】:
Hey androidLearner 感谢您的回复。它仍在替换行而不是添加另一行..以上是关于替换而不是将项目添加到 Arraylist的主要内容,如果未能解决你的问题,请参考以下文章
List.Add 正在替换索引 0 而不是添加到下一个位置。为啥?
查找和替换替代方法?而不是 "REPLACE",只是简单地添加文本到现有的单元格中?