gpt4 book ai didi

java - 在回收者 View 中设置 Intent ,OnCreate 以转到特定列表(行)的另一个特定 Activity

转载 作者:行者123 更新时间:2023-11-30 12:05:01 26 4
gpt4 key购买 nike

我已经实现了 onClick,当我点击每个列表时,所有列表都将转到“abc” Activity ,但我想将包含单词“dd”的列表导航到 Activity “abcActivity”和包含单词的列表“ss”到“defActivity”。

我试图添加另一个 intent 但它再次对所有列表显示相同 :(

请帮助我,我在写这篇文章时阅读了所有建议的文章,但没有一篇是我所问的答案。请帮助我。

这是主要 Activity :

public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {
private RecyclerView recyclerView;
private static final String TAG = MainActivity.class.getCanonicalName();

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

recyclerView = findViewById(R.id.home_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

recyclerView.setLayoutManager(layoutManager);

List<Word> wordList = new ArrayList<>();


wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
wordList.add(new Word(R.drawable.ic_launcher_background, "cc"));


WordAdapter adapter = new WordAdapter(this);
recyclerView.setAdapter(adapter);
adapter.addItems(wordList);
}

@Override
public void onNoteClick(int position) {
Log.d(TAG, "clicked on the position:" + position);


Intent myIntent = new Intent(MainActivity.this, abc.class);
myIntent.putExtra("dd", 1);
MainActivity.this.startActivity(myIntent);


Intent feedbackIntent = new Intent(MainActivity.this, def.class);
feedbackIntent.putExtra("ss", 0);
MainActivity.this.startActivity(feedbackIntent);

}
}

这是我在“abc”的oncreate下添加的代码

Intent myIntent = getIntent();
String value = myIntent.getStringExtra("dd" );

这是我在“def”的oncreate下添加的代码

Intent intent = getIntent();
String value = intent.getStringExtra("ss");

这是名为 WordAdapter 的适配器

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

private List<Word> wordList;
private OnNoteListener mOnNoteListener;

public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
this.wordList = wordList;
this.mOnNoteListener = onNoteListener;
}

public WordAdapter(OnNoteListener onNoteListener) {
this(new ArrayList<Word>(), onNoteListener);
}

public void addItems(List<Word> items) {
wordList.addAll(items);
notifyDataSetChanged();
}

public void clear() {
wordList.clear();
notifyDataSetChanged();
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
int resource = wordList.get(position).getImageResource();
String title = wordList.get(position).getTitle();
viewholder.setData(resource, title);
}

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

static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private ImageView imageView;
private TextView title;

private OnNoteListener onNoteListener;

public ViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);

imageView = itemView.findViewById(R.id.imageView);
title = itemView.findViewById(R.id.word);
this.onNoteListener = onNoteListener;

itemView.setOnClickListener(this);
}

private void setData(int resource, String titleText) {
imageView.setImageResource(resource);
title.setText(titleText);
}

@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}

public interface OnNoteListener {
void onNoteClick(int position);
}
}

我知道我错了:(。我是初学者,请容忍这些并推荐解决方案。在此处输入代码

最佳答案

试试这个,

在这里,我将 recyclerview 项目的相应标题传递给 Activity 。这样我们就可以在 MainActivity 中处理了。

在您的 WordAdapter 类中,将下面的方法插入 ViewHolder

@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition(), wordList.get(position).getTitle());
}

然后像下面的代码一样改变你的interface参数,

 public interface OnNoteListener {
void onNoteClick(int position, String title);
}

紧随其后的是 MainActivity,从 WordAdapter 将值集 Intent 传递给适当的 Activity ,如下面的代码所示。

@Override
public void onNoteClick(int position, String title) {
Log.d(TAG, "clicked on the position:" + position);
if(title.equalsIgnoreCase("dd"){
Intent myIntent = new Intent(MainActivity.this, abc.class);
myIntent.putExtra("dd", 1);
MainActivity.this.startActivity(myIntent);
}
else {
Intent feedbackIntent = new Intent(MainActivity.this, def.class);
feedbackIntent.putExtra("ss", 0);
MainActivity.this.startActivity(feedbackIntent);
}

}

关于java - 在回收者 View 中设置 Intent ,OnCreate 以转到特定列表(行)的另一个特定 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687320/

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