2011-09-21 41 views
0

我想從我的數據庫中的某些數據列出清單。從兩個數據庫項目列出清單

我的數據庫中的前兩組數據是名和姓。

我想讓我的列表顯示名字和姓氏,而不是現在它只顯示名字的地方。我怎麼做?我的代碼如下所示:

private void fillData() 
{ 
    Cursor contactCursor = mDbHelper.fetchAllReminders(); 
    startManagingCursor(contactCursor); 

    String[] from = new String[]{DbAdapter.KEY_FIRST}; 

    int[] to = new int[]{R.id.contactlist}; 

    SimpleCursorAdapter contacts = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from, to); 
    setListAdapter(contacts); 

} 
+0

有你嘗試製作自定義列表適配器? –

+0

你可以去自定義列表適配器,並使用LayoutInflate。 –

回答

0

這裏是一個完整的實現。您需要創建一個自定義行和一個自定義數組適配器。 這裏是一個完整的教程http://commonsware.com/Android/excerpt.pdf

這將告訴你一切你需要知道完成這件事。

另請參閱這裏,另外一個例子。

How to add an EditText to a ListView

編輯:如何建立一個自定義列表視圖,並從DATABSE

首先創建一個ListView活動返回數據。

public class meeting_list extends ListActivity { 


Cursor model = null; 
meetingAdapter adapter = null; 

    //This should be what ever your database helper is from your SQLite base. 
meetingHelper helper = null; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.meeting_list); 
    helper = new meetingHelper(this); 
    model = helper.getAll(); 
    startManagingCursor 
    (model); 


adapter = new meetingAdapter(model); 
    setListAdapter(adapter); 
    registerForContextMenu(getListView()); 

//Ondestroy is used to close the database to free up resources 
    @Override 
public void onDestroy(){ 
    super.onDestroy(); 

    helper.close(); 
} 

    @Override 
public void onListItemClick(ListView list, View view, int position, long id){ 
    Intent i = new Intent(meeting_list.this, meeting_create_edit.class); 

      // 
    i.putExtra(ID_EXTRA, String.valueOf(id)); 

    startActivity(i); 
} 

//Here create a class to extend the Cursor Adapter 
class meetingAdapter extends CursorAdapter{ 
    meetingAdapter(Cursor c){ 
     super(meeting_list.this, c); 
    } 




@Override 
    public void bindView(View row, Context ctxt, Cursor c) { 

     meetingHolder holder = (meetingHolder)row.getTag(); 
     holder.populateFrom(c, helper); 

    } 


@Override 
    public View newView(Context ctxt, Cursor c, ViewGroup parent) { 
     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.mrow, parent, false); 
     meetingHolder holder = new meetingHolder(row); 
     row.setTag(holder); 
     return row; 
    } 
} 


//Here create a class to actually hold the view for the row in the listview. 
static class meetingHolder{ 
    private TextView mtitle = null; 
    private TextView maddress = null; 
    private ImageView Icon = null; 

    meetingHolder(View row){ 
     mtitle=(TextView)row.findViewById(R.id.mtitle); 
     maddress = (TextView)row.findViewById(R.id.address); 
     Icon = (ImageView)row.findViewById(R.id.Micon); 

    } 

//Here populate the row with the data from your database 
    void populateFrom(Cursor c, meetingHelper helper){ 

      mtitle.setText(helper.getMettingTitle(c)); 
      maddress.setText(helper.getAddress(c)); 

這應該這樣做。只需將您的信息替換到應該在的位置即可。這是一個爲你準備的教程。

+0

您基本上只需要創建一個自定義行,其中包含兩個文本視圖,一個用於名字,另一個用於姓氏。然後,當您創建自定義適配器時,您會希望覆蓋getView()。該教程將爲您提供您需要了解的一切。 –

+0

嘿,我讀了pdf,試圖做成一些東西,但我並沒有讓它工作。我無法確定如何使用數據庫填充列表。你能告訴我如何做到這一點的代碼嗎? – unnamet

+0

好的,給我一下。 –

0

現在,我已經嘗試根據您的指導編輯代碼,我已經爲現在做看起來是這樣的:

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 

     mDbHelper = new DbAdapter(this); 
     mDbHelper.open(); 
     fillData();  

} 

private void fillData() 
{ 
    Cursor contactCursor = mDbHelper.fetchAllReminders(); 
    startManagingCursor(contactCursor); 

    String[] from = new String[]{DbAdapter.KEY_FIRST}; 

    int[] to = new int[]{R.id.contactlist}; 

    SimpleCursorAdapter contactsfirst = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from, to); 

    String[] from2 = new String[]{DbAdapter.KEY_LAST}; 

    int[] to2 = new int[]{R.id.contactlist}; 

    SimpleCursorAdapter contactslast = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from2, to2); 

    setListAdapter(contactsfirst,last); 

} 

而且我的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="wrap_content" 
    android:orientation="horizontal" 
> 
<TextView 
android:id="@+id/first" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="40sp" 
/> 
<TextView 
android:id="@+id/last" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="40sp" 
    /> 
</LinearLayout> 
+0

它看起來像你填寫正確的數據。你重寫了BaseAdapter嗎?你的名單實際上是否填充任何東西? –

+0

我如何重寫baseadapter?但列表劑量填充任何東西,因爲該程序需要編譯 – unnamet

+0

LOL哇......好吧,我很快就把東西放在一起。你必須在教程中遇到問題? –