I have the problem that a ListView does not show my custom Array Adapter. I tested it with an standart ArrayAdapter, which worked. I also tested if the ArrayList with the data exists, which was also the case. ATM i think it's the Adapter, but i can't find the problem.
Where the List should be shown
public class LiSpellFragment extends Fragment {
private ArrayList<Spell> list;
public ListView lv_spellList;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_li_spell, container, false);
isList();
TestAdapter al = new TestAdapter(getContext(),R.layout.row_test, list);
lv_spellList.setAdapter(al);
return root;
}
void isList(){
if(this.list == null){
ArrayList<Spell> spellArrayList = new ArrayList<>();
Spell spell;
spell = new Spell("Hamlet");
spellArrayList.add(spell);
spell = new Spell("Carl");
spellArrayList.add(spell);
spell = new Spell("Frank");
spellArrayList.add(spell);
}
this.list = spellArrayList;
}
}
}
Custom Adapter Code
public class TestAdapter extends ArrayAdapter<Spell> {
ArrayList<Spell>list = new ArrayList<>();
int resource;
Context context;
public TestAdapter(@NonNull Context context, int resource, ArrayList<Spell> list) {
super(context, resource);
this.list = list;
this.resource = resource;
this.context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
TextView tv = (TextView) convertView.findViewById(R.id.textView2);
tv.setText(list.get(position).getName());
return convertView;
}
}
XML of the displayed row
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
class Spell
public class Spell {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}