SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter三者的使用区别

布局文件

  • 布局文件



    
    

        

        

        
    

    
    

    

  • ListView中item布局



    
    

    
    

    
    

SimpleAdapter

  • 摘要
 SimpleAdapter simpleAdapter = new SimpleAdapter(
                                                   this,
                                                   getData(), 
                                                   R.layout.item_database,
                                                   new String[]{"_id", "name", "age"}, 
                                                   new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});
  • 详细使用
private void init() {
        //打开数据库
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");
        SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        mCursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);
        //实例适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,getData(), R.layout.item_database,
                new String[]{"_id", "name", "age"}, new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});
        //绑定适配器
        mListView.setAdapter(simpleAdapter);
    }

    private List> getData() {
        List> list = new ArrayList<>();

        int idIndex = mCursor.getColumnIndex("_id");
        int nameIndex = mCursor.getColumnIndex("name");
        int ageIndex = mCursor.getColumnIndex("age");

        while (mCursor.moveToNext()) {
            int id = mCursor.getInt(idIndex);
            String name = mCursor.getString(nameIndex);
            int age = mCursor.getInt(ageIndex);

            Map map = new HashMap<>();
            map.put("_id",id);
            map.put("name",name);
            map.put("age",age);

            list.add(map);
        }
        mCursor.close();
        return list;
    }

SimpleCursorAdapter

  • 摘要
 SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                this, 
                R.layout.item_database, 
                cursor, 
                new String[]{"_id","name", "age"}, 
                new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age},
               CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  • 详细使用
private void init() {
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");
        SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);
        // flags:FLAG_REGISTER_CONTENT_OBSERVER,官方推荐使用的标签,当数据发生变化时,通过观察者标签,实时观察数据的变化,并刷新UI界面
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                this, R.layout.item_database, cursor, new String[]{"_id","name", "age"}, new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        mListView.setAdapter(simpleCursorAdapter);
    }

CursorAdapter

  • 摘要
 MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  • 自定义MyCursorAdapter
public class MyCursorAdapter extends CursorAdapter {

    public MyCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_database, null);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.mIdTextView = (TextView) view.findViewById(R.id.tv_id);
        viewHolder.mAgeTextView = (TextView) view.findViewById(R.id.tv_age);
        viewHolder.mNameTextView = (TextView) view.findViewById(R.id.tv_name);

        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        int id = cursor.getInt(cursor.getColumnIndex("_id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        int age = cursor.getInt(cursor.getColumnIndex("age"));


        viewHolder.mIdTextView.setText(id + "");
        viewHolder.mNameTextView.setText(name);
        viewHolder.mAgeTextView.setText(age + "");

    }

    class ViewHolder {
        TextView mIdTextView;
        TextView mAgeTextView;
        TextView mNameTextView;
    }
}
  • 使用
private void init() {
        //拿到数据
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db"); // 数据库文件 /storage/emulated/0/my_database.db
        SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
    //读取数据
        Cursor cursor = sqLiteDatabase.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);

        //适配
        MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mListView.setAdapter(adapter);
    }

你可能感兴趣的:(SimpleAdapter,CursorAdapter,SimpleCursorAdapter)