我是android编码的新手,需要我的项目帮助:
我有一个函数,可以从mainActivity的OnResume调用。该函数从数据库中重新加载数据,并将其显示在列表视图中。 现在,当我转到第二个Activity并输入新数据时,将在调用secondActivity的onPause时保存数据。返回mainActivity后,我看不到对列表视图所做的更改,但是当我单击列表视图的同一项并在第二次返回时再次转到第二个Activity时,更改开始显示,并且代码可以正常工作。第一次尝试功能不起作用的可能原因是什么?
注意:我还从主要Activity的onCreate调用了函数
编辑1:
调用函数
public void fill_list() {
cursor = db.rawQuery("SELECT * FROM " + GameEntry.TABLE + " WHERE " + GameEntry.G_DIFF + " = " + diff, null);
yesitis = cursor.moveToFirst();
if (yesitis)
count = cursor.getCount();
//int count=cursor.getCount();
listv = (ListView) findViewById(R.id.listview1);
listv.setAdapter(null);
arr.clear();
for (int j = 0; j < 15; j++) {
String data;
int j2 = j + 1;
data = String.valueOf(j2);
cursor3 = db.rawQuery("SELECT * FROM " + GameEntry.TABLE + " WHERE " + GameEntry.G_DIFF + " = " + diff+" AND "+GameEntry.G_LEVEL+" = "+j, null);
if (cursor3.moveToFirst()) {
cursor3.moveToPosition(0);
int showstate = cursor3.getColumnIndex(AppContract.GameEntry.G_STATE);
String temp1 = cursor3.getString(showstate);
if (temp1.equals("notStarted")) {
} else {
int showtime = cursor3.getColumnIndex(AppContract.GameEntry.time);
String temp2 = cursor3.getString(showtime);
data = data + " " + temp1 + " " + temp2;
}
}
arr.add(data);
}
ArrayAdapter<String> arradapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
switch (diff) {
case 1:
textView.setBackgroundColor(Color.parseColor("#83EEA4"));
break;
case 2:
textView.setBackgroundColor(Color.parseColor("#00C5F6"));
break;
case 3:
textView.setBackgroundColor(Color.parseColor("#FF416A"));
break;
}
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
return textView;
}
};
listv.setAdapter(arradapt);
listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View view, int ii, long id) {
level_select=ii;
cursor2 = db.rawQuery("SELECT * FROM " + GameEntry.TABLE + " WHERE " + GameEntry.G_DIFF + " = " + diff + " AND " + GameEntry.G_LEVEL + " = " + ii, null);
if (cursor2.moveToFirst()) {
cursor2.moveToPosition(0);
g_exist = "yes";
} else {
g_exist = "no";
}
Intent send = new Intent(ListActivity.this, GameActivity.class);
send.putExtra("level", ii);
send.putExtra("difficulty", diff);
send.putExtra("exist", g_exist);
startActivity(send);
}
});
}
onResume:
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "onResumed called", Toast.LENGTH_LONG).show();
fill_list();
}
mainActivity onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
MobileAds.initialize(this,
"ca-app-pub-6165188418561315~3404004371");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
diff = getIntent().getExtras().getInt("difficulty");
mhelper = new appdbhelper(this);
db = mhelper.getReadableDatabase();
fill_list();
Toast.makeText(getApplicationContext(), "sqlsize =" + String.valueOf(yesitis), Toast.LENGTH_SHORT).show();
}
第二次活动暂停(enterdata函数更新数据库的指定行):
@Override
protected void onPause() {
super.onPause();
//Toast.makeText(getApplicationContext(), "onPause called", Toast.LENGTH_LONG).show();
timer.stop();
enterdata();
}
预先感谢