我的 Android 应用程序中有一个选项卡活动(A.java)。当我从活动 A 转到另一个活动(比如 B)并返回 A 时,我在选项卡中找不到任何内容
我在选项卡活动中有两个选项卡(比如 A)。
选项卡 1 显示了一些内容(此处无关紧要)。
选项卡 2 显示使用 SimpleCursorAdapter(工作完美)连接到 SQLite 数据库的 ListView。
现在我单击选项卡 2 的列表中的一个项目。这会将您带到另一个活动(例如 B)。
现在当我从活动 B 返回到活动 A(选项卡式活动)时。
我在那里没有看到 ListView。选项卡 2 为空。
该怎么办? 请检查代码的光标适配器部分,因为我收到了
“错误/光标(8736):fillWindow() 中的语句无效” IN logcat。
这是 TAB 2 的活动(java 文件)。
public class ViewAll extends Activity implements OnItemClickListener {
TextView selection ;
Cursor cursor;
ListView lv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bday_list);
DatabaseHelp dbHelp = new DatabaseHelp(getApplicationContext());
Log.d("holla", "after database open!");
lv1= (ListView)findViewById(R.id.List_of_bday);
dbHelp.open();
cursor =dbHelp.fetchAllContacts();
Log.d("DOR DOR",cursor.getCount()+"");
startManagingCursor(cursor);
// the desired columns to be bound
String[] columns = new String[] {DatabaseHelp.KEY_NAME, DatabaseHelp.KEY_DATE };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tv_name, R.id.tv_date };
// create the adapter using the cursor pointing to the desired data as well as the layout information
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_each_row, cursor, columns, to);
// set this adapter as your ListActivity's adapter
lv1.setAdapter(mAdapter);
dbHelp.close();
lv1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, "clicked "+arg2, Toast.LENGTH_SHORT).show();
Intent i=new Intent(this,ViewOne.class);
startActivity(i);
}}
这是 TABBEd 活动的代码:-
public class Today extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res=getResources();
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TodayFinal.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("today").setIndicator("View Today's Birthday",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ViewAll.class);
//THIS TAKES YOU TO THE ABOVE MENTIONED ACTIVITY(TAB 2)
spec = tabHost.newTabSpec("all").setIndicator("View All",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
}
I have TWO tabs in Tab Activity(say A).
Tab 1 shows something(immaterial here).
Tab 2 shows a ListView connected to the SQLite Database with SimpleCursorAdapter(WORKS PERFECT).
Now i click on an item in LIST of Tab 2. This takes you to another activity(say B).
now when i go back from activity B to activity A(tabbed activity).
I dont see the ListView there. The Tab 2 is empty.
What to do?
Plz check the cursor adapter part of the code also because i receive a
"ERROR/Cursor(8736): Invalid statement in fillWindow()" IN logcat.
This is activity(java file) of TAB 2.
public class ViewAll extends Activity implements OnItemClickListener {
TextView selection ;
Cursor cursor;
ListView lv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bday_list);
DatabaseHelp dbHelp = new DatabaseHelp(getApplicationContext());
Log.d("holla", "after database open!");
lv1= (ListView)findViewById(R.id.List_of_bday);
dbHelp.open();
cursor =dbHelp.fetchAllContacts();
Log.d("DOR DOR",cursor.getCount()+"");
startManagingCursor(cursor);
// the desired columns to be bound
String[] columns = new String[] {DatabaseHelp.KEY_NAME, DatabaseHelp.KEY_DATE };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tv_name, R.id.tv_date };
// create the adapter using the cursor pointing to the desired data as well as the layout information
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_each_row, cursor, columns, to);
// set this adapter as your ListActivity's adapter
lv1.setAdapter(mAdapter);
dbHelp.close();
lv1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, "clicked "+arg2, Toast.LENGTH_SHORT).show();
Intent i=new Intent(this,ViewOne.class);
startActivity(i);
}}
This is the code of the TABBEd ACTIVITY:-
public class Today extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res=getResources();
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TodayFinal.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("today").setIndicator("View Today's Birthday",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ViewAll.class);
//THIS TAKES YOU TO THE ABOVE MENTIONED ACTIVITY(TAB 2)
spec = tabHost.newTabSpec("all").setIndicator("View All",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置适配器后,您可以调用
dbHelp.close();
。但是应该有打开的数据库连接才能使游标适配器正常工作,因为它在每个getView
调用上都使用游标。将关闭数据库调用移至onDestroy
。或者打开它并在onStart
中设置适配器,然后在onStop
中关闭。You call
dbHelp.close();
after setting the adapter. But there should be open db connection for cursor adapter to work properly since it use cursor on everygetView
call. Move your closing db call toonDestroy
. Or open it and set adapter inonStart
and close then inonStop
alternatively.