2

I would like to implement a method that allows me to implement a listview with 2 different String-array from from strings.xml. My code works but my phone can only show the first 10 items. where is my problem? I f I try to run the code on a virtual device it works correctly.

Here is my code with only one textview. (this one shows only the first 10 items)

Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter , adapter_testi;
ImageView img_titoli;
List<RowData> rowData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_titoli);

    img_titoli = (ImageView) findViewById(R.id.img_titoli); // Create an icon
    Picasso.get().load(R.drawable.sfondo).resize(2048, 1356).centerInside().into(img_titoli);

    btt_backHome = (Button) findViewById(R.id.btt_backHome);
    lV_titoli = (ListView) findViewById(R.id.lV_titoli);
    eT_search = (EditText) findViewById(R.id.eT_search);

    //serve per nascondere la tastiera
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


    adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
    lV_titoli.setAdapter(adapter);
    adapter.getFilter().filter("");

    final Intent refresh = new Intent(this, activity_titoli.class);
    final Intent to_Home = new Intent (this , Activity_Main.class);
    final Intent to_Canzone_from_titoli = new Intent (this , activity_canzone.class);

    eT_search.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            (activity_titoli.this).adapter.getFilter().filter(arg0);
        }
    });

    btt_backHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(to_Home);
        }
    });

    lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Resources res = getResources();
            List<String> list = Arrays.asList(res.getStringArray(R.array.titoli));

            to_Canzone_from_titoli.putExtra("riga", list.indexOf(adapter.getItem(i)));
            startActivity(to_Canzone_from_titoli);

        }
    });
}

And this is my xml code:

<ImageView
        android:id="@+id/img_titoli"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btt_backHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:text="indietro"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="20dp" />

    <EditText
        android:id="@+id/eT_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:ems="10"
        android:hint="Ricerca..."
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ListView
                android:id="@+id/lV_titoli"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </ScrollView>
Twing90
  • 407
  • 2
  • 5
  • 15

2 Answers2

2
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ListView
                android:id="@+id/lV_titoli"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </ScrollView>

this is the problem. you have a listview nested inside a scroll view. you dont need to do that as a listview itsself scrolls. try this in place of that whole thing:

<ListView
            android:id="@+id/lV_titoli"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="128dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </ListView>
Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
0

Your problem is so straightforwardly simple: You don't have to put ListView in ScrollView to make it able to scroll, because ListView has its own class implementation of scrolling.

Delete this tag:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">        
</ScrollView>

And keep it like that:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lV_titoli"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
 </LinearLayout>

If you wish to dig deeper, please check this question: ListView inside ScrollView is not scrolling on Android

Good Luck!

khalid3e
  • 399
  • 3
  • 14