API版本的方法的适配

核心代码:

MainActivity .java

package com.itheima.db;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.itheima.db.dao.PersonDao2;
import com.itheima.db.domain.Person;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListView lv = (ListView) findViewById(R.id.lv);
		PersonDao2 dao = new PersonDao2(this);

		if (Build.VERSION.SDK_INT >= 11) {
			// api 11以上才可以运行
			lv.setAdapter(new SimpleCursorAdapter(this, R.layout.list_item, dao
					.findAllCursor(),
					new String[] { "name", "phone", "money" }, new int[] {
							R.id.tv_name, R.id.tv_phone, R.id.tv_money }, 0));
			Log.i(TAG,"我是高版本api");
		} else {

			// api 11 一下运行
			lv.setAdapter(new SimpleCursorAdapter(this, R.layout.list_item, dao
					.findAllCursor(),
					new String[] { "name", "phone", "money" }, new int[] {
							R.id.tv_name, R.id.tv_phone, R.id.tv_money }));
			Log.i(TAG,"我是低版本api");
		}

	}

}



你可能感兴趣的:(Android基础)