我有一个从数据库填充的列表视图。我想在下面的代码中扩展ActionBarActivity而不是ListActivity(但我仍然需要它),因为我有一些使用Action Bar的功能。
Java类:
public class ChooseHelp extends ListActivity {
ListView listHelp;
private final ArrayList<String> results = new ArrayList<>();
public final static String ID_HELP = "com.module.help._ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
listHelp = (ListView) findViewById(android.R.id.list);
listHelp.setOnItemClickListener(onListClick);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private final AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(ChooseHelp.this, HelpContent.class);
i.putExtra(ID_HELP, String.valueOf(id));
startActivity(i);
}
};
private void openAndQueryDatabase() {
try {
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
SQLiteDatabase newDB = dbHelper.getWritableDatabase();
String tableName = "tbl_Help";
Cursor c = newDB.rawQuery("SELECT help FROM " +
tableName +
" LIMIT 18", null);
if (c != null) {
if (c.moveToFirst()) {
do {
String HelpTopic = c.getString(c.getColumnIndex("help_topic"));
results.add(HelpTopic);
} while (c.moveToNext());
}
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.module.example.ChooseHelp">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/allTopics" />
</RelativeLayout>
我试图像this那样做,但它不适合我的设计
请帮帮我。非常感谢你。
答案 0 :(得分:1)
您可以使用listview创建一个布局:
<强> activity_help.xml 强>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:id="@+id/listview"
android:layout_height="match_parent"
android:layout_width="match_parent">
</LinearLayout>
然后进入你的ActionBarActivity:
public class ChooseHelp extends ActionBarActivity {
private ArrayList<String> results;
ListView listView;
ArrayAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
results = new ArrayList<>();
openAndQueryDatabase();
listView = (ListView)findViewById(R.id.listview);
listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(onListClick);
}