英文:
How to pass an ArrayList of Room entities to another activity? (Android Room)
问题
Sure, here's the translated version of your provided content:
我正在制作一个名为MotoDescriptive的应用程序,您可以在其中搜索有关特定摩托车的信息。但问题是,我无法将一个Room实体(MotoEntity)数组发送到另一个活动,以便从我点击的项中获取数据。
这是我用来显示摩托车RecyclerView的Fragment:
(代码部分略)
这是用于该RecyclerView的适配器:
(代码部分略)
我的问题在这里:
(代码部分略)
如何将MotoEntity的ArrayList发送到另一个活动?
Please note that I've only translated the code parts you provided and omitted other parts as per your request. If you have any further questions or need assistance with specific code portions, feel free to ask.
英文:
I am making an app called MotoDescriptive where you can search informations about specific motorcycles. But the problem is that i can't send an array of Room entities(MotoEntity) to another activity in order to get the data from the item i clicked.
This is the Fragment i use to show a recyclerview of motorcycles:
package com.example.motodescriptive;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import java.util.ArrayList;
public class RecFragment extends Fragment implements MotorAdapter.OnNoteClicked {
private ArrayList<MotoEntity>motorcycle;
private RecyclerView recyclerView;
private MotorAdapter adapter;
private SearchView searchView;
MotoEntity motoEntity, motoEntity2, motoEntity3, motoEntity4, motoEntity5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_rec, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
searchView = v.findViewById(R.id.search_bar);
motoEntity = new MotoEntity();
motoEntity.setMoto_name("Aprilia dorsoduro 750");
motoEntity.setMoto_desc("test");
motoEntity.setMoto_img("https://upload.wikimedia.org/wikipedia/commons/c/c8/Aprilia_SMV750_Dorsoduro.jpg");
motoEntity2 = new MotoEntity();
motoEntity2.setMoto_name("Yamaha YZF R125");
motoEntity2.setMoto_desc("this is awesome");
motoEntity2.setMoto_img("https://motosvet.com/tabla/uploads/monthly_2019_06/large.yamaha_yzf_r125.jpg.1c366691f4ec323971ed7ffabf931e1e.jpg");
motoEntity3 = new MotoEntity();
motoEntity3.setMoto_name("KTM duke 125");
motoEntity3.setMoto_desc("this is awesoasssme");
motoEntity3.setMoto_img("https://bd.gaadicdn.com/processedimages/ktm/125-duke/source/125-duke5ecdf3eee8762.jpg?tr=w-360");
motoEntity4 = new MotoEntity();
motoEntity4.setMoto_name("suzuki");
motoEntity4.setMoto_desc("test teste");
motoEntity4.setMoto_img("https://prod-suzuki.azureedge.net/media/13851/jimnytransparent.png?anchor=center&mode=crop&rnd=132255608430000000");
motoEntity5 = new MotoEntity();
motoEntity5.setMoto_name("tomos ");
motoEntity5.setMoto_desc("tasasss is awesoasssme");
motoEntity5.setMoto_img("https://www.kolo.si/wp-content/uploads/2016/03/MOPED-TOMOS-FLEXER.ZELEN_.jpg");
MainActivity.appDatabase.motoDao().insert(motoEntity);
MainActivity.appDatabase.motoDao().insert(motoEntity2);
MainActivity.appDatabase.motoDao().insert(motoEntity3);
MainActivity.appDatabase.motoDao().insert(motoEntity4);
MainActivity.appDatabase.motoDao().insert(motoEntity5);
motorcycle = new ArrayList<>();
motorcycle.add(motoEntity);
motorcycle.add(motoEntity2);
motorcycle.add(motoEntity3);
motorcycle.add(motoEntity4);
motorcycle.add(motoEntity5);
adapter = new MotorAdapter(motorcycle, this, getContext());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return false;
}
});
return v;
}
@Override
public void OnNote(Bundle bundle) {
Intent intent = new Intent(getContext(), Fragments.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
And this is my Adapter for that recyclerview
package com.example.motodescriptive;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class MotorAdapter extends RecyclerView.Adapter<MotorAdapter.ViewHolder> implements Filterable {
private ArrayList<MotoEntity> arr;
private ArrayList<MotoEntity> arrFull;
private OnNoteClicked onNoteClicked;
private Context context;
public MotorAdapter(ArrayList<MotoEntity> motorcycles, OnNoteClicked onNoteClicked, Context context) {
this.arr = motorcycles;
this.arrFull = new ArrayList<>(arr);
this.onNoteClicked = onNoteClicked;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.motocycles, parent, false);
return new ViewHolder(v, onNoteClicked);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.text1.setText(arr.get(position).getMoto_name());
holder.text2.setText(arr.get(position).getMoto_desc());
Glide.with(context).load(arr.get(position).getMoto_img()).into(holder.image1);
}
@Override
public int getItemCount() {
return arr.size();
}
@Override
public Filter getFilter() {
return ExampleFilter;
}
Filter ExampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
ArrayList<MotoEntity> filteredList = new ArrayList<>();
if(charSequence == null || charSequence.length() == 0) {
filteredList.addAll(arrFull);
} else {
String filteredPattern = charSequence.toString().toLowerCase().trim();
for(MotoEntity item: arrFull) {
if(item.getMoto_name().toLowerCase().startsWith(filteredPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
arr.clear();
arr.addAll((List) filterResults.values);
notifyDataSetChanged();
}
};
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView text1;
private TextView text2;
private ImageView image1;
private CardView cardView;
OnNoteClicked onNoteClicked;
public ViewHolder(@NonNull View itemView, OnNoteClicked onNoteClicked) {
super(itemView);
text1 = itemView.findViewById(R.id.text1);
text2 = itemView.findViewById(R.id.text2);
image1 = itemView.findViewById(R.id.image1);
cardView = itemView.findViewById(R.id.cardView);
this.onNoteClicked = onNoteClicked;
cardView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putInt("id", getAdapterPosition());
onNoteClicked.OnNote(bundle);
}
}
public interface OnNoteClicked {
void OnNote(Bundle bundle);
}
}
My problem is here:
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putInt("id", getAdapterPosition());
onNoteClicked.OnNote(bundle);
}
}
How can i send an arraylist of MotoEntities to another activity?
答案1
得分: 0
首先,使MotoEntity
实现Serializable
接口,然后您可以传递该列表。
Bundle bundle = new Bundle();
bundle.putSerializable();
(Note: The provided code snippet is incomplete and contains a method call bundle.putSerializable()
without arguments. Please complete the method call with the appropriate arguments before using it.)
英文:
First, make MotoEntity
implement Serializable
, then you can pass the list.
Bundle bundle = new Bundle();
bundle.putSerializable();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论