내 코드에서 지금 당장 목록보기를 제대로 새로 고치려면 데이터베이스 정보를 다시 가져와 SimpleCursorAdapter
을 다시 만들어야합니다.SimpleCursorAdapter에서 notifyDataSetChanged 사용이 작동하지 않습니다.
예를 들어 목록보기 내에 버튼이 있습니다. 이 버튼을 클릭하면 목록보기 항목에 대한 데이터베이스 항목이 제거됩니다. 따라서 어댑터를 다시 만들지 않고 목록보기에서 항목을 제거하면됩니다. 나는 (이 SimpleCursorAdapater
을 확장하고 notifyDataSetChanged()
기능을 사용할 수 할 수 있기 때문에) SimpleCursorAdapter
에서 BaseAdapater
내 세계를 변화 시도했습니다,하지만 여전히 작동하지 않습니다
. 여기
내가 지금 사용하고 코드입니다 (작업 않는) : 목록보기 항목 내부의 삭제 버튼private RoutinesDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME };
private int[] to;
@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routines);
toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
datasource = new RoutinesDataSource(this);
datasource.open();
Cursor cursor = datasource.fetchAllRoutines();
to = new int[] { R.id.listitem_routine_name };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}
코드 :
global
선언과
onCreate()
에 대한
코드
public void onClick(View view) {
ListView l = getListView();
int position = l.getPositionForView(view);
Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
cursor.moveToPosition(position);
long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
switch (view.getId()) {
case R.id.button_routine_delete:
toast_deleted.setText(getString(R.string.toast_routine_deleted));
toast_deleted.show();
datasource.deleteRoutine(id);
onResume();
break;
}
}
onResume()
을 사용하여 메모 해주십시오.
저는 datasource.deleteRoutine(id)
이 작동을 닫고 다시 열 때 목록 항목이 사라 졌음을 알고 있습니다.
제거 목록보기 항목에 제대로 목록을 보여줍니다 onResume에 대한 코드() :
@Override
protected void onResume() {
datasource.open();
Cursor cursor = datasource.fetchAllRoutines();
if (isEditing) {
to = new int[] { R.id.listitem_routine_edit_name };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine_edit, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}
else {
to = new int[] { R.id.listitem_routine_name };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}
super.onResume();
}
난 그냥 그 나쁜 관행 어댑터를 제거되었습니다 단순히 목록 항목을 제거 할 때마다 다시 생각한다 데이터베이스에서. 내가 말한 것처럼 BaseAdapater를 사용하여 notifyDataSetChanged를 시도했지만 간단히 작동하지 않습니다.
또한 isEditing
부울을 기록하십시오. 삭제 버튼이 표시된 작업 표시 줄에서 편집 버튼을 클릭하면 true로 설정됩니다. 이는 내가 클릭 할 때 활동을 시작하는 편집 버튼을 가지고 있기 때문에 유용합니다. 편집을 마친 후에 다시 돌아올 때 사용자를위한 버튼을 보여줍니다.
어쨌든 누군가가 어댑터를 다시 만들지 않고도 목록을 새로 고치는 방법을 알려줄 수 있습니까? 아니면 제가 가장 좋은 방법을 수행 했나요?
나는 비슷한 검색어로 여기에 답변했습니다. - http://stackoverflow.com/questions/13953171/update-the-listview-after-inserting-a-new-record-with-simplecursoradapter-requ/13953470#13953470 – mango
감사합니다. . 아프다. – scarhand
완벽하게 작동 망고 감사합니다! – scarhand