I very new to the Android SDK but I've managed to create a Dashboard layout and now I'm in the process of building out the activity classes / layouts behind each dashboard button. I have the click event firing & launching the associated button activity, etc (1 act per button). I also have a ti开发者_JS百科tlebar on each activity layout that allows navigation back to the main dashboard, settings, etc. Basic look & feel. All has been working great up until this point. I'm now attempting to use a ListView on the activity layouts to display data from a local db (sqlite).
In my onCreate() method, I use a SimpleCursorAdapter to attempt to bind a cursor (SQL result) to the ListView in my layout. I'm actually using 2 layouts here based on a tutorial I did earlier (Notepad). The 2nd layout is for each "row" present in the cursor. The problem is that once I set the layout in the SimpleCursorAdapter to this 2nd layout, I lose the titlebar functionality in my first layout. And obviously if I try to replicate the titlebar code in the 2nd layout, it will be repeated with each instance in the cursor.
Also - all of my "sub" activity classes are inheriting from a base class (FunDashboardActivity) that inherits from Activity. I cannot use the ListActivity as a base class here because of my design. Again - I'm completely new to Android and would greatly appreciate any feedback / help. THANK YOU in advance.
This is my activity code:
public class FunSitesActivity extends FunDashboardActivity {
private int mSiteNumber = 1;
private FunDbAdapter mDbHelper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_sites);
mDbHelper = new FunDbAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData() {
// Get Site Data Stored
Cursor c = mDbHelper.fetchAllSites();
startManagingCursor(c);
// Create Cursor Adapter
String[] from = new String[] { FunDbAdapter.KEY_SITE };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.site_row, c, from, to);
// Create ListView & Set Cursor
ListView lv = new ListView(this);
lv.setAdapter(notes);
setContentView(lv);
}
// act_site xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout style="@style/TitleBar">
<ImageButton style="@style/TitleBarAction"
android:src="@drawable/title_home"
android:contentDescription="@string/description_about"
android:onClick="onClickHome" />
<ImageView style="@style/TitleBarSeparator" />
<TextView style="@style/TitleBarText" />
<ImageButton style="@style/TitleBarAction"
android:contentDescription="@string/description_config"
android:src="@drawable/title_config"
android:onClick="onClickConfig" />
<ImageButton style="@style/TitleBarAction"
android:contentDescription="@string/description_about"
android:src="@drawable/title_about"
android:onClick="onClickAbout" />
</LinearLayout>
<ListView
android:id="@+id/sitelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_sites"/>
</LinearLayout>
// site_row xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I think I figured it out. The problem was in my fillData method, where I was setting the contentView to the new ListView instance. See new code below
private void fillData() {
// Get Site Data Stored
Cursor c = mDbHelper.fetchAllSites();
startManagingCursor(c);
// Create Cursor Adapter
String[] from = new String[] { JAMDbAdapter.KEY_SITE };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.site_row, c, from, to);
// Create ListView & Set Cursor
// NEW APPROACH
ListView lv = (ListView) findViewById(R.id.sitelist);
lv.setAdapter(notes);
// OLD APPROACH
//ListView lv = new ListView(this);
//lv.setAdapter(notes);
//setContentView(lv);
}
I'm not sure if this is the "best" approach for what I'm trying to accomplish so any / all feedback would be GREATLY appreciated.
精彩评论