Android:CursorAdapter、ContentResolver、startManagingCursor


public class MainActivity extends ListActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
          
        ContentResolver cr = getContentResolver();//得到contentprovider对象
        final Cursor c = cr.query(Phones.CONTENT_URI, null, null, null, null);//得到表的游标对象
        startManagingCursor(c);
          
        setListAdapter(new CursorAdapter(this,c)
        {
            @Override//找到行布局
            public View newView(Context context, Cursor cursor, ViewGroup viewGroup)
            {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(android.R.layout.simple_list_item_2, null);
                return layout;
            }
              
            @Override//获取和设置行布局控件
            public void bindView(View view, Context context, Cursor cursor)
            {
                TextView tvName = (TextView) view.findViewById(android.R.id.text1);
                TextView tvPhone = (TextView) view.findViewById(android.R.id.text2);
                  
                String name = c.getString(c.getColumnIndex("name"));
                String phone = c.getString(c.getColumnIndex("number"));
                  
                tvName.setText(name);
                tvPhone.setText(phone);
            }
        });
         
    }
      
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
      
}














本文出自 “天空没有痕迹但我飞过” 博客,转载请与作者联系!

你可能感兴趣的:(android,CursorAdapter,contentresolver)