从两个数据库项创建列表

时间:2011-09-21 19:33:38

标签: android database list

我想根据我在数据库中的一些数据制作一个列表。

我数据库中的前两组数据是名字和姓氏。

我希望我的列表显示名字和姓氏,而不是现在只显示名字的地方。我怎么做?我的代码如下所示:

    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);

}

2 个答案:

答案 0 :(得分:0)

这是一个完整的实现。您需要创建自定义行和自定义数组适配器。 这是一个完整的教程http://commonsware.com/Android/excerpt.pdf

这将告诉您完成此操作需要了解的所有信息。

另请参阅此处我发布了另一个示例。

How to add an EditText to a ListView

编辑:如何构建自定义列表视图并从数据库

返回数据

首先创建一个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));

这应该这样做。只需将信息替换为应有的信息。这是我为你准备的一个教程。

答案 1 :(得分: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>