package com.example.myfirstapp;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class PersonManagementActivity extends Activity {
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_person_management);
DBOpenHelper db=new DBOpenHelper(this);
PersonService personService=new PersonService();
personService.setDbOpenHelper(db);
/*采用SimpleAdapter
List<Person> personList=personService.queryPerson();
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
for(Person person:personList){
Map<String,Object> map=new HashMap<String,Object>();
map.put("_id", person.getId());
map.put("name", person.getName());
map.put("age", person.getAge());
data.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.activity_person_listviewitem,
new String[]{"id","name","age"},
new int[]{R.id.id,R.id.name,R.id.age});
*/
Cursor c=personService.queryPersonCursor();
//采用SimpleCursorAdapter 必须要有一个'_id'的字段
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.activity_person_listviewitem,c,
new String[]{"_id","name","age"},
new int[]{R.id.id,R.id.name,R.id.age});
ListView listView=(ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView lView=(ListView) parent;
// HashMap<String,Object> item=(HashMap<String, Object>) lView.getItemAtPosition(position);
// Toast.makeText(PersonManagementActivity.this, item.get("_id").toString(),1).show();
Cursor c=(Cursor) lView.getItemAtPosition(position);
Toast.makeText(PersonManagementActivity.this,c.getString(c.getColumnIndex("_id")),1).show();
}
});
}
}