0

I have an alertDialog with a ListView in it. By default it is showing all the items it can fit on dialog screen, but I would like to limit this to i.e. 3 items at a time. How can I achieve this? This is an excerpt of my code, not relevant parts are omitted

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) || (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER)){
        if (event.getAction() == KeyEvent.ACTION_UP){

                ArrayList<HashMap<String, String>> names = new ArrayList<HashMap<String, String>>(totalItems);

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                View rowList = getLayoutInflater().inflate(R.layout.activity_list, null);
                ListView listView = rowList.findViewById(R.id.listView);
                String[] from = new String[] { "title", "description" };
                int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
                int nativeLayout = R.layout.list_item;
                SimpleAdapter simpleAdapter = new SimpleAdapter(this, names, nativeLayout , from, to);
                listView.setAdapter(simpleAdapter);
                simpleAdapter.notifyDataSetChanged();
                alertDialog.setView(rowList);

                listDialog = alertDialog.show();

this is the listview layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorMenuBackground">
    <ListView
        android:id="@+id/listView"
        android:listSelector="@color/colorMenuBackgroundSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>
</RelativeLayout>
kekk0
  • 47
  • 7
  • You could limit the size of your listview in the layout to only display 3 items by giving it a fixed height. Instead of `android:layout_height="wrap_content"` you would put something like: `android:layout_height="144dp"` assuming one listview item is 48dp height. – Bruno Bieri Jul 06 '21 at 11:33
  • if you are using custom adapter `public int getCount() { return 6; }` – Usama Altaf Jul 06 '21 at 11:49
  • @UsamaAltaf I tried overriding method getCount, but it does not scroll the list anymore. It seems to be stuck to the number of items returned. – kekk0 Jul 06 '21 at 11:54
  • you want full list but you want to show 3 items on screen after scroll other items should visible then it's not possible – Usama Altaf Jul 06 '21 at 11:56
  • @BrunoBieri is there a way to make it more flexible, by not specifying actual height, just making it fit 3 items? – kekk0 Jul 06 '21 at 11:57
  • @kekk0 sure, you can get the item height at runtime and set it as the listview height as of your need. Maybe this can help you with that: https://stackoverflow.com/a/11470918/1306012 – Bruno Bieri Jul 06 '21 at 12:50

1 Answers1

0

just trim your names ArrayList before setting it as constructor param of SimpleAdapter

while(names.size() > 3){
    names.remove(0); //removes first item in arraylist
    //names.remove(names.size() - 1); //removes last item in arraylist
}

or more efficient way: create empty names and add only desired items

ArrayList<HashMap<String, String>> names = new ArrayList<>();
names.add(totalItems.get(0));
names.add(totalItems.get(2));
names.add(totalItems.get(4));
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Actually I don't want to reduce the items (around 30 atm), I need to still have them all, and I need to still be able to scroll through all of them. I just need to show 3 at a time instead of i.e. 7. Is there anything I can change, in the layout or overriding a method, tho achieve this? – kekk0 Jul 06 '21 at 11:30
  • 1
    you can set `android:layout_height="200dp"` for your root ` – snachmsm Jul 06 '21 at 12:05