gpt4 book ai didi

java - 如何从 RecyclerView Adapter 获取传递给 MainActivity 的变量?

转载 作者:行者123 更新时间:2023-12-02 03:20:05 25 4
gpt4 key购买 nike

我只是在玩一些代码,学习新东西,当我遇到这个问题时......我试图将一个变量从我的 RecylcerViewAdapter 传递到 MainActivity 中的方法,但我似乎无法完成它。

我尝试了很多不同的界面和转换方法,但没有任何效果。由于我对这一切都很陌生,也许我在某个地方犯了一个小错误?

我的界面:

public interface AdapterCallback {
void onMethodCallback(int id);
}

这是我的适配器类:

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

private List<Post> postList;
private Context context;
private AdapterCallback listener;

public PostAdapter() {
}

public PostAdapter(List<Post> postList, Context context) {
this.postList = postList;
this.context = context;
}

public void setListener(AdapterCallback listener) {
this.listener = listener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_layout, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
viewHolder.tvTitle.setText(postList.get(position).getTitle());
viewHolder.tvBody.setText(new StringBuilder(postList.get(position).getBody().substring(0, 20)).append("..."));
viewHolder.tvId.setText(String.valueOf(postList.get(position).getUserId()));

viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = postList.get(position).getId();
if (listener != null) {
listener.onMethodCallback(id);
}
}
});
}

@Override
public int getItemCount() {
return postList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

TextView tvTitle;
TextView tvBody;
TextView tvId;
LinearLayout parentLayout;

public ViewHolder(View itemView) {
super(itemView);

tvTitle = itemView.findViewById(R.id.tvTitle);
tvBody = itemView.findViewById(R.id.tvBody);
tvId = itemView.findViewById(R.id.tvId);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
And my MainActivity:

public class MainActivity extends AppCompatActivity {

public static final String TAG = "MainActivityLog";
private CompositeDisposable disposable = new CompositeDisposable();

@BindView(R.id.rvPosts)
RecyclerView rvPosts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ButterKnife.bind(this);

rvPosts.setHasFixedSize(true);
rvPosts.setLayoutManager(new LinearLayoutManager(this));

populateList();
logItems();
}

private void populateList() {
MainViewModel viewModel = ViewModelProviders.of(MainActivity.this).get(MainViewModel.class);
viewModel.makeQuery().observe(MainActivity.this, new Observer<List<Post>>() {
@Override
public void onChanged(@Nullable List<Post> posts) {
PostAdapter adapter = new PostAdapter(posts, getApplicationContext());
rvPosts.setAdapter(adapter);
}
});
}

public void logItems() {
PostAdapter adapter = new PostAdapter();
adapter.setListener(new AdapterCallback() {
@Override
public void onMethodCallback(int id) {
MainViewModel viewModel = ViewModelProviders.of(MainActivity.this).get(MainViewModel.class);
viewModel.makeSingleQuery(id).observe(MainActivity.this, new Observer<Post>() {
@Override
public void onChanged(@Nullable final Post post) {
Log.d(TAG, "onChanged: data response");
Log.d(TAG, "onChanged: " + post);
}
});
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
disposable.clear();
}
}

populateList() 方法工作正常,但 logItems() 方法有问题。

因此,当我单击 RecyclerView 中的 View 时,我希望日志输出所单击帖子的标题、描述和 ID。但什么也没发生...

因此,任何帮助将不胜感激。

最佳答案

创建适配器全局变量,即字段。使用相同的对象来设置每个属性。

private PostAdapter adapter;

将您的 logItems 方法替换为:

public void logItems() {
adapter.setListener(new AdapterCallback() {
@Override
public void onMethodCallback(int id) {
MainViewModel viewModel = ViewModelProviders.of(MainActivity.this).get(MainViewModel.class);
viewModel.makeSingleQuery(id).observe(MainActivity.this, new Observer<Post>() {
@Override
public void onChanged(@Nullable final Post post) {
Log.d(TAG, "onChanged: data response");
Log.d(TAG, "onChanged: " + post);
}
});
}
});
}

并用以下内容填充列表:

private void populateList() {
MainViewModel viewModel = ViewModelProviders.of(MainActivity.this).get(MainViewModel.class);
viewModel.makeQuery().observe(MainActivity.this, new Observer<List<Post>>() {
@Override
public void onChanged(@Nullable List<Post> posts) {
adapter = new PostAdapter(posts, getApplicationContext());
rvPosts.setAdapter(adapter);
logItems();
}
});
}

并且不要从 onCreate 调用 logItems()

关于java - 如何从 RecyclerView Adapter 获取传递给 MainActivity 的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56931974/

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