2016-04-02 9 views
0

ここからAndroidで初心者です。ArrayAdapterを使用したListViewのライブアップデート

私はMainActivityを持っています。MainActivityはStringを生成し、それをArrayList(シングルトンパターン)に送ります。 HistoryActivityもあり、ArrayListの内容が表示されます。

ArrayListをArrayに送信し、activity_historyにHistoryActivityのArrayAdapterを持つListViewがあります。 activity_historyには、ArrayListから項目を削除(および再作成)し、ArrayList全体をクリアするボタンがあります。ただし、ListViewは自動的に更新されません(アクティビティは閉じられ、再度開く必要があります)。

私は現在((BaseAdapter) historyView.getAdapter()).notifyDataSetChanged();(下記参照)を試みていますが、動作しません。どんなアドバイスや指導も素晴らしいでしょう。

HistoryActivity.java

public class HistoryActivity extends AppCompatActivity { 

DataStorage history = DataStorage.getDataStorage(); 
String[] historyArray; 
private ListView historyView; 

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

@Override 
protected void onStart() { 
    super.onStart(); 
    historyView = (ListView) findViewById(R.id.history); 
    historyArray = history.getHistory().toArray(new String[history.getHistory().size()]); 
    historyView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, historyArray)); 
} 

/** 
* Clear all of history 
*/ 
public void clearHist(View view) { 
    history.getHistory().clear(); 
    historyArray = history.getHistory().toArray(new String[history.getHistory().size()]); 
    ((BaseAdapter) historyView.getAdapter()).notifyDataSetChanged(); 
} 

/** 
* Remove last entry to history 
*/ 
public void clearOne(View view) { 
    history.getHistory().remove(0); 
    historyArray = history.getHistory().toArray(new String[history.getHistory().size()]); 
    ((BaseAdapter) historyView.getAdapter()).notifyDataSetChanged(); 
} 

/** 
* Change to main view 
*/ 
public void sendMessage(View view) { 
    Intent intent = new Intent(this, MainActivity.class); 
    startActivity(intent); 
} 
} 

activity_history.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="wrap_content" 
android:layout_height="wrap_content" 
android:background="#000000" 

tools:context="com.mattbraddock.mbcgen.HistoryActivity"> 

<LinearLayout 
    android:id="@+id/buttons" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:onClick="clearOne" 
     android:text="Remove Last" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:onClick="sendMessage" 
     android:text="Return" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:onClick="clearHist" 
     android:text="Clear All" /> 

</LinearLayout> 

<ListView 
    android:id="@+id/history" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/buttons" 
    android:layout_alignParentTop="true" 
    android:paddingBottom="16dp" /> 

</RelativeLayout> 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mattbraddock.mbcgen"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" 
     android:screenOrientation="portrait" 
     android:theme="@style/Theme.AppCompat.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".HistoryActivity" 
     android:label="Card History" 
     android:theme="@style/Theme.AppCompat.Dialog" 
     android:screenOrientation="portrait"></activity> 
    <!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
     App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
</application> 

</manifest> 
+0

ArrayAdapterあなたは、配列を使う、というのはなぜArrayListの...を受け入れますか?また、notifyDatasetChangedを呼び出すときにアダプターをキャストする必要はありません。 –

+0

'ArrayAdapter mAdapter = new ArrayAdapter <>(this、android.R.layout.simple_list_item_1、historyArray)'配列アダプターを先に参照してください。 'mAdapter.notifyDataSetChanged()'を試してください –

+0

@ cricket_007 - 私がアダプタをキャストしないと、 'notifyDataSetChanged()'で "メソッドを解決できません"というメッセージが表示されます。 –

答えて

0

問題は、各時間はあなたが別の新しく作成された配列にhistoryArray変数の参照を変更

historyArray = history.getHistory().toArray(new String[history.getHistory().size()]); 

を使用することですが、アダプターは、まだあなたは、アダプタのコンストラクタに渡された古い配列への参照を保持します。

あなたの提案は、配列ArrayListで置き換え、clear()add()、およびaddAll()のメソッドを使用することです。アダプタのデータへの参照を保持し、notifyDataSetChanged()が機能するようにする場合は、historyArrayを再割り当てしないでください。

+0

Arrayを作成するのではなく、ArrayListを使用するだけで問題を解決できました。ありがとうございました。 –

0

私はアダプタへの参照を保存し、配列の代わりにArraylistを使用します。

historyView = (ListView) findViewById(R.id.history); 
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, history.getHistory()); 
historyView.setAdapter(adapter); 

次に、ビューを更新する場合は、アダプターを変更してください。

adapter.clear(); 

それとも

adapter.remove(0); 
関連する問題