2010-01-16 6 views
2

프로그래밍 지식이 희박하여 며칠 전에 Java와 Android SDK를 발견했습니다.ListAdapters/SQLite 데이터베이스를 사용하는 Begginner

나는의 SQLiteDatabase하는 SQLiteOpenHelper, 제대로 을 작업 커서가 현재

(내가 커서를하는 SQLiteOpenHelper를 사용하여 DB를 열 생성/내 DB에 쿼리를하고 얻는 방법을 이해 생각을 의미) , 나는 + labName + labCity를 하나의 결과 문자열로 랩핑하고 하나의 TextView R.id.label로 표시합니다.

labName을 R.id.label에 바인딩하고 labCity를 다른 TextView에 바인딩 할 수 있습니까? R.id. 라벨 2?

public class MainLabList extends ListActivity implements OnItemClickListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_lab_list); 

    // DB 
    GestionDB gdb = new GestionDB(this, GestionDB.DATABASE_NAME, null, GestionDB.DATABASE_VERSION); 
    SQLiteDatabase theDB = gdb.getWritableDatabase(); 

    // Cursor 
    String[] columns = {GestionDB.LAB_NAME, GestionDB.LAB_ADDRESS_CITY}; 
    Cursor c = 
     theDB.query(GestionDB.TABLE_NAME, columns, null, null, null, null, null); 

    //////// 
    this.setTitle(" " + c.getCount() + " Labs in Local Database"); 
    ArrayList<String> results = new ArrayList<String>(); 
    int i = 0; 
    c.moveToFirst(); 

    /* Loop through all Results */ 
    do { 
     i++;   
     String labName = c.getString(c.getColumnIndex(GestionDB.LAB_NAME)); 
     String labCity = c.getString(c.getColumnIndex(GestionDB.LAB_ADDRESS_CITY)); 

     /* Add current Entry to results. */ 
     results.add("" + i + ": " + labName 
         + " (" + labCity + ")"); 
    } while (c.moveToNext()); 

    //need rework    
    ListView lvLabListing = (ListView) findViewById(android.R.id.list); 
    ArrayAdapter<String> labListAdapter = new ArrayAdapter<String>(this, 
      R.layout.listing, R.id.label, results); 
    lvLabListing.setAdapter(labListAdapter); 
    lvLabListing.setOnItemClickListener(this); 
    // 

    // don't forget to close the database 
    gdb.close(); 

그리고 내가하고 싶은 listing.xml 수정 : 여기

내가 도움이 필요 내 ListActivity의 코드

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:layout_width="fill_parent"> 
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label"></TextView> 
</LinearLayout> 

내 GestionDB 클래스는 간단합니다, 그냥 몇 가지를 정의 데이터베이스가 존재하지 않으면 몇 가지 변수와 함께 열.

답변

0

CursorAdapter, 아마도 SimpleCursorAdapter을 사용해야합니다. Here is an example.

+0

원본 예제가 도움이되었습니다. SimpleCursorAdapter를 사용했지만 예외에서 멈추었습니다. java.lang.IllegalArgumentException : '_id'열이 없습니다. 쿼리에 _ID 열이없는 경우이 예외가 발생합니다. 내 첫 번째 질문에 대해 고맙습니다 :) 해결 – Dullahx

+0

예, CursorAdapter 가족의 제한 사항 중 하나입니다. '_ID'라는 결과 집합에 'long'으로 변환 할 수있는 열이 있어야합니다 . – CommonsWare

관련 문제