2011-09-21 43 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哇......好吧,我很快就把东西放在一起。你必须在教程中遇到问题? –