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>
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
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);
}
}
I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With