开发者

System IDs in android

开发者 https://www.devze.com 2023-03-21 06:50 出处:网络
I\'ve been following the Notepad tutorial, and I\'ve noticed that I have to use R.id.empty and 开发者_Go百科R.id.list in my XML file in order for the program to work.

I've been following the Notepad tutorial, and I've noticed that I have to use R.id.empty and 开发者_Go百科R.id.list in my XML file in order for the program to work.

What do these IDs mean?

Do I have to give all my lists the R.id.list ID?

I tried looking for them in the reference page, but they have no description.


I'm not familiar with R.id.empty, but using android.R.id.list is necessary when you're using a ListActivity with a custom layout.

See android.app.ListActivity:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)


Refer to ListActivity.java

@Override
public void onContentChanged() {
    super.onContentChanged();
    View emptyView = findViewById(com.android.internal.R.id.empty);
    mList = (ListView)findViewById(com.android.internal.R.id.list);
    if (mList == null) {
        throw new RuntimeException(
                "Your content must have a ListView whose id attribute is " +
                "'android.R.id.list'");
    }
    if (emptyView != null) {
        mList.setEmptyView(emptyView);
    }
    mList.setOnItemClickListener(mOnClickListener);
    if (mFinishedStart) {
        setListAdapter(mAdapter);
    }
    mHandler.post(mRequestFocus);
    mFinishedStart = true;
}

That is why you should define "android:id="@android:id/list" in your xml to let ListActivity.java find the needed layout when executing mList = (ListView)findViewById(com.android.internal.R.id.list);.

0

精彩评论

暂无评论...
验证码 换一张
取 消