onClickListenerAdapterList

onClickListenerAdapterList

我想得到我尝试使用简单的吐司的位置,但是知道了,但是当我尝试将其用于界面时,我得到了一个错误

ListActivity

public class ListActivity extends BaseActivity implements ListAdapter.OnClickListenerAdapterList {

private DatabaseReference databaseReference;
private RecyclerView labelRecycleView;
private ArrayList<DataFirebase> listLabel = new ArrayList<>();
private ListAdapter listAdapter;
private Button bInput;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    findView();
    setDataToList();
    initListener();
}

private void setDataToList() {
    simpleway.progressDialog(ListActivity.this);
    databaseReference.child("Username").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                DataFirebase modelClass = dataSnap.getValue(DataFirebase.class);
                modelClass.setLabel(dataSnap.getKey());
                listLabel.add(modelClass);
            }
            putLayoutManager();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            System.out.println(databaseError.getDetails() + " " + databaseError.getMessage());
            simpleway.stopProgressDialog();
        }
    });
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    labelRecycleView.setLayoutManager(layoutManager);
    labelRecycleView.setItemAnimator(new DefaultItemAnimator());
}

private void putLayoutManager() {
    listAdapter = new ListAdapter(ListActivity.this, listLabel, this);
    labelRecycleView.setAdapter(listAdapter);
    simpleway.stopProgressDialog();
}

@Override
public void findView() {
    databaseReference = FirebaseDatabase.getInstance().getReference();
    labelRecycleView = findViewById(R.id.listRecyclerVIew);
    bInput = findViewById(R.id.inputData);
}


@Override
public void initListener() {
    bInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            simpleway.startnextActivity(ListActivity.this, InputActivity.class);
        }
    });
}

@Override
public void OnClickPositionsAndValue(String enough) {
    TitleFragment titleFragment = new TitleFragment();
    FragmentTransaction fragmentTransaction =
            getSupportFragmentManager()
                    .beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putString("Check", enough);
    titleFragment.setArguments(bundle);
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right,
            R.anim.enter_from_right, R.anim.exit_to_right)
            .replace(R.id.container_layout, titleFragment)
            .addToBackStack(null)
            .commit();
}
}


列表适配器
    公共类ListAdapter扩展了RecyclerView.Adapter {

private Simpleway simpleway;
private ArrayList<DataFirebase> dataList;
private AppCompatActivity someActivity;
private Fragment fragmentActivity;
private OnClickListenerAdapterList onClickListenerAdapterList;

public ListAdapter(AppCompatActivity someActivity, ArrayList<DataFirebase> dataList,
                   OnClickListenerAdapterList onClickListenerAdapterList) {
    this.dataList = dataList;
    this.someActivity = someActivity;
    simpleway = new Simpleway();
}

public ListAdapter(Fragment fragmentActivity, ArrayList<DataFirebase> dataList,
                   OnClickListenerAdapterList onClickListenerAdapterList) {
    this.dataList = dataList;
    this.fragmentActivity = fragmentActivity;
    simpleway = new Simpleway();
}


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

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int positions) {
    final DataFirebase labelList = dataList.get(positions);

    viewHolder.textLabel.setText(labelList.getLabel());
    String enough = labelList.getLabel();

}

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

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    LinearLayout linearLabel;
    TextView textLabel;
    OnClickListenerAdapterList monClickListenerAdapterList;


    ViewHolder(@NonNull View itemView, OnClickListenerAdapterList onClickListenerAdapterList) {
        super(itemView);
        textLabel = itemView.findViewById(R.id.text_label);
        linearLabel = itemView.findViewById(R.id.linearLabel);
        monClickListenerAdapterList = onClickListenerAdapterList;

        linearLabel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        simpleway.toastMessage(someActivity.getApplicationContext(), String.valueOf(getAdapterPosition()));
        monClickListenerAdapterList.OnClickPositionsAndValue(getAdapterPosition());
    }
}

public interface OnClickListenerAdapterList{
    void OnClickPositionsAndValue(int positions);
}


}


大多数情况下,当我尝试获取getAdapterPositions时,我得到的错误是这样的。

错误

 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.parzival.flashcard.adapter.ListAdapter$OnClickListenerAdapterList.OnClickPositionsAndValue(int)' on a null object reference
    at com.example.parzival.flashcard.adapter.ListAdapter$ViewHolder.onClick(ListAdapter.java:90)


我如何获得适配器位置?我的界面有问题吗?

最佳答案

问题似乎是您的monClickListenerAdapterList仅为空。

如果查看ListAdapter类的构造函数,则会看到:

public ListAdapter(AppCompatActivity someActivity, ArrayList<DataFirebase> dataList,
                   OnClickListenerAdapterList onClickListenerAdapterList) {
    this.dataList = dataList;
    this.someActivity = someActivity;
    simpleway = new Simpleway();
}


但是,请注意,尽管您将OnClickListenerAdapterList onClickListenerAdapterList作为参数传递,但从未在构造函数内部调用this.onClickListenerAdapterList = onClickListenerAdapterList;

这样,您只是传递了侦听器,但从未存储它...因此ListAdapter的onClickListenerAdapterList实际上仍然为null。

因此,当您将该onClickListenerAdapterList传递给ViewHolders时,实际上是在传递仍然为null的onClickListenerAdapterList。

因此,要解决您的错误,只需将构造函数更改为:

public ListAdapter(AppCompatActivity someActivity, ArrayList<DataFirebase> dataList,
                       OnClickListenerAdapterList onClickListenerAdapterList) {
        this.dataList = dataList;
        this.someActivity = someActivity;
        this.onClickListenerAdapterList = onClickListenerAdapterList;
        simpleway = new Simpleway();
    }

09-19 08:25