Modify code to use ArrayList instead of SimpleCursor
Hi this is the code I'm looki开发者_如何学JAVAng at:
https://github.com/findup/Android_Sample_TodoApp/blob/master/src/jp/co/example/testapp/MainActivity.java
Around line 127, it chooses to use a database connection to fetch content from the database.
Instead of fetching data from the database, I'd like to use an ArrayList to hold the data. Could anyone help me figure out what I need to do? Thanks!
Not a one-for-one example based on your code, but this is will populate a list from a database table without a simple cursor:
public class MyListAdapter extends ListActivity {
List<ContactGroup> groups = new ArrayList<ContactGroup>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.groups = getGrpList();
ContactGroupAdapter cAdapter = new ContactGroupAdapter(this);
setListAdapter(cAdapter);
}
private List<ContactGroup> getGrpList(){
List<ContactGroup> grps = new ArrayList<ContactGroup>();
ContentResolver cr = getContentResolver();
Cursor groupCur = cr.query(Groups.CONTENT_URI, new String [] {Groups._ID, Groups.NAME}, null, null, Groups.NAME + " ASC");
if (groupCur.getCount() > 0) {
while (groupCur.moveToNext()) {
ContactGroup newGroup = new ContactGroup();
newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
grps.add(newGroup);
}
}
return grps;
}
public class ContactGroupAdapter extends BaseAdapter{
public ContactGroupAdapter(Context c) {
mContext = c;
}
public int getCount() {
return groups.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater vi = LayoutInflater.from(this.mContext);
convertView = vi.inflate(R.layout.two_line_list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else {
//Get view holder back
holder = (ViewHolder) convertView.getTag();
}
ContactGroup cg = groups.get(position);
if (cg != null) {
//Name
holder.toptext = (TextView) convertView.findViewById(R.id.text1);
holder.toptext.setText(cg.Name);
//ID
holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
holder.bottomtext.setText(cg.Id);
}
return convertView;
}
private Context mContext;
}
public static class ViewHolder {
TextView toptext;
TextView bottomtext;
}
public class ContactGroup{
public String Id;
public String Name;
}
}
Then there are XML files ...
two_line_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/text1" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/text2" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
and main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="@id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="No Groups" />
</LinearLayout>
精彩评论