Omer
Omer

Reputation: 317

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 three lists that CustomAdapter class gets are from SQL, which works(i debugged it and it gets 3 full lists)

EDIT: now it just fails, and it gives me this error. I have changed the code to the suggested answers, and it fixed the last problems

the list:

<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" />

this is the onCreate:

ListView neighborhood = (ListView)findViewById(R.id.neighborhood);
CustomAdapter customAdapter= new CustomAdapter(houseList,numList,passList);
neighborhood.setAdapter(customAdapter);

this is the Adapter Helper class:

   class CustomAdapter extends BaseAdapter{

    ArrayList<String> houseList ;
    ArrayList<String> numList ;
    ArrayList<String> passList ;

    public CustomAdapter(ArrayList<String>list1,ArrayList<String>list2,ArrayList<String>list3){
        this.houseList = list1;
        this.numList = list2;
        this.passList = list3;

    }

    @Override
    public int getCount() {
        return houseList.size();
    }

    @Override
    public Object getItem(int position) {
        return houseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        view = getLayoutInflater().inflate(R.layout.activity_pass_item_view,null);

        TextView house = (TextView) view.findViewById(R.id.house);
        TextView num = (TextView) view.findViewById(R.id.num);
        TextView pass = (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 null;
    }
}

and this is the custom layout for the listview: contains only 3 text view:

<TextView
    android:id="@+id/num"
    android:layout_width="86dp"
    android:layout_height="45dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="28dp"
    app:layout_constraintEnd_toStartOf="@+id/house"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/house"
    android:layout_width="86dp"
    android:layout_height="45dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/pass"
    android:layout_width="86dp"
    android:layout_height="45dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toStartOf="@+id/num"
    app:layout_constraintTop_toTopOf="parent" />

EDIT: this is the error it gives me now:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.omer.deliverypasses, PID: 17510 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference at android.widget.AbsListView.obtainView(AbsListView.java:2363) at android.widget.ListView.measureHeightOfChildren(ListView.java:1280) at android.widget.ListView.onMeasure(ListView.java:1188) at android.view.View.measure(View.java:18788) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) at android.view.View.measure(View.java:18788) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thank you very much

Upvotes: 0

Views: 80

Answers (2)

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

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:

class CustomAdapter extends BaseAdapter{

    ArrayList<String> houseList ;
    ArrayList<String> numList ;
    ArrayList<String> passList ;

    public CustomAdapter(ArrayList<String>list1,ArrayList<String>list2,ArrayList<String>list3){
        this.houseList = list1;
        this.numList = list2;
        this.passList = list3;

    }

    @Override
    public int getCount() {
        return houseList.size();
    }
   @Override
    public long getItemId(int position) {
      return position;
     }
    @Override
    public View getView(int i, View view, ViewGroup parent) {
        view = getLayoutInflater().inflate(R.layout.activity_pass_item_view,null);

        TextView house = (TextView) view.findViewById(R.id.house);
        TextView num = (TextView) view.findViewById(R.id.num);
        TextView pass = (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.

Upvotes: 1

Sanwal Singh
Sanwal Singh

Reputation: 1793

You done silly mistake that's why you are not able to get views. Change there follow methods

 @Override
public int getCount() {
    return <HERE YOU HAVE TO GIVE SIZE>;
    // like return houseList.size();
}

@Override
public Object getItem(int position) {
    return <HERE YOU HAVE TO GIVE POSITION TO GET ITEM>;
    // like return houseList.get(position);
}

@Override
public long getItemId(int position) {
    return <HERE YOU HAVE TO PASS POSITION ONLY>;
     // like return position;
}

And your error solve.

Upvotes: 1

Related Questions