使用ArrayAdapter实时更新ListView

时间:2016-04-02 12:07:55

标签: java android listview arraylist

Android初学者。

我有一个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>

2 个答案:

答案 0 :(得分:0)

问题在于每次使用

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

您将historyArray变量的引用更改为另一个新创建的数组,但适配器仍保留对传递给适配器构造函数的旧数组的引用。

我的建议是使用ArrayList替换您的数组并使用clear()add()addAll()方法。如果您希望保留对适配器数据的引用,并且historyArray能够正常工作,请避免重新分配notifyDataSetChanged()

答案 1 :(得分: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);