在ListView中加载多个布局

发布于 2024-12-11 16:33:34 字数 3075 浏览 0 评论 0原文

我正在尝试在列表视图中显示多个布局。我在列表视图中只有三个项目,每行都有不同的布局。这是我正在使用的代码

private class CompetitionDetailsAdapter extends ArrayAdapter<Article> {


public CompetitionDetailsAdapter(Context context, int textViewResourceId
                ) {
            super(context, 0);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);
            LayoutInflater inflater=getLayoutInflater();
            int type = getItemViewType(position);
            switch(type)
            {
                case 0:
                {
                    View headerCell = inflater.inflate(R.layout.rsscellheader, null);
                    ImageView imageView = (ImageView)headerCell.findViewById(R.id.headerCellImage);
                    TextView title = (TextView)headerCell.findViewById(R.id.txt_HeaderCellTitle);
                    TextView date = (TextView)headerCell.findViewById(R.id.txt_HeaderCellDate);
                    Activity activity = (Activity) getContext();
                    imageLoader.DisplayImage(appDeleg.getSelectedCompetition().getImage(), activity, imageView);
                    return headerCell;
                    //break;
                }
                case 1:
                {
                    View webViewCell = inflater.inflate(R.layout.rsswebviewcell, null);
                    WebView webView = (WebView)webViewCell.findViewById(R.id.webView);
                    webView.loadData(appDeleg.getSelectedCompetition().getDescription(), "text/html", "utf-8");
                    return webViewCell;
                }
                case 2:
                {
                    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
                    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);
                    return btnWebView;
                }
            }
            return convertView;
        }
    }

,但是在运行时崩溃并给出错误

10-25 14:00:14.298: ERROR/AndroidRuntime(3102): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.measureScrapChild(ListView.java:1117)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1200)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.onMeasure(ListView.java:1109)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.view.View.measure(View.java:8172)

有人可以帮忙吗? 我怎样才能在安卓中做到这一点? 谢谢

I am trying to display multiple layout in my listview. I am having only three items in list view and each row is having a different layout. Here is the code I am using for that

private class CompetitionDetailsAdapter extends ArrayAdapter<Article> {


public CompetitionDetailsAdapter(Context context, int textViewResourceId
                ) {
            super(context, 0);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);
            LayoutInflater inflater=getLayoutInflater();
            int type = getItemViewType(position);
            switch(type)
            {
                case 0:
                {
                    View headerCell = inflater.inflate(R.layout.rsscellheader, null);
                    ImageView imageView = (ImageView)headerCell.findViewById(R.id.headerCellImage);
                    TextView title = (TextView)headerCell.findViewById(R.id.txt_HeaderCellTitle);
                    TextView date = (TextView)headerCell.findViewById(R.id.txt_HeaderCellDate);
                    Activity activity = (Activity) getContext();
                    imageLoader.DisplayImage(appDeleg.getSelectedCompetition().getImage(), activity, imageView);
                    return headerCell;
                    //break;
                }
                case 1:
                {
                    View webViewCell = inflater.inflate(R.layout.rsswebviewcell, null);
                    WebView webView = (WebView)webViewCell.findViewById(R.id.webView);
                    webView.loadData(appDeleg.getSelectedCompetition().getDescription(), "text/html", "utf-8");
                    return webViewCell;
                }
                case 2:
                {
                    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
                    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);
                    return btnWebView;
                }
            }
            return convertView;
        }
    }

But while running its crashing and giving error

10-25 14:00:14.298: ERROR/AndroidRuntime(3102): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.measureScrapChild(ListView.java:1117)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1200)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.widget.ListView.onMeasure(ListView.java:1109)
10-25 14:00:14.298: ERROR/AndroidRuntime(3102):     at android.view.View.measure(View.java:8172)

Could someone help please?
How can I do this in android?
Thanks

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

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

发布评论

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

评论(2

挽你眉间 2024-12-18 16:33:34

我发现了这个问题。情况 2 中的返回类型出现错误。我返回的是 ImageButton,而不是视图
这是正确的代码

case 2:
{     
    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);

    return buttonCell;
}

I found the issue. Its an error with the return type in case 2. Instead of view I was returning a ImageButton
Here is the correct code

case 2:
{     
    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);

    return buttonCell;
}
茶色山野 2024-12-18 16:33:34

我认为你的错误是你试图在列表中放入超过 3 个项目,并且当你的列表大于该列表时,它会向你显示一个错误......尝试这样做:

private class CompetitionDetailsAdapter extends ArrayAdapter<Article> {
int i =0;

public CompetitionDetailsAdapter(Context context, int textViewResourceId
                ) {
            super(context, 0);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);
            LayoutInflater inflater=getLayoutInflater();

            switch(i)
            {
                case 0:
                {
                    View headerCell = inflater.inflate(R.layout.rsscellheader, null);
                    ImageView imageView = (ImageView)headerCell.findViewById(R.id.headerCellImage);
                    TextView title = (TextView)headerCell.findViewById(R.id.txt_HeaderCellTitle);
                    TextView date = (TextView)headerCell.findViewById(R.id.txt_HeaderCellDate);
                    Activity activity = (Activity) getContext();
                    imageLoader.DisplayImage(appDeleg.getSelectedCompetition().getImage(), activity, imageView);
                    return headerCell;
                    i++;
                    //break;
                }
                case 1:
                {
                    View webViewCell = inflater.inflate(R.layout.rsswebviewcell, null);
                    WebView webView = (WebView)webViewCell.findViewById(R.id.webView);
                    webView.loadData(appDeleg.getSelectedCompetition().getDescription(), "text/html", "utf-8");
                    i++;
                    return webViewCell;
                }
                case 2:
                {
                    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
                    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);
                    i = 0;
                    return btnWebView;
                }
            }
            return convertView;
        }
    }

I think that your mistake is that you are trying to put more than a 3 item in a list, and when your list is bigger than that it's showing you a mistake... try with this:

private class CompetitionDetailsAdapter extends ArrayAdapter<Article> {
int i =0;

public CompetitionDetailsAdapter(Context context, int textViewResourceId
                ) {
            super(context, 0);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);
            LayoutInflater inflater=getLayoutInflater();

            switch(i)
            {
                case 0:
                {
                    View headerCell = inflater.inflate(R.layout.rsscellheader, null);
                    ImageView imageView = (ImageView)headerCell.findViewById(R.id.headerCellImage);
                    TextView title = (TextView)headerCell.findViewById(R.id.txt_HeaderCellTitle);
                    TextView date = (TextView)headerCell.findViewById(R.id.txt_HeaderCellDate);
                    Activity activity = (Activity) getContext();
                    imageLoader.DisplayImage(appDeleg.getSelectedCompetition().getImage(), activity, imageView);
                    return headerCell;
                    i++;
                    //break;
                }
                case 1:
                {
                    View webViewCell = inflater.inflate(R.layout.rsswebviewcell, null);
                    WebView webView = (WebView)webViewCell.findViewById(R.id.webView);
                    webView.loadData(appDeleg.getSelectedCompetition().getDescription(), "text/html", "utf-8");
                    i++;
                    return webViewCell;
                }
                case 2:
                {
                    View buttonCell = inflater.inflate(R.layout.rssbuttoncell, null);
                    ImageButton btnWebView = (ImageButton)buttonCell.findViewById(R.id.btn_WebView);
                    i = 0;
                    return btnWebView;
                }
            }
            return convertView;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文