How to use R to select a ListView when using it with ListActivity

a 夏天 提交于 2019-12-24 20:26:47

问题


I have this 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);
    }
    // ...
}

with this layout:

<?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>

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.

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?

Regards,


回答1:


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

ListView myListView=getListView ();



回答2:


Use android.R.id.list instead of R.id.list.

(And by the way, the error message you wrote here turns out to have the answer)

Basically, anything defined in XML as @android:whatever is the same as android.R.whatever in the Java code. Anything that you defined in XML, which will look like @whatever, is the same as R.whatever in Java code.

Final edit: If you're using ListActivity, you are required to have a ListView with the id @android:id/list. Thus, unless you want to add another ListView to your activity, you shouldn't need @+id/list (or R.id.list).




回答3:


You need to create an AlertDialog and call the dialog to show inside the listview's onItemLongClick() event. Click here for an example.



来源:https://stackoverflow.com/questions/6770267/how-to-use-r-to-select-a-listview-when-using-it-with-listactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!