Custom Listview Android Fails
so I have made a List view, called it 'neighborhood'. it supposed to show a list of names of houses, number, and their passwords. for it, I have made a layout with 3 textviews. the
Solution 1:
At first change the listview height to wrap_content like this
<ListView
android:id="@+id/neighborhood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/allAddresses"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/AddresSearch" />
You have created another 3 different lists in the adapter constructor. The parent list always null. In this case, the app goes to crash. Without creating new list set values on parent list by using this before the list variable.
And then the adapter should be updated to this:
classCustomAdapterextendsBaseAdapter{
ArrayList<String> houseList ;
ArrayList<String> numList ;
ArrayList<String> passList ;
publicCustomAdapter(ArrayList<String>list1,ArrayList<String>list2,ArrayList<String>list3){
this.houseList = list1;
this.numList = list2;
this.passList = list3;
}
@OverridepublicintgetCount() {
return houseList.size();
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int i, View view, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.activity_pass_item_view,null);
TextViewhouse= (TextView) view.findViewById(R.id.house);
TextViewnum= (TextView) view.findViewById(R.id.num);
TextViewpass= (TextView) view.findViewById(R.id.pass);
house.setText(houseList.get(i).toString());
num.setText(numList.get(i).toString());
pass.setText(passList.get(i).toString());
return view;
}
}
Hope this will help.
Solution 2:
You done silly mistake that's why you are not able to get views. Change there follow methods
@OverridepublicintgetCount() {
return <HERE YOU HAVE TO GIVE SIZE>;
// like return houseList.size();
}
@Overridepublic Object getItem(int position) {
return <HERE YOU HAVE TO GIVE POSITION TO GET ITEM>;
// like return houseList.get(position);
}
@OverridepubliclonggetItemId(int position) {
return <HERE YOU HAVE TO PASS POSITION ONLY>;
// like return position;
}
And your error solve.
Post a Comment for "Custom Listview Android Fails"