Listview未在Fragment中更新

时间:2016-11-10 06:37:13

标签: java android listview android-fragments android-lifecycle

我确信updateFromDatabase()函数有效,我使用print语句来查看放入mCoordinatesArray的条目是否存在,而不是空字符串。但是,当我重新启动应用程序时,片段永远不会使用数据库中的项填充列表视图。我认为这与片段生命周期有关,但我不知道。

此外,当我第一次没有重新启动应用程序并运行它时,列表视图运行正常。当我旋转或重新启动应用程序时,列表视图不再填充。

public class LocalFragment extends Fragment{

private ListView mLocalList;
private ArrayAdapter<String> adapter;
private ArrayList<String> mCoordinatesArray;
private BroadcastReceiver mBroadcastReceiver;
private LocationBaseHelper mDatabase;
private DateFormat dateFormat;
private String dateString;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_local,container,false);
    // SQLite Setup
    mDatabase = new LocationBaseHelper(getActivity());

    mLocalList = (ListView) v.findViewById(R.id.lv_local);
    mCoordinatesArray = new ArrayList<>();
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCoordinatesArray);
    if(!mDatabase.size().equals("0")){
        updateFromDatabase();
    }
    mLocalList.setAdapter(adapter);
    return v;
}

@Override
public void onResume() {
    super.onResume();

    if(mBroadcastReceiver == null){
        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                dateFormat = new SimpleDateFormat("MM/dd HH:mm:ss a");
                dateString = dateFormat.format(new Date());

                String[] data = intent.getStringExtra("coordinates").split(" ");
                mDatabase.insertEntry(dateString,data[0],data[1]);
                System.out.println(mDatabase.size());

                mCoordinatesArray.add(dateString + " " + data[0] + " " + data[1]);
                adapter.notifyDataSetChanged();
            }
        };
    }
    getActivity().registerReceiver(mBroadcastReceiver, new IntentFilter("location_update"));
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(mBroadcastReceiver!=null){
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }
}

// THIS METHOD CAN BE USED TO UPDATE THE ARRAY HOLDING COORDINATES FROM THE LOCAL DATABASE
private void updateFromDatabase(){
    //mCoordinatesArray.clear();
    mCoordinatesArray = mDatabase.getEntireDatabase();
    adapter.notifyDataSetChanged();
}

}

这是我的助手课程,以防万一,但我认为问题不在这里。

public class LocationBaseHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static final String D​A​T​A​B​A​S​E​_​N​A​M​E​ = "locationBase.db";

public LocationBaseHelper(Context context) {
    super(context, D​A​T​A​B​A​S​E​_​N​A​M​E​, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " +    LocationTable.NAME              + " ("      +
                                    LocationTable.Cols.DATE_TIME    + " text, " +
                                    LocationTable.Cols.LATITUDE     + " text, " +
                                    LocationTable.Cols.LONGITUDE    + " text )"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void insertEntry(String date_time, String latitude, String longitude){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues content = new ContentValues();
    content.put(LocationTable.Cols.DATE_TIME,date_time);
    content.put(LocationTable.Cols.LATITUDE,latitude);
    content.put(LocationTable.Cols.LONGITUDE,longitude);
    db.insert(LocationTable.NAME,null,content);
}

public ArrayList<String> getEntireDatabase(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + LocationTable.NAME,null);
    cursor.moveToFirst();

    ArrayList<String> values = new ArrayList<>();

    do{
        String value = (String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.DATE_TIME)) + " " +
                       (String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LATITUDE))  + " " +
                       (String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LONGITUDE));
        values.add(0,value);

    }while(cursor.moveToNext());

    return values;
}

public String size(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + LocationTable.NAME,null);
    cursor.moveToFirst();
    return cursor.getString(0);
}
}

3 个答案:

答案 0 :(得分:1)

尝试重新创建ArrayAdapter而不是使用.notifyDataSetChanged():

// update Data
mCoordinatesArray = mDatabase.getEntireDatabase();
// create new adapter with new updated array
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCoordinatesArray);
// set adapter for the listview
mLocalList.setAdapter(adapter);

答案 1 :(得分:1)

通过调用mCoordinatesArray = mDatabase.getEntireDatabase();,您正在更改mCoordinatesArray的引用,并且适配器仍然保留旧引用,因此它看不到任何更改。

您应该更新它包含的值,而不是创建mCoordinateArray的新实例,例如:

mCoordinateArray.clear();
mCoordinateArray.addAll(mDatabase.getData());
adapter.notifyDataSetChange();

这样您就可以更改适配器引用的数据,而不是创建适配器不知道的全新数据集。

答案 2 :(得分:0)

您可以将此方法放入片段中,并从附加到片段的活动中调用。

public void updateList(List<?> result) {

    if (multimediaListRent.size()>0) {
        multimediaGridView.setAdapter(gridMediaListAdapter);
        multimediaListRent.clear();
    }

    gridMediaListAdapter.notifyDataSetChanged();
}