如何修复Integer无法在ViewHolder中强制转换

时间:2015-12-30 11:55:08

标签: android-fragments android-viewholder

我有适配器并将详细信息检索到listview

public class TestViewModel : BindableBase
{
    private ObservableCollection<TestElement> _testElementItemsSource;
    private TestElement _testElementSelectedItem;

    public ObservableCollection<TestElement> TestElementItemsSource
    {
        get { return _testElementItemsSource; }
        set { this.SetProperty(ref this._testElementItemsSource, value); }
    }

    public TestElement TestElementSelectedItem
    {
        get { return _testElementSelectedItem; }
        set { this.SetProperty(ref this._testElementSelectedItem, value); }
    }
}

public class TestElement : BindableBase
{
    private Guid _id;
    private string _caption;
    private ObservableCollection<TestElement> _children;

    public Guid ID
    {
        get { return _id; }
        set { this.SetProperty(ref this._id, value); }
    }

    public String Caption
    {
        get { return _caption; }
        set { this.SetProperty(ref this._caption, value); }
    }

    public ObservableCollection<TestElement> Children
    {
        get { return _children; }
        set { this.SetProperty(ref this._children, value); }
    }
}

这是我的日志

private class ChatDisplayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public ChatDisplayAdapter() {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        private class ViewHolder {

            TextView chatTitle;
            TextView chatPlace;
            TextView chatDate;
            TextView notificationCount;

        }


        @Override
        public int getCount() {
            return groupEventMoList.size();
        }

        @Override
        public Object getItem(int position) {
            return groupEventMoList.get(position);
        }

        @Override
        public long getItemId(int id) {
            // for sqllite management
            return 0;
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.chatwindow, null);
                holder = new ViewHolder();
                convertView.setClickable(true);
                convertView.setFocusable(true);

                holder.chatTitle = (TextView) convertView.findViewById(R.id.chat_title);
                holder.chatPlace = (TextView) convertView.findViewById(R.id.event_place);
                holder.chatDate = (TextView) convertView.findViewById(R.id.event_date);
                holder.notificationCount = (TextView) convertView.findViewById(R.id.notification_count);
                holder.chatPlace.setTextColor(getResources().getColor(R.color.black));
                holder.chatDate.setTextColor(getResources().getColor(R.color.black));
                holder.chatTitle.setTextColor(getResources().getColor(R.color.black));
                holder.notificationCount.setTextColor(getResources().getColor(R.color.black));
                convertView.setTag(holder);
                //Log.e("view", "holder" + convertView.getTag());
            }
            else {

              //  Log.e("view", "holder" + convertView.getTag());
                holder = (ViewHolder) convertView.getTag();
              //  Log.e("view", "holder" + holder);

            }

           // holder = (ViewHolder) convertView.getTag();
            convertView.setTag(groupEventMoList.get(position));
            holder.chatPlace.setText(groupEventMoList.get(position).getPlace());
            holder.notificationCount.setText(Integer.toString(groupEventMoList.get(position).getCount()));
            holder.chatTitle.setText(groupEventMoList.get(position).getText());

            String actualDate = groupEventMoList.get(position).getEventDate();
            Log.e("view", "notification" + groupEventMoList.get(position).getCount());
            Log.e("view", "after notification position" + position);
            try {
                //date format changed here
                Date formatDate = new SimpleDateFormat("yyyy-MM-dd").parse(actualDate);
                dateResult = new SimpleDateFormat("dd-MM-yyyy").format(formatDate);
                holder.chatDate.setText(dateResult);
            } catch (ParseException e) {

                e.printStackTrace();
            }

            Log.e("view", "position" + groupEventMoList.get(position).getPlace());

            final EventMO eventMO = groupEventMoList.get(position);
            convertView.setTag(position);
            View v = convertView.findViewById(R.id.chat_window_single);
            v.getRootView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("position", v.getTag().toString());
                    Intent groupAct = new Intent(context, GroupChatActivity.class);
                    groupAct.putExtra("eventMo", eventMO);
                    groupAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(groupAct);

                }

            });


            return convertView;
        }

    }

}

我在其他部分确实有错误。如何解决这个问题

我在此行附近收到错误:holder =(ViewHolder)convertView.getTag(); 持有人返回0

1 个答案:

答案 0 :(得分:1)

您正在设置视图标记三次。请注意以下几行:

convertView.setTag(holder);
...
convertView.setTag(groupEventMoList.get(position));
...
convertView.setTag(position);

使用最后一行,您将Integer设置为标记,这就是您获得ClassCastException的原因。 删除多余的setTag,只留下第一个正确的。

类似案例herehere