嗨,这是我正在看的代码:
在第127行附近,它选择使用数据库连接从数据库中获取内容。
我不想从数据库中获取数据,而是使用ArrayList来保存数据。谁能帮我弄清楚我需要做什么?谢谢!
答案 0 :(得分:0)
不是基于您的代码的一对一示例,但这将填充数据库表中的列表而不使用简单的游标:
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;
}
}
然后是XML文件...
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>
和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>