3

これは私のコードです。ここでは、Arraylistからアイテムを削除し、notifyDataSetChangedを使用してレイアウトアイテムを更新しようとしています。

public class GroupContactListingAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<ContactListDto> groupListDto = new ArrayList<ContactListDto>();
    private Activity context;
    public static ArrayList<String> selectList;

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    /**
     * Assigning the parameter
     * 
     * @param mContext1
     *            Getting the context of the class
     * @param contactList
     *            list to be display
     * @param value
     *            if 0 all, if 1 unblocked and 2 blocked
     * @param isContactList
     *            if 1 then contact list and if 0 search contact list
     */
    public GroupContactListingAdapter(Activity mContext1,
            ArrayList<ContactListDto> contactList) {
        this.context = mContext1;
        this.mInflater = (LayoutInflater) mContext1
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (this.groupListDto != null) {
            this.groupListDto.clear();
        }
        this.groupListDto = contactList;
        selectList = new ArrayList<String>();
    }

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

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

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        final ViewHolder viewHolder;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.group_listing_item, null);
            viewHolder = new ViewHolder();
            viewHolder.nameTextView = (TextView) convertView
                    .findViewById(R.id.group_name);
            viewHolder.relative = (RelativeLayout) convertView
                    .findViewById(R.id.group_layout);
            viewHolder.deleteContacts = (Button) convertView
                    .findViewById(R.id.btn_group_contact_delete);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.deleteContacts.setVisibility(View.VISIBLE);
        viewHolder.deleteContacts
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Helper.printLogD(" deleting position is " + position);
                        selectList.add(groupListDto.get(position)
                                .getContactId());
                        groupListDto.remove(groupListDto.get(position));

                        notifyDataSetChanged();


                    }
                });

        viewHolder.nameTextView.setText(groupListDto.get(position)
                .getContactFullName());
        if (position % 2 == 0) {
            viewHolder.relative.setBackgroundColor(context.getResources()
                    .getColor(R.color.white));
        } else {
            viewHolder.relative.setBackgroundColor(context.getResources()
                    .getColor(R.color.list_even_color));
        }
        return convertView;
    }

次のエラーが発生します

12-18 11:43:01.895: E/ListView(24434): null
12-18 11:43:01.895: E/ListView(24434): java.lang.NullPointerException
12-18 11:43:01.895: E/ListView(24434):  at com.hazemedia.stewdent.client.android.ui.adapter.GroupContactListingAdapter$ViewHolder.access$4(GroupContactListingAdapter.java:133)
12-18 11:43:01.895: E/ListView(24434):  at com.hazemedia.stewdent.client.android.ui.adapter.GroupContactListingAdapter.getView(GroupContactListingAdapter.java:94)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.AbsListView.obtainView(AbsListView.java:1428)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.ListView.makeAndAddView(ListView.java:1801)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.ListView.fillSpecific(ListView.java:1339)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.ListView.layoutChildren(ListView.java:1625)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.AbsListView.onLayout(AbsListView.java:1279)
12-18 11:43:01.895: E/ListView(24434):  at android.view.View.layout(View.java:7321)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
12-18 11:43:01.895: E/ListView(24434):  at android.view.View.layout(View.java:7321)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-18 11:43:01.895: E/ListView(24434):  at android.view.View.layout(View.java:7321)
12-18 11:43:01.895: E/ListView(24434):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-18 11:43:01.895: E/ListView(24434):  at android.view.View.layout(View.java:7321)
12-18 11:43:01.895: E/ListView(24434):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1217)
12-18 11:43:01.895: E/ListView(24434):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1995)
12-18 11:43:01.895: E/ListView(24434):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 11:43:01.895: E/ListView(24434):  at android.os.Looper.loop(Looper.java:150)
12-18 11:43:01.895: E/ListView(24434):  at android.app.ActivityThread.main(ActivityThread.java:4389)
12-18 11:43:01.895: E/ListView(24434):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 11:43:01.895: E/ListView(24434):  at java.lang.reflect.Method.invoke(Method.java:507)
12-18 11:43:01.895: E/ListView(24434):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
12-18 11:43:01.895: E/ListView(24434):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-18 11:43:01.895: E/ListView(24434):  at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

3

この問題は、convertView インスタンスで viewHolder にタグを付けていないために発生しました。

convertView .setTag(viewHolder);
于 2012-12-19T05:35:45.317 に答える
1

このタイプのコードを試してみてください。

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    ViewHolder mViewHolder;
    if(convertView == null){
        mViewHolder = new ViewHolder();
        mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.group_listing_item, null);
        mViewHolder. nameTextView = (TextView) convertView .findViewById(R.id.group_name);
        mViewHolder.relative = (RelativeLayout) convertView .findViewById(R.id.group_layout);
        mViewHolder.deleteContacts = (Button) convertView .findViewById(R.id.btn_group_contact_delete);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (ViewHolder)convertView.getTag();
    }
    deleteContacts.setVisibility(View.VISIBLE);
    deleteContacts .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Helper.printLogD(" deleting position is " + position);
            selectList.add(groupListDto.get(position) .getContactId());
            groupListDto.remove(groupListDto.get(position));
            notifyDataSetChanged();
        }
    });

    nameTextView.setText(groupListDto.get(position) .getContactFullName());
    if (position % 2 == 0) {
        relative.setBackgroundColor(context.getResources() .getColor(R.color.white));
    } else {
        relative.setBackgroundColor(context.getResources() .getColor(R.color.list_even_color));
    }
    return convertView;
}
public class ViewHolder{
    TextView nameTextView;
    RelativeLayout relative;
    Button deleteContacts;
}
于 2012-12-18T06:58:05.367 に答える