Загрузка нескольких макетов в ListView - PullRequest
0 голосов
/ 25 октября 2011

Я пытаюсь отобразить несколько макетов в моем списке.У меня есть только три элемента в виде списка, и каждая строка имеет разный макет.Вот код, который я использую для этого

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)

Может кто-нибудь помочь, пожалуйста?Как я могу сделать это в Android?Спасибо

Ответы [ 2 ]

1 голос
/ 25 октября 2011

Я нашел проблему. Это ошибка с типом возврата в случае 2. Вместо представления я возвращал ImageButton Вот правильный код

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

    return buttonCell;
}
0 голосов
/ 25 октября 2011

Я думаю, что ваша ошибка в том, что вы пытаетесь поместить более 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;
        }
    }
...