Android 中嵌入 ListView?

发布于 2024-12-09 13:48:26 字数 65 浏览 0 评论 0原文

我想在我的程序中使用 listView。但是,我还想在同一窗口中使用一些不同的组件(按钮、文本编辑等)。我该怎么做?

I want to use a listView in my program. However, I also want to use some different components(Button, TextEdit etc.) in the same window. How can i do it?

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

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

发布评论

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

评论(2

我的影子我的梦 2024-12-16 13:48:26

为此,您必须使用基本适配器。这很简单。这里面有四个方法
1.获取计数
2.获取项目
3.获取物品ID
4.getView

类CustomListAdapter扩展BaseAdapter
{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = view;

        if(rowView == null)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(QuickSearchingWithAndroid.this);
            rowView = layoutInflater.inflate(R.layout.custom_list_view,parent,false);
        }

        WebView webView1 = (WebView)rowView.findViewById(R.id.webview1);

        webView1.loadUrl("http://www.google.co.in");


        return rowView;
    }

}

然后我们必须创建该类的对象并将该对象设置为:
CustomListAdapter customListAdapter = new CustomListAdapter();
listView.setAdapter(customListAdapter);

上面两行您必须在 onStart 或 onCreate 方法中执行。
我希望这能解决您的问题...只需检查如何创建布局文件,因为必须创建两个布局文件。

You have to use Base Adapter for that. It is simple. There are four methods Inside this
1.getCount
2.getItem
3.getItemId
4.getView

class CustomListAdapter extends BaseAdapter
{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = view;

        if(rowView == null)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(QuickSearchingWithAndroid.this);
            rowView = layoutInflater.inflate(R.layout.custom_list_view,parent,false);
        }

        WebView webView1 = (WebView)rowView.findViewById(R.id.webview1);

        webView1.loadUrl("http://www.google.co.in");


        return rowView;
    }

}

Then we have to create the object of the class and set That Object as:
CustomListAdapter customListAdapter = new CustomListAdapter();
listView.setAdapter(customListAdapter);

Above two lines you have to do inside the onStart or onCreate method.
I hope this will solve your problem...just check how the layout file has to create because their is two layout file has to create.

掐死时间 2024-12-16 13:48:26

无需扩展 ListActivity,只需使用常规活动并实例化您的列表视图,如下所示:

listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
    listView.setTextFilterEnabled( true );
    listView.setOnItemClickListener( new OnItemClickListener()
    {
        public void onItemClick( AdapterView<?> parent, View view,
            int position, long id ) {
            try
            {
                Intent i = new Intent().setClass( getParent(), GuideActivity.class );
                i.putExtra( "action", "nextstep" );
                i.putExtra( "session", currentStep.getSession() );
                i.putExtra( "step", currentStep.getStep() );
                i.putExtra( "response", currentStep.getResponse( position ).getId() );
                TabGroupActivity parentActivity = ( TabGroupActivity ) getParent();
                parentActivity.startChildActivity( currentStep.getStep(), i );
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    });

您当然必须进行一些自定义,因为这是直接从我自己的应用程序获取的。

我为此应用程序制作了自己的适配器。您可能应该改用 ArrayAdapter。

Instead of extending ListActivity, just use a regular activity and instantiate your list view like this:

listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
    listView.setTextFilterEnabled( true );
    listView.setOnItemClickListener( new OnItemClickListener()
    {
        public void onItemClick( AdapterView<?> parent, View view,
            int position, long id ) {
            try
            {
                Intent i = new Intent().setClass( getParent(), GuideActivity.class );
                i.putExtra( "action", "nextstep" );
                i.putExtra( "session", currentStep.getSession() );
                i.putExtra( "step", currentStep.getStep() );
                i.putExtra( "response", currentStep.getResponse( position ).getId() );
                TabGroupActivity parentActivity = ( TabGroupActivity ) getParent();
                parentActivity.startChildActivity( currentStep.getStep(), i );
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    });

You will of course have to do some customization since this is taken directly from my own application.

I made my own adapter for this application. You should probably use an ArrayAdapter instead.

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