我正在用Java开发一个移动应用程序。我的活动页面中有一个EditText,它将搜索和筛选项目。这些项目是通过使用适配器和视图支架列出的。但是,当我从搜索结果中单击该项目时,该项目的位置会发生变化。有没有一种方法可以在搜索过滤器之后获得原始位置?
这是我为活动类准备的
private ArrayList<BuildingItem> buildingList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.building_selector);
//initialize database
db = new DatabaseHelper(this);
buildings = db.getBuildingData();
//populate buildingList with all database entries as BuildingItem objects
for(ArrayList building : buildings) {
String name = building.get(1).toString();
String altName = building.get(3).toString();
String dept = building.get(4).toString();
BuildingItem bi = new BuildingItem(Integer.parseInt(building.get(2).toString()), name, altName, dept);
buildingList.add(bi);
}
mRecyclerView = findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new BuildingAdapter(buildingList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new BuildingAdapter.OnItemClickListener(){
@Override
public void onItemClick(int position){
//changeItem(position, "Clicked");
passBuildings(position);
//change color
//TextView clicked = findViewById(R.id.textView2);
//clicked.setTextColor(Color.parseColor("#FF0000"));
}
});
//end of old buildingselector
// search feature
buildingSearch = (EditText) findViewById(R.id.editTextSearch);
buildingSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}
});
}
private void filter(String text)
{
ArrayList<BuildingItem> filterNames = new ArrayList<>();
//looping through existing elements
for(BuildingItem b: buildingList)
{
String buildingName = b.getBuilding();
if(buildingName.toLowerCase().contains(text.toLowerCase()))
filterNames.add(b);
}
mAdapter.filterList(filterNames);
}
我需要在活动页面中使用passBuildings函数中的位置,这需要项目的原始位置值才能返回正确的BuildingItem
public void passBuildings(int position){
buildingsToPass.add(buildingList.get(position).getBuilding());
if(buildingsToPass.size() == 1){
changeItem(position, "Starting Point");
TextView title = findViewById(R.id.SelectTitle);
title.setText("Select a Destination Building");
buildingSearch.getText().clear();
}
if(buildingsToPass.size()==2){
changeItem(position, "Destination");
// go to map page
Intent launchActivity = new Intent(BuildingActivity.this, MapsActivity.class);
launchActivity.putExtra("places", buildingsToPass);
startActivity(launchActivity);
}
}
这是我的适配器
public static class BuildingViewHolder extends RecyclerView.ViewHolder{
public ImageView buildingImage;
public TextView buildingName;
public TextView buildingAltName;
public TextView buildingDept;
public BuildingViewHolder(View itemView, OnItemClickListener listener){
super(itemView);
buildingImage = itemView.findViewById(R.id.buildingImage);
buildingName = itemView.findViewById(R.id.buildingName);
buildingAltName = itemView.findViewById(R.id.buildingAltName);
buildingDept = itemView.findViewById(R.id.buildingDept);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
public void filterList(ArrayList<BuildingItem> building)
{
this.buildingList = building;
this.notifyDataSetChanged();
}
public BuildingAdapter(ArrayList<BuildingItem> buildings){
buildingList = buildings;
}
@Override
public BuildingViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.building_item, parent, false);
BuildingViewHolder evh = new BuildingViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(BuildingViewHolder holder, int position){
BuildingItem currentItem = filteredBuildingList.get(position);
holder.buildingImage.setImageResource(currentItem.getImageResource());
holder.buildingName.setText(currentItem.getBuilding());
holder.buildingAltName.setText(currentItem.getAltName());
holder.buildingDept.setText(currentItem.getDept());
}
@Override
public int getItemCount(){
return buildingList.size();
}
您可以传递整个对象,而不是将position
从适配器传递回活动,因此更改onItemClick
的参数以接受BuildingItem
而不是int
现在,当您单击一个项目(应用过滤器后(时,该项目将被传递回活动,以使用buildingList.get(item)
:获得位置
mAdapter.setOnItemClickListener(new BuildingAdapter.OnItemClickListener(){
@Override
public void onItemClick(BuildingItem item){
int position = buildingList.indexOf(item); // here is the position you need
// rest of your code..........
}
});