我如何能实现不ListActivity一个ListView? (仅使用活动) [英] How can I implement a ListView without ListActivity? (use only Activity)
本文介绍了我如何能实现不ListActivity一个ListView? (仅使用活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android,我真的需要做这种方式(我认为这样做的另一个活动
),但谁能告诉我一个简单的code(在的onCreate()
法),可以做列表视图
没有 ListActivity
?
I'm new to Android, and I really need to do it this way (I've considered doing it in another Activity
), but can anyone show me a simple code (just the onCreate()
method) that can do Listview
without ListActivity
?
感谢
推荐答案
如果您有包含这样一个ListView活性的XML布局
If you have an xml layout for the activity including a listView like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="fill_parent"
然后在您的onCreate你可以有这样的事情
Then in your onCreate you could have something like this
setContentView(R.layout.the_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myList);
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v,int position, long id)
{
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
}
});
这篇关于我如何能实现不ListActivity一个ListView? (仅使用活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文