来自数据库的Android Listview

来自数据库的Android Listview,android,sql,database,Android,Sql,Database,我是android新手。我在使用sqlite数据库中的数据创建listview时遇到了一个问题。 我在下面找到了从联系人数据创建listview的代码 public class Test extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get a

我是android新手。我在使用sqlite数据库中的数据创建listview时遇到了一个问题。 我在下面找到了从联系人数据创建listview的代码

public class Test extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get a cursor with all people
        Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null);
        startManagingCursor(c);

        ListAdapter adapter = new SimpleCursorAdapter(this,
                // Use a template that displays a text view
                android.R.layout.simple_list_item_1,
                // Give the cursor to the list adatper
                c,
                // Map the NAME column in the people database to...
                new String[] {Contacts.DISPLAY_NAME},
                // The "text1" view defined in the XML template
                new int[] {android.R.id.text1});
        setListAdapter(adapter);
    }

    private static final String[] CONTACT_PROJECTION = new String[] {
        Contacts._ID,
        Contacts.DISPLAY_NAME
    };
}
现在我使用这段代码从数据库中提取数据

SQLiteDatabase myDB= null;
    String TableName = "myTable";
    myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

   // Get a cursor with all people
    Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
现在我如何组合这两个代码,以便在列表中显示从数据库提取的数据


提前感谢。

如果您想列出来自contact和DB两个源的数据,我认为您唯一的选择是使用两个源的返回值构造一个数组

List finalList = new ArrayList();
 Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null);
        startManagingCursor(c);
c.moveToFirst();
迭代此光标并将字符串值填充到finalList

SQLiteDatabase myDB= null;
    String TableName = "myTable";
    myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

   // Get a cursor with all people
    Cursor c2 = myDB.rawQuery("SELECT * FROM " + TableName , null);
   startManagingCursor(c2);
c2.moveToFirst();
String[] ipArray = new String[finalList.size()];
迭代c2游标并向finalList添加值

通过迭代finalList将finalList转换为数组

SQLiteDatabase myDB= null;
    String TableName = "myTable";
    myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

   // Get a cursor with all people
    Cursor c2 = myDB.rawQuery("SELECT * FROM " + TableName , null);
   startManagingCursor(c2);
c2.moveToFirst();
String[] ipArray = new String[finalList.size()];
循环浏览列表并将值填充到ipArray


我手头没有IDE,所以我键入了流程。

如果您的光标中已经有数据,您可以按照下面教程中的代码进行操作


如果是这种情况,只需复制问题中的db代码并替换游标c=getcontentaresolver…..我在问这个问题之前尝试过它。但它在这里显示错误检查日志文件我有类似的问题,而不是使用simplecursoradaptor,我使用了以下机制。游标cur=myDB.rawQuerySELECT*FROM+TableName,null;startManagingCursorcur;List makTempList=新建ArrayList;如果cur.moveToFirst{do{int tempVal=cur.getInt1;String tempValYear=String.valueOftempVal;if!maktemplast.containstempValYear{maktemplast.addtempValYear;}}而cur.moveToNext;}则将列表转换为数组,并将此数组作为列表的输入参数传递。