user5111
user5111

Reputation: 165

Listview with different 1 row different and other all same. Here is my code :

Here is my code i am using to make such list but when i scroll the list it crashes.

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (position == 0) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.custom_diary_listitem,
                        parent, false);
                holder = new ViewHolder();
                holder.tv_customItem_title = (TextView) convertView
                        .findViewById(R.id.custom_diary_listitem_tv_commenttitle);
                holder.tv_customItem_date = (TextView) convertView
                        .findViewById(R.id.custom_diary_listitem_tv_commentdate);
                holder.tv_customItem_comment = (TextView) convertView
                        .findViewById(R.id.custom_diary_listitem_tv_comment);
                holder.btn_customItem_newcomment = (Button) convertView
                        .findViewById(R.id.custom_diary_listitem_btn_addnewcomment);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_customItem_title.setText(strDiaryDetailTitle);
            holder.tv_customItem_date.setText(strCommentDate);
            holder.tv_customItem_comment.setText(strCommentTitle);
            holder.btn_customItem_newcomment
                    .setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            context.startActivity(new Intent(context,
                                    NewCommentActivity.class));
                        }
                    });

        } else {
            if (convertView == null) {
                convertView = inflater.inflate(layout, parent, false);

                holder = new ViewHolder();

                holder.tv_title = (TextView) convertView
                        .findViewById(R.id.newcomment_listitem_tv_content);

                holder.tv_date = (TextView) convertView
                        .findViewById(R.id.newcomment_listitem_tv_date);

                convertView.setTag(holder);
            } else if (convertView.getId() != R.id.custom_diary_listitem_lin_main) {

                convertView = inflater.inflate(layout, parent, false);

                holder = new ViewHolder();

                holder.tv_title = (TextView) convertView
                        .findViewById(R.id.newcomment_listitem_tv_content);

                holder.tv_date = (TextView) convertView
                        .findViewById(R.id.newcomment_listitem_tv_date);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_title.setText(arrPostData.get(position - 1)
                    .getDiarySubTitle().get(position - 1));

            holder.tv_date.setText(arrPostData.get(position - 1).getDiaryDate()
                    .get(position - 1));
        }
        return convertView;
    }

    class ViewHolder {

        TextView tv_title, tv_date, tv_customItem_title, tv_customItem_date,
                tv_customItem_comment, btn_customItem_newcomment;

    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

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

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

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            type = TYPE_ITEM1;
        } else if (position == 1) {
            type = TYPE_ITEM2;
        }

        return type;
    }

    @Override
    public int getViewTypeCount() {

        return 2;

    }

    @Override
    public boolean areAllItemsEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return false;
    }

}

Here is my logcat : -

12-19 15:48:25.161: E/AndroidRuntime(1363): FATAL EXCEPTION: main
12-19 15:48:25.161: E/AndroidRuntime(1363): java.lang.NullPointerException
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.vervesys.bmgo.adapter.CommentAdapter.getView(CommentAdapter.java:123)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.AbsListView.obtainView(AbsListView.java:2251)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.ListView.makeAndAddView(ListView.java:1769)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.ListView.fillDown(ListView.java:672)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.ListView.fillGap(ListView.java:636)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5036)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3193)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3467)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.vervesys.bmgo.MyDetailDiary.onTouch(MyDetailDiary.java:124)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.View.dispatchTouchEvent(View.java:7122)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.View.dispatchPointerEvent(View.java:7307)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4203)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4243)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.Choreographer.doFrame(Choreographer.java:523)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.os.Handler.handleCallback(Handler.java:615)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.os.Looper.loop(Looper.java:137)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at java.lang.reflect.Method.invoke(Method.java:511)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 15:48:25.161: E/AndroidRuntime(1363):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 61

Answers (1)

Kelevandos
Kelevandos

Reputation: 7082

Please use the below methods:

getViewTypeCount() - make this return 2, for two view types you need

getItemViewType(int position) - make it return 0 for the first element and 1 for the rest

And then, in the getView() method:

switch(getItemViewType(position)){
    case 0:
        /*Implement the Holder pattern for the first view */
    break;
    case 1:
        /*Implement the Holder pattern for the second view */
    break;
}

What this gives you is two separate "stacks" of convertViews, so you do not have to overwrite a completely different view type :-)

Upvotes: 1

Related Questions