ListView won't populate (from file)

TheJ0y

I am trying to populate a ListView from a .txt file but somehow fail to it. The file read and is added to the ArrayList properly. I have tried the ArrayAdapter to the ArrayList and set it as the Adapter of the ListView under OnCreate() and call notifyDataSetChanged() after the list is updated.

I'm fairly new to java, I'm more used to (and prefer) C#

Here's parts of my code:

public class MainActivity extends ActionBarActivity{

ArrayAdapter<String> arrayAdapter = null;
ArrayList<String> arrayList = null;
ListView listView = null;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    listView = (ListView) findViewById(R.id.lv_Run);
    arrayList = new ArrayList<String>();
    arrayList.clear();
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(arrayAdapter);
}

//<...>

            if (file.isFile() && file.getName().endsWith(".txt")){
                Button btn = new Button(ll1.getContext());
                btn.setText(file.getName().replace(".txt", ""));
                btn.setTag(file.getName().replace(".txt", ""));
                btn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
                        String btnString = v.getTag().toString();
                        UpdateList(btnString);
                    }
                });
                btn.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
                ll1.addView(btn);
            }

//<...>

public void UpdateList(String btnString){
    try{
        File file = new File(runs.getAbsolutePath()+ File.separator + btnString + ".txt");
        arrayList = getFileContent(file.getAbsolutePath());
        arrayAdapter.notifyDataSetChanged();
    catch (IOException e){
        e.printStackTrace();
    }
}

//<...>

public static ArrayList<String> getFileContent(String fileName) throws IOException{
    ArrayList<String> result = new ArrayList<>();
    File aFile = new File(fileName);
    BufferedReader reader;  
    String aLine;

    if (!aFile.isFile()){
        return result;
    }
    try{
        reader = new BufferedReader(new FileReader(aFile));
    }
    catch (FileNotFoundException e1){
        e1.printStackTrace();
        return result;
    }
    while ((aLine = reader.readLine()) != null){
        result.add(aLine + "\n");
    }
    reader.close();

    return result;
}

//<...>

<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=".MainActivity"
            android:background="#ff000000"
            android:baselineAligned="false"
            android:id="@+id/pan_MasterPan">

    <LinearLayout android:orientation="vertical"
                android:layout_weight="2.5"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/pan_Left">

        <LinearLayout android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="top"
                android:layout_weight="8"
                android:id="@+id/pan_SelRun">

            <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/select_run"
            android:id="@+id/btn_LoadRun"
            android:clickable="true"
            android:onClick="runSelectClick"/>
        </LinearLayout>

        <LinearLayout android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:focusableInTouchMode="false"
                android:id="@+id/pan_RunPOI">

            <ListView
            android:id="@+id/lv_Run"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="visible"
            android:layout_weight="1"
            android:dividerHeight="1dp"
            android:drawSelectorOnTop="true"
            android:clickable="true"
            android:choiceMode="singleChoice"
            android:divider="@android:color/black"/>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:background="#ff1A1A1A"
            android:id="@+id/pan_Right">

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ForDebug"
        android:text="@string/debugbtnstrg"
        android:clickable="true"
        android:onClick="ForDebug_OnClick"
        android:longClickable="false"
        android:textStyle="italic"/>

    </LinearLayout>

</LinearLayout>

Two days I try this and that without any difference between every try...

Thanks a lot for you time, it's really appreciated.

---- Edit ----

Updated Code: (sorry to show only parts, this is part of a massive and messy code...)

public void UpdateList(String btnString){
    try{
        File file = new File(runs.getAbsolutePath()+ File.separator + btnString + ".txt");
        arrayList = getFileContent(file.getAbsolutePath());
        Toast.makeText(getApplicationContext(), "aL_Size: " + arrayList.size(), Toast.LENGTH_LONG).show(); 
//return 5
        arrayAdapter.addAll(arrayList);
        Toast.makeText(getApplicationContext(), "aA_Count: " + arrayAdapter.getCount(), Toast.LENGTH_LONG).show(); 
//return 5
        arrayAdapter.notifyDataSetChanged();
    catch (IOException e){
        e.printStackTrace();
    }
}

---- Edit 2 ----

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        listView = (ListView) findViewById(R.id.lv_Run);
        arrayList = new ArrayList<String>();
        arrayList.clear();
        arrayList.add("TEST1");
        arrayList.add("TEST2");
        arrayList.add("TEST3");
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(arrayAdapter);
    }
ρяσѕρєя K

Call ArrayAdapter.addAll method for adding new items in current adapter of ListView before calling notifyDataSetChanged :

   arrayList = getFileContent(file.getAbsolutePath());
   arrayAdapter.addAll(arrayList);
   arrayAdapter.notifyDataSetChanged();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Android: Populate a a ListView from a Database

분류에서Dev

Unable to populate data in listView from Database in Android

분류에서Dev

React: Why won't this basic data populate in my functional component?

분류에서Dev

QEMU guest system starts off /dev/sdc but won’t start from disk image file

분류에서Dev

How to populate jquery mobile listview from pouchdb data

분류에서Dev

How to populate database table from a file in eclipse?

분류에서Dev

StreamWriter won't write to log file

분류에서Dev

Word won't paste images from web?

분류에서Dev

Bash - populate 2D array from file

분류에서Dev

VI won't let me save my vimrc file

분류에서Dev

Code won't write to CSV file Python3, why?

분류에서Dev

Python won't write small object to file but will with large object

분류에서Dev

Supervisor open file limit won't change when using Chef

분류에서Dev

Excel 2010 Starts with a grey screen, won't open file

분류에서Dev

My ListView won't work ...it says: "Unfortunately your app has stopped"

분류에서Dev

React Native ListView won't update on sorting or after retrieving new data

분류에서Dev

Xcode won't download the exact HTML from website

분류에서Dev

UIPickerView won't show the data loaded from NSUserDefaults

분류에서Dev

Late 2015 iMac won't wake from sleep - black display

분류에서Dev

Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

분류에서Dev

Using javascript variable in mysql query and populate listview

분류에서Dev

DTD won't verify

분류에서Dev

String won't print

분류에서Dev

RequestContext won't work

분류에서Dev

Gimp won't launch

분류에서Dev

Windows - Can't delete folder from Recycle Bin and it won't restore

분류에서Dev

Drive won't boot from its Grub, but will from another drive's Grub, after cloning, why?

분류에서Dev

git agony part 2: what does it mean when git won't commit, pull or push a file?

분류에서Dev

Windows task scheduler won't run file on non-system drive

Related 관련 기사

  1. 1

    Android: Populate a a ListView from a Database

  2. 2

    Unable to populate data in listView from Database in Android

  3. 3

    React: Why won't this basic data populate in my functional component?

  4. 4

    QEMU guest system starts off /dev/sdc but won’t start from disk image file

  5. 5

    How to populate jquery mobile listview from pouchdb data

  6. 6

    How to populate database table from a file in eclipse?

  7. 7

    StreamWriter won't write to log file

  8. 8

    Word won't paste images from web?

  9. 9

    Bash - populate 2D array from file

  10. 10

    VI won't let me save my vimrc file

  11. 11

    Code won't write to CSV file Python3, why?

  12. 12

    Python won't write small object to file but will with large object

  13. 13

    Supervisor open file limit won't change when using Chef

  14. 14

    Excel 2010 Starts with a grey screen, won't open file

  15. 15

    My ListView won't work ...it says: "Unfortunately your app has stopped"

  16. 16

    React Native ListView won't update on sorting or after retrieving new data

  17. 17

    Xcode won't download the exact HTML from website

  18. 18

    UIPickerView won't show the data loaded from NSUserDefaults

  19. 19

    Late 2015 iMac won't wake from sleep - black display

  20. 20

    Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

  21. 21

    Using javascript variable in mysql query and populate listview

  22. 22

    DTD won't verify

  23. 23

    String won't print

  24. 24

    RequestContext won't work

  25. 25

    Gimp won't launch

  26. 26

    Windows - Can't delete folder from Recycle Bin and it won't restore

  27. 27

    Drive won't boot from its Grub, but will from another drive's Grub, after cloning, why?

  28. 28

    git agony part 2: what does it mean when git won't commit, pull or push a file?

  29. 29

    Windows task scheduler won't run file on non-system drive

뜨겁다태그

보관