2012-03-28 2 views
2

나는 맞춤 목록 용 코드가 있습니다.android : 데이터베이스에 맞춤 목록 연결

my_list.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="vertical"> 

    <TextView android:id="@+id/text1" 
     android:textSize="16px" 
     android:textStyle="bold" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <TextView android:id="@+id/text2" 
     android:textSize="12px" 
     android:textStyle="italic" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

과 활동

public class ZcustListActivity extends ListActivity { 


//db Work 
SQLiteDatabase db; 
String SQL; 
ArrayList<String> db_results=null; 


    private SimpleAdapter notes; 

    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 

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

    //db Work 
    this.db = this.openOrCreateDatabase("DEMO", MODE_PRIVATE, null); 
    this.db.execSQL("CREATE TABLE IF NOT EXISTS MEN(A VARCHAR,B VARCHAR,C VARCHAR);"); 
    this.db.execSQL("delete from MEN;"); 
    db.execSQL("INSERT INTO MEN(A,B,C) VALUES('AA1','BB1','CC1');"); 
    db.execSQL("INSERT INTO MEN(A,B,C) VALUES('DD2','EE2','FF2');"); 


     notes = new SimpleAdapter(
       this, 
       list, 
       R.layout.my_list, 
       new String[] { "line1","line2" }, 
       new int[] { R.id.text1, R.id.text2 } ); 
     setListAdapter(notes); 

     HashMap<String,String> item = new HashMap<String,String>(); 
     item.put("line1","i am row 1"); 
     item.put("line2","i am row 2"); 
     list.add(item); 
     notes.notifyDataSetChanged(); 
    } 
} 

난 내 데이터베이스 작업을 추가 -하지만 어떻게이 결합?

i'am 안드로이드에 새로운이 ...... 나를 위해

답변

2

찾고있는 무슨이다 DBManager 클래스가 있어야합니다. (. SimpleCursorAdapter 생성자에서 사용된다) 또한

private DBManager dbM = new DBManager(); 
private Cursor c; 

public void populateList(){ 
     dbM.open(); 
     c = this.db.query(MEN, new String[] {"line1", "line2" 
      }, null, null, null, null, null); 
    startManagingCursor(c); 

    String[] from = new String[]{"line1","line2" }; 

     int[] to = new int[]{R.id.text1, R.id.text2}; 

SimpleCursorAdapter notes = 
     new SimpleCursorAdapter (this, R.layout.list_row, c, from, to); 
    setListAdapter(notes); 

} 
} 

, 당신은 당신의 list_row에 대해 별도의 뷰를 생성해야하므로 (모든 레이아웃은 당신이 의지 할 정의 : 당신은 SimpleCursorAdapter로에서 onCreate에 populateList 메소드를 호출 테이블의 각 행에 넣어야하는 데이터로 2 개의 textViews가있는 LinearLayout이됩니다).

+0

감사합니다. 내 업데이트 – Gali

+0

db에 필요한 라인으로 커서를 초기화하고 위에 쓴 코드를 적용하십시오. 예 : 커서 c = db.exeSQL (SELECT * FROM MEN); – Th0rndike

+0

나는 이것을 시험해보고 오류가 발생했다 : c = this.db.fetchAllNotes(); 그것은 말합니다 : fetchAllNotes() 메서드는 SQLiteDatabase 형식에 대해 정의되지 않았습니다 – Gali