1

テキストファイル (rollcall.txt) からの情報で更新したいリストビューがあります。rollcall.txt が更新されるたびに、rollcall() (以下のコード) を呼び出しています。rollcall() が呼び出される前に、テキスト ファイル内のデータが正しく更新されていることを確認しました。私が抱えている問題は、次に rollcall() を呼び出すまでリストビューに更新されたエントリが表示されないことです (IE は常に 1 更新ステップ遅れているように見えます)。

どこが間違っていますか?

   public void rollcall(){
             String[] splitdata = null;
            try{
               File myFile = new File(Environment.getExternalStorageDirectory() + "/rollcall.txt");
                    FileInputStream fIn = new FileInputStream(myFile);
                    BufferedReader myReader = new BufferedReader(
                            new InputStreamReader(fIn));
                    String aDataRow = "";
                    String aBuffer = "";
                    while ((aDataRow = myReader.readLine()) != null) {
                        aBuffer += aDataRow + "\n";
                    }   
                    splitdata = aBuffer.split("`"); //recover the file and split it based on `
                    myReader.close();
            }
               catch (Exception e) {
                      Toast.makeText(getBaseContext(), e.getMessage(),
                              Toast.LENGTH_SHORT).show();
              }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.logbooklayout, splitdata);
            lv1.setAdapter(adapter);
            adapter.notifyDataSetChanged(); //called to ensure updated data is refreshed into listview without reload

編集: rollcall はこのメソッドから呼び出されます:

  public void onClick(View v) {
if (v==badd){
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.setTitle("ROLLCALL"); //Set Alert dialog title here
                    alert.setMessage("Enter data: "); //Message here

                    // Set an EditText view to get user input 
                    final EditText input = new EditText(this);
                    alert.setView(input);

                    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                     //You will get as string input data in this variable.
                     // here we convert the input to a string and show in a toast.
                     add = input.getEditableText().toString();
                     try {
                        File myFile = new File(Environment.getExternalStorageDirectory() + "/rollcall.txt");
                        myFile.createNewFile();
                        FileOutputStream fOut = new FileOutputStream(myFile, true);
                        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                        myOutWriter.append(add);
                        myOutWriter.append("`");  // ` used to split the file down later in lv section
                        myOutWriter.close();
                        fOut.close();
                }
                catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                    }               
                    } // End of onClick(DialogInterface dialog, int whichButton)
                }); //End of alert.setPositiveButton
                    alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                          dialog.cancel();
                      }
                }); //End of alert.setNegativeButton
                    AlertDialog alertDialog = alert.create();
                    alertDialog.show();
                    rollcall();
            }//end badd 
}

助けてくれてありがとう、私はarrayadaptersを使うのが初めてです。

アンディ

4

3 に答える 3

1

adapter.notifyDataSetChanged()アダプタを更新するたびに呼び出すと、リストビューが自動的に更新されます

于 2013-07-19T07:34:59.863 に答える
1

2 つの理由から、rollcall を非同期タスクとして実行することをお勧めします。まず、実行中に UI を停止しませんrollcall()

次に、onPostExecute(Object o)`adapter.notifyDataSetChanged();を呼び出すことができる場所を呼び出すことができます。'

于 2013-07-25T19:14:20.543 に答える