簡體   English   中英

AutoCompleteTextView不顯示從內部存儲器加載的字符串

[英]AutoCompleteTextView is not showing strings loaded from the Internal Storage

我想制作一個AutoCompleteTextview,它將從內部存儲加載先前保存的建議。 我成功地將字符串從內部存儲設備加載到字符串數組(我使用日志記錄來檢查...。)。

然后當我將字符串數組加載到適配器並將適配器設置為AutoCompleteTextview時,之后AutoCompleteTextview不會顯示我從內部存儲中加載的建議字符串,但會顯示我已加載的建議字符串到運行時的字符串數組。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //this is the array where i am trying to save my data into 
    loadedString=new String[1];
    loadedString[0]="the pre-loaded String"

    adapter = new ArrayAdapter<String>(this,   android.R.layout.simple_list_item_1, loadedString);


    atcv = (AutoCompleteTextView) findViewById(R.id.autocomplete);

    int objectCounter = 0;       

    try {

        FileInputStream fis = this.openFileInput(FILENAME);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);

        String temp;


        while ((temp = br.readLine()) != null) {
        //here i am calculating the lines of my data-file 
        //as 1 line contains 1 string object
        //so that i can initialize the string array


         objectCounter++;
        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //intializing the array
    // objectCounter+1 and index =1 because i loaded an object before

      loadedString = new String[objectCounter+1];

     int index = 1;  


    try {

        FileInputStream fis = this.openFileInput(FILENAME);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);


        String temp;


        while ((temp = br.readLine()) != null) {


            loadedString[index] = temp;
            index++;
        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }





    adapter.notifyDataSetChanged();
    atcv.setAdapter(adapter);
    atcv.setThreshold(1);


}

這是我用來保存數據的方法

 public void saver(View view) {

      String string;


         if(actv.getText()!=null){
            advice = advices.getText().toString();}

        advice=advice+"\n";


        try{

        FileOutputStream fos = openFileOutput(FILENAME, context.MODE_APPEND);

        fos.write(advice.getBytes());
        fos.close();
            Toast.makeText(this, "File  saved" , Toast.LENGTH_LONG).show();

        }


    catch (Exception e){
        Toast.makeText(this, "File could not be saved", Toast.LENGTH_LONG).show();
    }

請幫忙。 請注意,我使用了notifyDataSetChanged()方法。 我將非常感激。

請注意,我使用notifyDataSetChanged()方法

在您的情況下,這是沒有用的。 ArrayAdapter將繼續使用您的舊String[1] 它不知道您的新String[]

首先,不要像在此處那樣在主應用程序線程上執行磁盤I / O。 使用某種形式的后台操作,例如AsyncTask

其次,不要兩次讀取數據,因為這對用戶而言是兩倍的速度。 例如,您可以使用ArrayList<String>這樣的數據結構,它可以動態擴展其大小。

然后,在加載字符串之后才創建ArrayAdapter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM