我正在创建一个Android应用程序,我有一个活动,显示从数据库中获取的项目列表(listView)。我决定更改应用程序,以便listView显示在主活动的导航抽屉中而不是单独的活动中,我不知道如何执行此操作。任何帮助将不胜感激。提前谢谢。
以下是显示listView的活动:
public class AddressBook extends ListActivity {
public static final String ROW_ID = "row_id";
private ListView contactListView;
private CursorAdapter contactAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contactListView = getListView();
contactListView.setOnItemClickListener(viewContactListener);
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.contactTextView };
contactAdapter = new SimpleCursorAdapter(
AddressBook.this, R.layout.contact_list_item, null, from, to);
setListAdapter(contactAdapter);
}
@Override
protected void onResume()
{
super.onResume();
new GetContactsTask().execute((Object[]) null);
}
@Override
protected void onStop()
{
Cursor cursor = contactAdapter.getCursor();
if (cursor != null)
cursor.deactivate();
contactAdapter.changeCursor(null);
super.onStop();
}
private class GetContactsTask extends AsyncTask<Object, Object, Cursor>
{
DatabaseConnector databaseConnector =
new DatabaseConnector(AddressBook.this);
@Override
protected Cursor doInBackground(Object... params)
{
databaseConnector.open();
return databaseConnector.getAllContacts();
}
@Override
protected void onPostExecute(Cursor result)
{
contactAdapter.changeCursor(result);
databaseConnector.close();
}
}
OnItemClickListener viewContactListener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Intent viewContact =
new Intent(AddressBook.this, ViewContact.class);
viewContact.putExtra(ROW_ID, arg3);
startActivity(viewContact);
}
};
}
这是xml布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contactTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="8dp"
android:textColor="@android:color/white"
android:textSize="20sp"></TextView>