therealone
therealone

Reputation: 421

ArrayList Adapter (for ListView) only displaying the first added item

Edit3: My own item list layout:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/text1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceListItemSmall"
      android:gravity="center_vertical"
/>

Edit2: Honestly, I might just make a for-loop that creates buttons for each file. This is way too much of a headache to be worth the time.

Edit: I'd like to emphasize the fact that I've copy+pasted my exact code into a new test app and it works fine. That might give you guys a clue.

After tons of debugging, I've narrowed down a problem. I'm trying to add items (files) to an ArrayList, then put that into an ArrayAdapter, and finally display the items in a ListView. The problem is that only the first added item is being displayed.

Here's how I was trying to do it:

ListView listView = (ListView) view.findViewById(R.id.templateFilesList);

    ArrayList<String> templateList = new ArrayList<>();



    File myDir = getContext().getFilesDir();
    File[] templateFiles = myDir.listFiles();

    int length = templateFiles.length;

    for(int i = 0; i < length; i++){
        templateList.add(templateFiles[i].getName());
    }

    ArrayAdapter arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
            templateList);

    listView.setAdapter(arrayAdapter);

The templateFiles[] array properly gets the 8 files in Internal Storage (confirmed via logcat/debugger), but only the first item is displayed in the ListView. Here's a clearer look at the issue:

// If I replace the for loop with this:

    templateList.add(templateFiles[1].getName());
    templateList.add(templateFiles[0].getName());

Only templateFiles[1] is displayed. Similarly, if I do this:

    templateList.add(templateFiles[0].getName());
    templateList.add(templateFiles[1].getName());

Only templateFiles[0] is displayed. Any ideas on what's going wrong?

Here's my XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.template_housing.MyTemplatesFrag"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    >

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_templates"
        android:textSize="34sp"
        android:textColor="@color/black"
        android:background="@color/Gold1"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:layout_marginBottom="10dp"
/>

<ListView
        android:id="@+id/templateFilesList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
>

</ListView>

Upvotes: 5

Views: 2871

Answers (5)

Veer
Veer

Reputation: 1383

I copied your code and ran it in Android Studio. It seems NO problem.

Here is my activity code(same xml code), the only difference is that I use an Activity, you may use a Fragment.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.templateFilesList);
    ArrayList<String> templateList = new ArrayList<>();

    File myDir = getFilesDir();
    File[] templateFiles = myDir.listFiles();
    int length = templateFiles.length;

    //case 1: in my project, length is 1, show only one element, see case2.
//        for (int i = 0; i < length; i++) {
//            templateList.add(templateFiles[i].getName());
//        }

    //case 2: try to add simple strings, 
    //because you said put simple strings in the list could cause the problem, too.
    templateList.add("a1");
    templateList.add("a2");
    templateList.add("a3");

    ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            templateList);
    listView.setAdapter(arrayAdapter);
}
}

enter image description here

I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.

Upvotes: 1

Vickyexpert
Vickyexpert

Reputation: 3167

It may be possible that your array adapter is not setting properly and therefore it is not working properly, so try to replace your array adapter by below code and check if it is working,

   ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, templateList);

Upvotes: 0

Cynapsis
Cynapsis

Reputation: 118

Can you post the simple list item 1 XML file too. This issue is not Java code related it has something to do with XML and size of elements. Try deleting that text view on top of the list view and your simple list item view should have wrap content as layout height.

And if you still want to use the textview you can use weight attribute. Give your textview a weight of 0 and the list view a weight of 1.

Upvotes: 0

sumandas
sumandas

Reputation: 565

Try to convert it to an array of Strings:

String [] templateListArray = templateList.toArray(new String[templateList.size()]);

and then do the assignment as follows:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
            templateListArray);

Hope this will fix it.

Upvotes: 0

Jorge Mendez
Jorge Mendez

Reputation: 674

If you are updating the content of your adapter you should be calling notifyDataSetChanged() as well to make sure your adapter gets to know that your content did change

Upvotes: 1

Related Questions