如何在没有 ListActivity 的情况下实现 ListView?(仅使用活动) [英] How can I implement a ListView without ListActivity? (use only Activity)

查看:29
本文介绍了如何在没有 ListActivity 的情况下实现 ListView?(仅使用活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,我真的需要这样做(我已经考虑在另一个 Activity 中这样做),但是谁能给我一个简单的代码(只是 onCreate() 方法)可以在没有 ListActivity 的情况下执行 Listview 吗?

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?

谢谢

推荐答案

如果你有一个活动的 xml 布局,包括像这样的 listView

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆