2015-02-10 59 views
0

我想從.txt文件填充ListView但不知何故失敗。 讀取的文件已正確添加到ArrayList。我已嘗試ArrayAdapterArrayList並將其設置爲OnCreate()下的ListView的適配器,並在列表更新後調用notifyDataSetChanged()ListView將不會填充(來自文件)

我是相當新到Java,我更習慣於(並且更喜歡)C#

這裏是我的代碼部分:

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> 

兩天我嘗試這一點,如果沒有之間的每一個嘗試任何區別...

非常感謝您的支持,非常感謝。

---- ----編輯

更新的代碼:(不好意思,只顯示部分,這是一個龐大而凌亂的代碼的一部分...)

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(); 
    } 
} 

- ---編輯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); 
    } 

回答

1

呼叫ArrayAdapter.addAll方法調用notifyDataSetChanged前添加ListView中的當前適配器新項目:

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

TheJ0y,您呼叫notifyDataSetChanged無所添加到適配器..繁榮K是在這裏.. Upvoted他.. – 2015-02-10 04:31:28

+0

謝謝,這實際上是一個缺失的部分。但是我仍然沒有得到任何東西:((沒有15個代表,我不知道該怎麼辦)對不起, – TheJ0y 2015-02-10 04:32:14

+0

@ TheJ0y:顯示更新後的代碼,並在調用addAll之前檢​​查'arrayList'中有多少物品 – 2015-02-10 04:36:09