gpt4 book ai didi

java - 已定义方法(Android 接口(interface))但仍未找到?

转载 作者:行者123 更新时间:2023-12-01 23:59:35 28 4
gpt4 key购买 nike

我遵循Head First的Android开发指南作为引用,并尝试使用cardview实现面条菜肴的recyclerview。

我目前正在按照书中的建议将点击功能作为与适配器分开的接口(interface)来实现;虽然我现在没有使用 fragment 。一个问题是我定义的 setListener 方法没有被我的 MainActivity 找到。

错误是“找不到符号方法setListener()”。所以,我的方法就像未定义一样。您知道如何解决这个问题吗?这是MainActivity的代码

package com.justin.android.foodapp;

import androidx.appcompat.app.A

public class MainActivity extends AppCompatActivity {

private Noodles[] noodlesDataset = Noodles.noodles;
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Log.i("MainActivity", "Hello World");

// get the recycler view
recyclerView = (RecyclerView) findViewById(R.id.noodleRecycler);

// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

//specify an adapter
mAdapter = new MyAdapter(noodlesDataset);
recyclerView.setAdapter(mAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);

mAdapter.setListener(new MyAdapter.Listener(){
public void onClick(int position) {
Intent intent = new Intent(MainActivity.this, NoodleDetailActivity.class);
intent.putExtra(NoodleDetailActivity.EXTRA_NOODLE_ID, position);
(MainActivity.this).startActivity(intent);
}
});

}
}

这是我的适配器代码,我在其中尝试定义 setListener 方法

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private Noodles[] mDataset;
private Listener listener;

interface Listener {
void onClick(int position);
}

// set listener to the implementation of given interface
public void setListener(Listener listener) {
this.listener = listener;
}

public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView textView;
public ImageView imageView;
public MyViewHolder(View v) {
super(v);
cardView = v.findViewById(R.id.card_view);
textView = v.findViewById(R.id.textView);
imageView = v.findViewById(R.id.food_image);
}
}
// contructor; pass the data from Java class Noodles to MyAdapter
public MyAdapter(Noodles[] noodlesDataset) {
mDataset = noodlesDataset;
}

@Override
public int getItemCount() {
return mDataset.length;
}

// create a new ViewHolder and place the views in layout my_text_view inside.
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final CardView cardView = holder.cardView;
holder.textView.setText(mDataset[position].getName());
holder.imageView.setImageResource(mDataset[position].getImageResourceId());

// when clicked, send an intent from cardview
cardView.setOnClickListener(new View.OnClickListener() {
@Override
// if listener is present, call the implementation of onClick set by the parent
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}

});
}

}

最佳答案

替换这个

私有(private)RecyclerView.Adapter mAdapter;

public MyAdapter mAdapter; 

并删除多余的内容并进行更新,如下所示

   private RecyclerView recyclerView;
public MyAdapter mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Log.i("MainActivity", "Hello World");

// get the recycler view
recyclerView = (RecyclerView) findViewById(R.id.noodleRecycler);


//specify an adapter
mAdapter = new MyAdapter(noodlesDataset);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setAdapter(mAdapter);

recyclerView.setLayoutManager(layoutManager);

关于java - 已定义方法(Android 接口(interface))但仍未找到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185261/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com