为什么我的listview没有更新?

时间:2015-04-14 17:26:21

标签: java android android-fragments android-listview

我的listview出了问题。当我按下一个按钮时,它应该将一个项目添加到列表视图中,但它不会。我使用了Toast来确保按钮的onClick事件是好的。谁能帮我 ? 这是片段的java代码。

package com.wordpress.softwarebycs.i_cseditor;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Cosmin on 11.04.2015.
 */
public class code_Fragment extends Fragment implements AdapterView.OnItemSelectedListener{


    View rootview;
    WebView previewBrowser;
    EditText codebox,fname;
    Button save_btn;
    Spinner spinner;

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> list_adapter;
    ListView projects;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
    {



        rootview = inflater.inflate(R.layout.code_layout,container, false);
        previewBrowser = (WebView)rootview.findViewById(R.id.previewBrowser);
        codebox= (EditText)rootview.findViewById(R.id.codebox);
        projects=(ListView)rootview.findViewById(R.id.projectsListView);

        list_adapter=new ArrayAdapter<String>(rootview.getContext(), android.R.layout.simple_list_item_1, list);
        projects.setAdapter(list_adapter);
        list.clear();


        save_btn=(Button)rootview.findViewById(R.id.save_btn);
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeFile();

            }

        });

        fname=(EditText)rootview.findViewById(R.id.fname);

        spinner= (Spinner)rootview.findViewById(R.id.spinner);


        ArrayAdapter adapter = ArrayAdapter.createFromResource(rootview.getContext(),R.array.spinner_items,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);



        final TabHost tabHost=(TabHost)rootview.findViewById(R.id.tabHost);
        tabHost.setup();
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                String file="i_C_s_editor_temp_file_ne_trebuie_un_nume_lung_tare_de_tot.html";
                try {
                    FileOutputStream fos = getActivity().openFileOutput(file, Context.MODE_PRIVATE);
                    fos.write(codebox.getText().toString().getBytes());
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                previewBrowser.loadUrl("file:///data/data/com.wordpress.softwarebycs.i_cseditor/files/"+file);

            }
        });
        TabHost.TabSpec spec1=tabHost.newTabSpec("Code");
        spec1.setContent(R.id.codeTab);
        spec1.setIndicator("Code");

        TabHost.TabSpec spec2=tabHost.newTabSpec("Preview");
        spec2.setContent(R.id.previewTab);
        spec2.setIndicator("Preview");

        TabHost.TabSpec spec3=tabHost.newTabSpec("File");
        spec3.setContent(R.id.fileTab);
        spec3.setIndicator("File");

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);


        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#ffe0e0e0"));
        }


        projects.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {
                TextView clickedView = (TextView) view;
                readFile(clickedView.getText().toString());
                //tabHost.setCurrentTab(0);
            }
        });

        return rootview;

    }

    public void writeFile()
    {
        String FILE_NAME=fname.getText().toString()+spinner.getSelectedItem().toString();
        try {
            FileOutputStream fos = getActivity().openFileOutput(FILE_NAME, Context.MODE_PRIVATE);

            fos.write(codebox.getText().toString().getBytes());
            list.add(FILE_NAME);
            Toast toast = Toast.makeText(rootview.getContext(),
                    "File saved!",
                    Toast.LENGTH_SHORT);

            toast.show();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public void actionStopped(){
        try {
            FileOutputStream fos = getActivity().openFileOutput("cfg2.txt", Context.MODE_PRIVATE);
            Integer k;
            for(k=0;k<list.size();k++)
            {fos.write(list.get(k).toString().getBytes());
                fos.write(System.getProperty("line.separator").getBytes());}
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    public void actionResummed() {
        list.clear();
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput("cfg2.txt")));
            String line;
            while ((line = bReader.readLine()) != null) {
                list.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        actionResummed();
    }

    @Override
    public void onPause() {
        super.onPause();
        actionStopped();
    }

    public void readFile(String path)
    {

        codebox.setText("");
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput(path)));
            String line;
            while ((line = bReader.readLine()) != null) {
                codebox.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

这是片段的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#ff666666">

    <TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tabHost"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="#ff8a3e6b">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

            <LinearLayout
                    android:id="@+id/codeTab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                    <EditText
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:inputType="textMultiLine"
                        android:ems="10"
                        android:id="@+id/codebox"
                        android:gravity="top|left"
                        android:background="#ffffffff"
                        android:scrollbars="vertical|horizontal"/>
                </LinearLayout>

            <LinearLayout
                    android:id="@+id/previewTab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">
                    <WebView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:id="@+id/previewBrowser"
                        android:scrollbars="vertical|horizontal"
                        ></WebView>

                </LinearLayout>
<!--fileTab-->
            <LinearLayout
                    android:id="@+id/fileTab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#ff666666"
                    android:orientation="vertical"
                        >

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/linearLayout"
                        android:layout_marginBottom="20dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:text="File&apos;s Name"
                            android:id="@+id/textView2"
                            android:layout_gravity="center_horizontal"
                            android:textColor="#ffe0e0e0"
                            android:layout_below="@+id/linearLayout"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="20dp" />
                    </LinearLayout>

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/linearLayout"
                        android:layout_centerHorizontal="true"
                        android:id="@+id/linearLayout2">

                        <EditText
                            android:layout_width="237dp"
                            android:layout_height="41dp"
                            android:id="@+id/fname"
                            android:layout_marginTop="10dp"
                            android:background="#ff8a3e6b"
                            android:textColor="#ffe0e0e0" />

                        <Spinner
                            android:layout_width="fill_parent"
                            android:layout_height="41dp"
                            android:id="@+id/spinner"
                            android:background="#ff8a3e6b"
                            android:layout_marginTop="9dp"
                          />
                    </LinearLayout>

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/linearLayout2"
                        android:layout_centerHorizontal="true"
                        android:id="@+id/linearLayout3">


                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Save File"
                            android:id="@+id/save_btn"
                            android:layout_gravity="center"
                            android:layout_marginTop="10dp"
                            android:background="#ff8a3e6b"
                            android:textColor="#ffe0e0e0"
                        />
                    </LinearLayout>
<!-- separator-->
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:id="@+id/separator"
                    android:background="#ff8a3e6b"
                    android:layout_marginTop="5dp" />
                <!--open file -->
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/separator"
                    android:layout_centerHorizontal="true"
                    android:id="@+id/linearLayout4">

                    <TextView
                        android:paddingTop="20dp"
                        android:paddingBottom="20dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/text2"
                        android:textSize="25dp"
                        android:text="Select file to open!"
                        android:layout_gravity="top|center"
                        />

                    <ListView
                        android:layout_width="fill_parent"
                        android:layout_below="@+id/text2"
                        android:layout_height="fill_parent"
                        android:id="@+id/projectsListView"
                        android:scrollbars="vertical|horizontal"
                        android:background="#ff8a3e6b">
                    </ListView>
                </LinearLayout>
            </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

要更新列表视图,您必须通知它,我在点击方法中看不到通知方法

中添加以下代码
save_btn.setOnClickListener //Method after calling to writeFile() Method

  list_adapter.notifyDataSetChanged();