如何在没有 XML 的情况下以编程方式将运行时创建的视图添加到 ListActivity 中

发布于 2024-10-17 04:50:04 字数 2458 浏览 5 评论 0原文

是否有可能在运行时完全创建一个 ListActivity 而不使用 XML?我正在尝试创建一个包含 8 个项目的列表视图,在四个 TextView 和四个 ToggleButton 之间交替。目标是拥有一个包含 TextViewToggleButtonTextViewToggleButtonTextView< 的垂直列表/strong>, ToggleButton, TextView, ToggleButton

这是我的代码:

public class MyActivity extends ListActivity implements OnCheckedChangeListener
{
    private ViewGroup.LayoutParams widthLayout = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
    private TextView[] textItems = new TextView[ 4 ];
    private ToggleButton[] toggleButtons = new ToggleButtons[ 4 ];
    private static final int ID_BASE = 5550;  // unique ID for this activity

    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setupView();
    }
//
    private void setupView() {
        Vector v_itemList = new Vector();
        for ( int i=0; i < 4; i++ ) {
            int t_color = 0xFFFFFF;
            //
            textItems[i] = new TextView( this );
            textItems[i].setText( ""+i+". text line" );
            textItems[i].setColor( t_color );
            t_color -= 0x220022;
            //
            toggleButtons[i] = new ToggleButton( this );
            toggleButtons[i].setTextOff( ""+i+". Off" );
            toggleButtons[i].setTextOn( "On ("+i+")" );
            toggleButtons[i].setId( ID_BASE+i );
            toggleButtons[i].setChecked( i%2 == 1 ? true : false );
            toggleButtons[i].setOnCheckedChangeListener( this );
            //
            v_itemList.addElement( textItems[i] );
            v_itemList.addElement( toggleButtons[i] );
        }
        ArrayAdapter listItemAdapter = new ArrayAdapter( this, android.R.layout.simple_list_item_1, v_itemList );
        this.setListAdapter( listItemAdapter );
    }
}

当我尝试此代码时,我得到了所有内容的列表列表视图元素的 toString() 输出: android.widget.TextView@4376f760 android.widget.ToggleButton@437708c8 android.widget.TextView@43772b10 android.widget.ToggleButton@43772fa0 ...等等...

然后我尝试使用以下方法添加 TextView 和 ToggleButton:

this.addContentView( textItems[i], widthLayout );
this.addContentView( toggleButtons[i], widthLayout );

在 for 循环中。但收到 RuntimeException,您的内容必须有一个 id 属性为“android.R.id.list”的 ListView

任何帮助将不胜感激,如果可能的话,我想避免使用任何对 XML 的引用。

Is it even possible to create a ListActivity completely during runtime without using XML? I’m trying to create a list view with 8 items, alternating between four TextViews and four ToggleButtons. The goal is to have a vertical list with TextView, ToggleButton, TextView, ToggleButton, TextView, ToggleButton, TextView, ToggleButton

Here’s my code:

public class MyActivity extends ListActivity implements OnCheckedChangeListener
{
    private ViewGroup.LayoutParams widthLayout = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
    private TextView[] textItems = new TextView[ 4 ];
    private ToggleButton[] toggleButtons = new ToggleButtons[ 4 ];
    private static final int ID_BASE = 5550;  // unique ID for this activity

    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setupView();
    }
//
    private void setupView() {
        Vector v_itemList = new Vector();
        for ( int i=0; i < 4; i++ ) {
            int t_color = 0xFFFFFF;
            //
            textItems[i] = new TextView( this );
            textItems[i].setText( ""+i+". text line" );
            textItems[i].setColor( t_color );
            t_color -= 0x220022;
            //
            toggleButtons[i] = new ToggleButton( this );
            toggleButtons[i].setTextOff( ""+i+". Off" );
            toggleButtons[i].setTextOn( "On ("+i+")" );
            toggleButtons[i].setId( ID_BASE+i );
            toggleButtons[i].setChecked( i%2 == 1 ? true : false );
            toggleButtons[i].setOnCheckedChangeListener( this );
            //
            v_itemList.addElement( textItems[i] );
            v_itemList.addElement( toggleButtons[i] );
        }
        ArrayAdapter listItemAdapter = new ArrayAdapter( this, android.R.layout.simple_list_item_1, v_itemList );
        this.setListAdapter( listItemAdapter );
    }
}

When I've tried this code, I get a list list view of all the element's toString() output:
android.widget.TextView@4376f760
android.widget.ToggleButton@437708c8
android.widget.TextView@43772b10
android.widget.ToggleButton@43772fa0
... ect ...

I then tried to add the TextView and ToggleButton using:

this.addContentView( textItems[i], widthLayout );
this.addContentView( toggleButtons[i], widthLayout );

within the for loop. But recieved a RuntimeException, Your content must have a ListView whos id attribute is 'android.R.id.list'

Any help would be appreciated, and if possible I'd like to avoid using any reference to XML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-10-24 04:50:04

这不是使用 ArrayAdapter 的方式。你给它的数组应该是要显示的数据,而不是显示数据的视图。通常,您只能从 ArrayAdapter 中获取 TextView。要获取 ToggleButtons,您需要继承 ArrayAdapter 并重写 getView()。

您看到的字符串是因为 ArrayAdapter 将您的视图数组视为要显示的数据。 ArrayAdapter 的标准行为是:

但是 TextView 被引用,它
将用 toString() 填充
数组中的每个对象

请参阅 ArrayAdapter列表视图教程了解更多信息。

我很好奇为什么你想在 ListView 中放置切换按钮。您所描述的内容听起来更适合 LinearLayout。

That's not how you use an ArrayAdapter. The array you give it should be the data to be displayed, not the views to display the data. Normally, you only get TextViews out of an ArrayAdapter. To get your ToggleButtons, you will need to subclass ArrayAdapter and override getView().

The strings you are seeing is because the ArrayAdapter is treating your array of views as the data to be displayed. The standard behavior for ArrayAdapter is:

However the TextView is referenced, it
will be filled with the toString() of
each object in the array

See the documentation for ArrayAdapter and the List View Tutorial for more info.

I'm curious why you want to put toggle buttons in a ListView. What you are describing sounds like it would be better suited to a LinearLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文