gpt4 book ai didi

java - 比 ListActivity 扩展 ActionBarActivity

转载 作者:太空狗 更新时间:2023-10-29 14:08:50 24 4
gpt4 key购买 nike

我有一个从数据库中填充的 ListView 。我想在下面的代码中扩展 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 , 但它不符合我的设计

请帮帮我。非常感谢。

最佳答案

您可以像这样使用 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);
}

关于java - 比 ListActivity 扩展 ActionBarActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com