ListView不会填充(来自文件)

时间:2015-02-10 04:16:47

标签: java android listview android-listview

我正在尝试从.txt文件填充 ListView ,但不知何故失败了。 文件读取并正确添加到 ArrayList 。我已经尝试 ArrayAdapter ArrayList 并将其设置为OnCreate()下的ListView的适配器,并在更新列表后调用notifyDataSetChanged()

我对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);
}

//&LT; ...&GT;

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

//&LT; ...&GT;

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

//&LT; ...&GT;

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

//&LT; ...&GT;

<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 个答案:

答案 0 :(得分:1)

调用ArrayAdapter.addAll方法在调用notifyDataSetChanged之前在ListView的当前适配器中添加新项:

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