[英]How to different open activities without using position
這是一個帶有類別和子類別的食譜應用程序。 我想知道除了 position 命令之外還有其他命令可以打開不同的活動。 因為我在搜索時遇到問題,它變成了字母順序,當我單擊該項目時,它給了我不同的結果,因為 position 正在改變。 我不知道我的代碼有什么問題,是搜索 function 還是 position 命令打開不同的新活動。
主適配器.class
public class MainAdapter extends FirebaseRecyclerAdapter<MainModel,MainAdapter.myViewHolder> {
private Context context;
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull MainAdapter.myViewHolder holder, int position, @NonNull MainModel model) {
holder.categoryName.setText(model.getName());
holder.categoryDesc.setText(model.getDescription());
Glide.with(holder.categoryImage.getContext())
.load(model.getImage())
.placeholder(R.drawable.common_google_signin_btn_icon_dark)
.error(R.drawable.common_google_signin_btn_icon_dark_normal)
.into(holder.categoryImage);
context = holder.itemView.getContext();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent;
switch (position){
case 0:
intent = new Intent(context, ChickenActivity.class);
break;
case 1:
intent = new Intent(context, PorkActivity.class);
break;
case 2:
intent = new Intent(context, BeefActivity.class);
break;
case 3:
intent = new Intent(context, SeafoodActivity.class);
break;
default:
intent = new Intent(context, VegetableActivity.class);
break;
}
context.startActivity(intent);
}
});
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item,parent,false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder{
ImageView categoryImage;
TextView categoryName, categoryDesc;
public myViewHolder(@NonNull View itemView) {
super(itemView);
categoryImage = (ImageView)itemView.findViewById(R.id.categoryImage);
categoryName = (TextView)itemView.findViewById(R.id.categoryName);
categoryDesc = (TextView)itemView.findViewById(R.id.categoryDesc);
}
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MainAdapter mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<MainModel> options =
new FirebaseRecyclerOptions.Builder<MainModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("category"), MainModel.class)
.build();
mainAdapter = new MainAdapter(options);
recyclerView.setAdapter(mainAdapter);
}
@Override
protected void onStart() {
super.onStart();
mainAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mainAdapter.stopListening();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search,menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
txtSearch(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
txtSearch(query);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void txtSearch(String str)
{
FirebaseRecyclerOptions<MainModel> options =
new FirebaseRecyclerOptions.Builder<MainModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("category").orderByChild("name").startAt(str).endAt(str+"~"), MainModel.class)
.build();
mainAdapter = new MainAdapter(options);
mainAdapter.startListening();
recyclerView.setAdapter(mainAdapter);
}
}
與其將活動綁定到一個數字,不如將其綁定到一個標簽或某個標識符。 例如,您在列表中有 3 個項目,例如:
[
{
name: "chickenwings"
},
{
name: "octopus"
},
{
name: "tomato"
}
]
相反,您可以擁有附加信息,例如該項目應與之關聯的活動,例如
[
{
name: "chickenwings",
category: "chicken"
},
{
name: "octopus",
category: "seafood"
},
{
name: "tomato",
category: "vegetable"
}
]
這將幫助您更新代碼,如下所示
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent;
val category = model.getCategory()
switch (category){
case "chicken":
intent = new Intent(context, ChickenActivity.class);
break;
case "pork":
intent = new Intent(context, PorkActivity.class);
break;
case "beef":
intent = new Intent(context, BeefActivity.class);
break;
case "seafood":
intent = new Intent(context, SeafoodActivity.class);
break;
case "vegetable":
default:
intent = new Intent(context, VegetableActivity.class);
break;
}
context.startActivity(intent);
}
});
您可以使用“holder.getAdapterPosition”代替“position”來解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.