0

我有一个适配器,它显示了一个 listView 的两个视图——一个 checktextview 和一个普通的 textview。我想在用户不在时使用checkedtextview,MarkMode以便他/她可以标记项目,并且我想在用户不在时使用textview MarkMode

所以这是我的原始代码,它只有MarkMode.

public class ContactsAdapter extends BaseAdapter {

    private List<Contact> mData;
    private LayoutInflater mInflater;
    private Context context;

    public ContactsAdapter(List<Contact> mData, Context context) {
        this.mData = mData;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

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

    @Override
    public String getItem(int position) {
        switch (context.getSharedPreferences("settings",
                HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
        case 1:
            return mData.get(position).getLastName() + " "
                    + mData.get(position).getFirstName();
        case 2:
            return mData.get(position).getFirstName() + " "
                    + mData.get(position).getMobileNumber();
        case 3:
            return mData.get(position).getLastName() + " "
                    + mData.get(position).getMobileNumber();
        default:
            return mData.get(position).getFirstName() + " "
                    + mData.get(position).getLastName();
        }
    }

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

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

        CheckedTextView result;

        if (convertView == null) {
            result = (CheckedTextView) mInflater.inflate(
                    R.layout.home_screen_contacts_mark_view, parent, false);
        } else {
            result = (CheckedTextView) convertView;
        }

        final String contactRow = getItem(position);
        result.setText(contactRow);
        result.setBackgroundResource(R.drawable.list_selector);

        return result;
    }

}

现在我把它改成了这个,所以我可以改变一个名为的类变量isMarkMode,这样它就可以显示不同的视图......

public class ContactsAdapter extends BaseAdapter {

    private List<Contact> mData;
    private LayoutInflater mInflater;
    private Context context;
    public static boolean inMarkMode = false;
    public ContactsAdapter(List<Contact> mData, Context context) {
        this.mData = mData;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

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

    @Override
    public String getItem(int position) {
        switch (context.getSharedPreferences("settings",
                HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
        case 1:
            return mData.get(position).getLastName() + " "
                    + mData.get(position).getFirstName();
        case 2:
            return mData.get(position).getFirstName() + " "
                    + mData.get(position).getMobileNumber();
        case 3:
            return mData.get(position).getLastName() + " "
                    + mData.get(position).getMobileNumber();
        default:
            return mData.get(position).getFirstName() + " "
                    + mData.get(position).getLastName();
        }
    }

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

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

                CheckedTextView result = null;
        TextView normalResult = null;

        if (inMarkMode) {
            if (convertView == null) {
                result = (CheckedTextView) mInflater.inflate(
                        R.layout.home_screen_contacts_mark_view, parent, false);
            } else {
                result = (CheckedTextView) convertView;
            }
        } else {
            if (convertView == null) {
                normalResult = (TextView) mInflater.inflate(
                        R.layout.home_screen_contacts_view, parent, false);
            } else {
                normalResult = (TextView) convertView;
            }
        }
        final String contactRow = getItem(position);
        result.setText(contactRow);
        result.setBackgroundResource(R.drawable.list_selector);
        normalResult.setText(contactRow);
        normalResult.setBackgroundResource(R.drawable.list_selector);

        if (inMarkMode) {
            return result;
        } else {
            return normalResult;
        }
    }
    }

}

最初它看起来像是在工作,但后来它停止了,我收到一个 logcat 错误,说NullPointerException这一行有一个:result.setText(contactRow);

我怎样才能解决这个问题?任何帮助,将不胜感激。

4

2 回答 2

1

resultinMarkModeis 时未初始化false。所以result.setText(contactRow);在 if block wheninMarkMode是这样的时候移动到里面true

 final String contactRow = getItem(position);
 if (inMarkMode) {
            //.....your code here..
            result.setText(contactRow);
            result.setBackgroundResource(R.drawable.list_selector);
  } else {
             //.....your code here..
            normalResult.setText(contactRow);
            normalResult.setBackgroundResource(R.drawable.list_selector);
        }
于 2013-10-12T11:48:45.680 回答
0

将您的 Getview 方法更改为此

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

            CheckedTextView result = null;
            TextView normalResult = null;

            if (inMarkMode) {
                if (convertView == null) {
                    result = (CheckedTextView) mInflater.inflate(
                            R.layout.home_screen_contacts_mark_view, parent, false);
                } else {
                    result = (CheckedTextView) convertView;
                }
            } else {
                if (convertView == null) {
                    normalResult = (TextView) mInflater.inflate(
                            R.layout.home_screen_contacts_view, parent, false);
                } else {
                    normalResult = (TextView) convertView;
                }

            }
final String contactRow = getItem(position);

            if (inMarkMode) {

            result.setText(contactRow);
            result.setBackgroundResource(R.drawable.list_selector);
                return result;
            } else {
            normalResult.setText(contactRow);
            normalResult.setBackgroundResource(R.drawable.list_selector);
                return normalResult;
            }
        }
        } 
于 2013-10-12T11:54:21.643 回答