2011-03-24 35 views
0
ArrayList的

你好,這是我看代碼:修改代碼,而不是使用SimpleCursor

https://github.com/findup/Android_Sample_TodoApp/blob/master/src/jp/co/example/testapp/MainActivity.java

圍繞線127,它選擇使用一個數據庫連接,從數據庫中獲取的內容。

而不是從數據庫中獲取數據,我想使用ArrayList來保存數據。任何人都可以幫我弄清楚我需要做什麼?謝謝!

+0

ArrayList可用於保存數據,但如果要從數據庫中提取數據,則仍需要數據庫連接。這兩個構造解決了不同的問題。 – 2011-03-24 00:58:33

回答

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> 
相關問題