2009-12-11 4 views
3

로 퍼팅하기 전에 SQLite는 데이터를 변경 :ListActivity : 이것은 내 ListActivity 인 행

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

    mDbHelper = new MyDbAdapter(this); 
    mDbHelper.open(); 
    fillData(); 

    registerForContextMenu(getListView()); 
} 

private void fillData() { 
    // Get all of the rows from the database and create the item list 
    Cursor c = mDbHelper.fetchAllTransactions(); 
    startManagingCursor(c); 

    // Create an array to specify the fields we want to display in the list 
    String[] from = new String[]{MyDbAdapter.KEY_DATE, MyDbAdapter.KEY_NAME}; 

    // and an array of the fields we want to bind those fields to 
    int[] to = new int[]{R.id.row_date, R.id.row_name}; 

    // Now create a simple cursor adapter and set it to display 
    setListAdapter(new SimpleCursorAdapter(this, R.layout.my_list_row, c, from, to)); 
} 

이 내 행 레이아웃입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/row_date" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
    <TextView android:id="@+id/row_name" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
</LinearLayout> 

날짜가 실제로 INT에 저장됩니다 SQLLite 데이터베이스이므로 정수로 표시됩니다.

int에서 날짜를 만드는 방법을 알고 있으므로 1216544611 (또는 무엇이든)이 아닌 "12/11/2009 11:32"으로 표시되는 방법을 알고 있습니까?

+0

저는 몇 년 늦었지만 작성하는 것이 가장 좋습니다. SimpleCursorAdapter의 커스텀 ViewBinder. # setViewBinder() 메서드를 호출하여 S.C.A에 연결할 수 있습니다. 이것은 당신이 묘사하는 것을 할 수있는 '의도 된'방법 일 가능성이 높습니다. –

답변

2

int를 날짜 문자열로 포맷하는 CursorAdapter을 직접 작성해야 할 것으로 생각됩니다. 트릭이 표시되기 전에 목록 항목의 형식을 bindView()을 구현하는 것입니다 :

private final class MyCursorAdapter extends CursorAdapter { 
    MyCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
    } 

    @Override public void bindView(View view, Context context, Cursor cursor) {    

     // Format the date string 
     TextView dateView = (TextView) view.findViewById(R.id.row_date); 
     long millis = cursor.getLong(cursor.getColumnIndex(MyDbAdapter.KEY_DATE)); 
     dateView.setText(DateFormat.getDateInstance().format(new Date(milis))); 

     // Do the rest of your view formatting 
     ... 
    } 

    @Override View public newView(Context context, Cursor cursor, ViewGroup parent) {   
     View view = getLayoutInflater().inflate(R.layout.my_list_row, null); 
     bindView(view, context, cursor); 
     return view;   
    } 
} 

과에 연결하여 ActivityonCreate()의 : 나 같은 사람들을 위해

setListAdapter(new MyCursorAdapter(this, c)); 
1

getLayoutInflater()에서 무엇을 궁금해하는 사람들하는 구현되지 않았거나 CursorAdapter에 의해 상속 된 경우 다음을 시도하십시오.

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);