Android 模拟器操作演练
2010-02-20 17:15:00 来源:WEB开发网import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ac01 extends ListActivity {
public static int g_variable;
public static final String AUTHORITY = "com.misoo.provider.rx09-02";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/Student");
private static final String[] PROJECTION
= new String[]{ "stud_no", "stud_name"};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getData() == null) intent.setData(CONTENT_URI);
Cursor cur = getContentResolver().query(getIntent().getData(),
PROJECTION, null, null, null);
ArrayList> coll = new ArrayList>();
Map item;
cur.moveToFirst();
while (!cur.isAfterLast()) {
item = new HashMap();
item.put("c1", cur.getString(0) + ", " + cur.getString(1));
coll.add(item);
cur.moveToNext();
}
this.setListAdapter(new SimpleAdapter(this, coll,
android.R.layout.simple_list_item_1, new String[] { "c1" },
new int[] { android.R.id.text1 }));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) { finish();}
}
指令:
Cursor cur = getContentResolver().query(getIntent().getData(),
PROJECTION, null, null, null);
要求Android代为寻找适合的ContentProvider来提供服务,并不刻意指定由DataProvider物件来担任。只要合乎ConentProvider介面,且符合意图条件的物件皆可以来为ac01物件提供服务。于是,ac01程式码就不再直接呼叫DataProvider类别的函数了,而是呼叫ContentProvider介面所提供的函数。再来仔细看看DataProvider类别与ContentProvider介面的搭配情形:
更多精彩
赞助商链接