我正在创建一个简单的程序,它将文本和计数存储在表中。此数据显示在listView中。我无法获取数据以及何时更新。但是,当我关闭并打开应用程序时,数据加载正常。我已经加入了我的计划。
MainActivity:
dailyAt()
数据库处理器:
public class MainActivity extends AppCompatActivity {
private List<Habit> habits;
private ListAdapter adapter;
private DatabaseHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = ( Toolbar ) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
handler = new DatabaseHandler(getApplicationContext());
habits = handler.getAllHabit();
View empty = getLayoutInflater().inflate(R.layout.list_item_empty, null, false);
addContentView(empty, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ListView habitList = ( ListView ) findViewById(R.id.habitList);
adapter = new ListAdapter(habits);
if ( habitList != null ) {
habitList.setAdapter(adapter);
habitList.setEmptyView(empty);
habitList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Habit habit = ( Habit ) adapter.getItem(position);
habit.updateCount();
handler.updateHabit(habit);
updateList();
}
});
habitList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Habit habit = (Habit) adapter.getItem(position);
handler.deleteHabit(habit);
updateList();
return true;
}
});
}
}
private void getNewHabit() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.Dialog));
alertDialog.setTitle("New Habit");
alertDialog.setMessage("Enter the name of the new habit");
final EditText input = new EditText(getApplicationContext());
int padding = Math.round(getResources().getDimension(R.dimen.margin));
input.setPadding(padding,padding,padding,padding);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
input.setLayoutParams(params);
alertDialog.setView(input);
alertDialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Habit habit = new Habit();
habit.setHabit(input.getText().toString());
habit.setCount(0);
handler.addHabit(habit);
updateList();
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void updateList() {
habits.clear();
habits = handler.getAllHabit();
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:2)
我得到了它的工作。我不知道这是否是正确的方法,或者它是否只是一种解决方法。在updateList()
方法中,我更改了行
habits = handler.getAllHabit();
到
habits.addAll(handler.getAllHabit());
使用AlertDialog添加项目后,listView立即立即更新。
答案 1 :(得分:1)
只要数据库中有更新,就使用SimpleCursorAdapter
并在适配器对象上调用changeCursor(newCursor)
。
将cursorAdpater声明为实例变量:
SimpleCursorAdapter cursorAdapter;
然后在onCreate中,初始化cursorAdapter并在listview上设置它:
Cursor cursor = handler.getAllHabit();//Change the return type of your getAllHabit method to Cursor
String[] from = new String[]{"name","count"};//database column names
int[] to = new int[]{R.id.tvName,R.id.tvCount};//TextView id's from custom list row layout
cursorAdapter = new SimpleCursorAdapter(this,R.layout.list_row,cursor,from,to,CursorAdapter.FLAG_AUTO_REQUERY);
habitList.setAdapter(cursorAdapter);
每当数据库中有任何更新时,将更新的数据放入新游标并在cursorAdapter上调用changeCursor()
并传递新游标。
Cursor newCursor = handler.getAllHabit();
cursorAdapter.changeCursor(newCursor);
答案 2 :(得分:0)
使用finishAffinity()
这种方法
完成此活动,并尝试完成其下方的所有活动
在具有相同亲和力的当前任务中。
ActivityCompat.finishAffinity(this);