Skip to content Skip to sidebar Skip to footer

Trouble With Listview

I have an issue with my Main Activity and a ListView and I absolutly don't understand how it works... OK ! This is what I want : Expectation It means I need a list of item : Tex

Solution 1:

listview has to have its own layout, so you need to create a new layout that just contains listview and in getView() method use it as a row of your listView

1- create a new row_listview.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".activities.MainMenu"><TextViewandroid:id="@+id/MainDeckNom"android:layout_width="wrap_content"android:layout_height="wrap_content" /></android.support.constraint.ConstraintLayout>

2-Change this line

view = inflater.inflate(R.layout.activity_main_menu, null);

to

view = inflater.inflate(R.layout.row_listview, null);

Solution 2:

In your adapter's getView() method, you have to design a child_view layout. The layout which you return will be show in every item of the list. Because you're returning main_activity_layout so every your item would show the mainAcitivy, which contains a button. Solution is create new layout with your own design and replace it with R.layout.main_acitivity_layout. Good luck

Solution 3:

You should not use R.layout.activity_main_menu in getView, because getView returns a line of the list which you don't want to have a button.

So, you have a separate layout activity_main_men specified as Content view for the activity which does not have TextView:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".activities.MainMenu"><ListViewandroid:id="@+id/MainDecksList"android:layout_width="363dp"android:layout_height="426dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:layout_marginEnd="8dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_marginEnd="8dp"android:layout_marginBottom="8dp"android:text="Button"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toBottomOf="@+id/MainDecksList" /></android.support.constraint.ConstraintLayout>

The in getView instead of

view = inflater.inflate(R.layout.activity_main_menu, null);

you just place:

view=new TextView()
view.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Post a Comment for "Trouble With Listview"