Reputation: 303
I like to get the positions i try using simple toast and i got it, but when i try to using it for interface i got an error
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 public class ListAdapter extends 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);
}
}
and mostly when i try to getAdapterPositions the error i got is like this.
The Error
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)
how do i getAdapter positions? is there something wrong with my interface?
Upvotes: 2
Views: 66
Reputation: 3234
The problem appears to be that your monClickListenerAdapterList
is just null.
If you look at the constructor of your ListAdapter
class, you'll see:
public ListAdapter(AppCompatActivity someActivity, ArrayList<DataFirebase> dataList,
OnClickListenerAdapterList onClickListenerAdapterList) {
this.dataList = dataList;
this.someActivity = someActivity;
simpleway = new Simpleway();
}
However, notice that although you passed in OnClickListenerAdapterList onClickListenerAdapterList
as a parameter, you never called this.onClickListenerAdapterList = onClickListenerAdapterList;
inside the constructor.
As such, you're simply passing in the listener, but never storing it... so the ListAdapter's onClickListenerAdapterList is actually still null.
So when you pass in that onClickListenerAdapterList to your ViewHolders, you're actually passing in the onClickListenerAdapterList that's still null.
So to fix your error, simply change your constructor to be:
public ListAdapter(AppCompatActivity someActivity, ArrayList<DataFirebase> dataList,
OnClickListenerAdapterList onClickListenerAdapterList) {
this.dataList = dataList;
this.someActivity = someActivity;
this.onClickListenerAdapterList = onClickListenerAdapterList;
simpleway = new Simpleway();
}
Upvotes: 1