实现ViewHolder模式的问题

时间:2015-06-22 06:48:24

标签: android design-patterns

我正在从一本书开发Android应用程序。 我收到错误:" crimeHolder无法解决"而且我无法理解为什么......

 public View getView(int poisition, View convertView , ViewGroup parent)
        {
            //If we weren't given a view, inflate one
            if (convertView == null)
            {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
                ViewHolder crimeHolder = new ViewHolder();
                crimeHolder.titleTextView = (TextView)convertView.findViewById(R.id.listItemTitleTextView);
                crimeHolder.dateTextView = (TextView)convertView.findViewById(R.id.listItemDateTextView);
                crimeHolder.solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crimeListItemSolvedCheckBox);

                // store the holder with the view
                convertView.setTag(crimeHolder);
            }

            //Configure the view for this Crime
            Crime crime = getItem(poisition);

            if (crime!=null)
            {

这3个显示错误:

                crimeHolder.titleTextView.setText(crime.getTitle());
                crimeHolder.dateTextView.setText(crime.getDate().toString());   
                crimeHolder.solvedCheckBox.setChecked(crime.isSolved());
            }
            return convertView;

        }
    }

    static class ViewHolder
    {
        TextView titleTextView;
        TextView dateTextView;
        CheckBox solvedCheckBox;
    }

2 个答案:

答案 0 :(得分:1)

试试这个......

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="packagename"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="20" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.location.GPS_ENABLED_CHANGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:name="packagename.MyAppilication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Wipro_pos" >
    <activity
        android:name="packagename.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="packagename.CaptureSignature"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="com.findfacts.wipropos.ShopProfileActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="Packagename.CustomProgressBar"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="packagename.LoginActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="packagename.UnsyncRegistrationDetailes"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="packagename.SettingActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="Packagename.VisitActivity"
        android:screenOrientation="portrait" >
    </activity>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <receiver
        android:name="Packagename.reciever.NetworkReceiver"
        android:label="NetworkReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="Packagename.reciever.DateChangedReceiver"
        android:label="DateChangedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
    </receiver>

    <service
        android:name="Packagename.UploadOfflineDataService"
        android:enabled="true" >
    </service>
</application>

答案 1 :(得分:0)

你在if块中声明犯罪持有者,所以在这个区块之外它不可用。请尝试以下代码

public View getView(int poisition, View convertView , ViewGroup parent)
        {
            ViewHolder crimeHolder = null;
            //If we weren't given a view, inflate one
            if (convertView == null)
            {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
                crimeHolder = new ViewHolder();
                crimeHolder.titleTextView = (TextView)convertView.findViewById(R.id.listItemTitleTextView);
                crimeHolder.dateTextView = (TextView)convertView.findViewById(R.id.listItemDateTextView);
                crimeHolder.solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crimeListItemSolvedCheckBox);

                // store the holder with the view
                convertView.setTag(crimeHolder);
            }

            //Configure the view for this Crime
            Crime crime = getItem(poisition);

            if (crime!=null)
            { crimeHolder.titleTextView.setText(crime.getTitle());
            crimeHolder.dateTextView.setText(crime.getDate().toString());   
            crimeHolder.solvedCheckBox.setChecked(crime.isSolved());
        }
        return convertView;

    }
}

static class ViewHolder
{
    TextView titleTextView;
    TextView dateTextView;
    CheckBox solvedCheckBox;
}