如何使用R,其中ListActivity使用时选择一个ListView [英] How to use R to select a ListView when using it with ListActivity

查看:221
本文介绍了如何使用R,其中ListActivity使用时选择一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

public class MyActivity extends ListActivity implements OnClickListener {
    private ArrayList<String> listItems = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setAdapter();
        this.bindButtons();
    }
    private void setAdapter() {
        setContentView(R.layout.siteactivity);
        adapter=new ArrayAdapter<String>(this,
                R.layout.siteitem,
                listItems);
        setListAdapter(adapter);
    }
    private void bindButtons() {
        findViewById(R.id.buttonPrevious).setOnClickListener(this);
        findViewById(R.id.buttonNext).setOnClickListener(this);
    }
    // ...
}

用这个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:id="@+id/siteActivity">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navigation">
    <Button
        android:text="&lt;="
        android:textSize="12dp"
        android:id="@+id/buttonPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:text="=&gt;"
        android:textSize="12dp"
        android:id="@+id/buttonNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
</LinearLayout>
<ListView
     android:id="@android:id/list"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/navigation"
     android:layout_above="@+id/transport_selection"
     />
</RelativeLayout>

请注意在的android:ListView中的:ID =ID /列表@机器人。如果我通过替换机器人:ID =@ + ID /列表,我的活动强制关闭,因为我的的内容必须有一个ListView的id属性是'机器人。 R.id.list',但这是预期的行为,我想。

Note the android:id="@android:id/list" of the ListView. If I replace it by android:id="@+id/list", my activity force closes, because my "content must have a ListView whose id attribute is 'android.R.id.list'", but this is the expected behaviour, I think.

现在,我想一个上下文菜单添加到ListView的项目。我trye​​d registerForContextMenu(findViewById(R.id.list)); ,但它不工作,因为的Andr​​oid,:ID

Now, I want to add a context menu to the items of the ListView. I tryed registerForContextMenu(findViewById(R.id.list));, but it doesn't work, because of the android:id.

那么,如何添加上下文菜单?

Then, how can I add a context menu?

问候,

推荐答案

当你正在使用ListActivity,您可以使用下面的code获得列表视图中的code:

As you are using ListActivity, you can use the following code to get list view in code:

ListView myListView=getListView ();

这篇关于如何使用R,其中ListActivity使用时选择一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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