ここから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>
ArrayAdapterあなたは、配列を使う、というのはなぜArrayListの...を受け入れますか?また、notifyDatasetChangedを呼び出すときにアダプターをキャストする必要はありません。 –
'ArrayAdapter mAdapter = new ArrayAdapter <>(this、android.R.layout.simple_list_item_1、historyArray)'配列アダプターを先に参照してください。 'mAdapter.notifyDataSetChanged()'を試してください –
@ cricket_007 - 私がアダプタをキャストしないと、 'notifyDataSetChanged()'で "メソッドを解決できません"というメッセージが表示されます。 –