大家好,我正在尝试在回收器视图的适配器类中使用意图从一种活动转移到另一种活动,但是这样做却出现son = me错误,以下是错误:
1.PakistaniActivity不是封闭类。
2.无法解析方法StatrActivity();
这是我的代码:
巴基斯坦活动
package com.recycler.hp.navigationbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class PakistaniActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pakistani);
RecyclerView rl=findViewById(R.id.list);
rl.setLayoutManager(new LinearLayoutManager(this));
String[] languges={"Badnamiyan", "song2","song3","song4","Song5","song6","song7","song8","song9"};
rl.setAdapter(new Adaptor(languges));
}
}
适配器.java
package com.recycler.hp.navigationbar;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.net.URL;
public class Adaptor extends RecyclerView.Adapter<Adaptor.viewholder> {
private String[] data;
public Adaptor(String[] data) {
this.data = data;
}
@Override
public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_item1, parent, false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(viewholder holder, final int position) {
String title = data[position];
holder.t.setText(title);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long x;
x=getItemId(position);
if(x==0)
{
Intent intent=new Intent(PakistaniActivity.this,Revival_Activity.class);
startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return data.length;
}
public class viewholder extends RecyclerView.ViewHolder {
ImageView i;
TextView t;
public viewholder(View itemView) {
super(itemView);
i = itemView.findViewById(R.id.img1);
t = itemView.findViewById(R.id.main1);
}
}
}
最佳答案
尝试不启动适配器内部的活动。您可以使用此适配器:
public abstract class ClickableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private ClickItemListener clickItemListener;
public void setClickItemListener(ClickItemListener clickItemListener) {
this.clickItemListener = clickItemListener;
}
@Override
public void onBindViewHolder(@NonNull final VH holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickItemListener != null)
clickItemListener.onClickItem(holder.itemView, holder.getAdapterPosition());
else
throw new IllegalStateException("Click listener not set");
}
});
}
public interface ClickItemListener {
void onClickItem(View v, int position);
}
}
然后使您的适配器扩展我的:
public class Adaptor extends ClickableAdapter<Adaptor.viewholder> {
private String[] data;
public Adaptor(String[] data) {
this.data = data;
}
@Override
public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_item1, parent, false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(viewholder holder, final int position) {
super.onBindViewHolder(holder,position);
String title = data[position];
holder.t.setText(title);
}
@Override
public int getItemCount() {
return data.length;
}
public class viewholder extends RecyclerView.ViewHolder {
ImageView i;
TextView t;
public viewholder(View itemView) {
super(itemView);
i = itemView.findViewById(R.id.img1);
t = itemView.findViewById(R.id.main1);
}
}
}
最后,在您的活动中,可以设置适配器的行为:
Adaptor adapter = new Adaptor(languges)
rl.setAdapter(adapter);
adapter.setClickItemListener(new ClickableAdapter.ClickItemListener() {
@Override
public void onClickItem(View v, int position) {
if(position==0)
{
Intent intent=new Intent(PakistaniActivity.this,Revival_Activity.class);
startActivity(intent);
}
}
});