2011-12-27 2 views
2

내 안드로이드 애플 리케이션에서 DB에서 값을 검색하는 목록을 표시하려면. 내가 간단한 커서 어댑터를 사용하여 그렇게 할 수 있지만 목록보기가 비어 있습니다. 내 code.my 목록보기에는 두 개의 행이 있습니다. app.what에서 내가 잘못하고있는 간단한 커서 어댑터를 어떻게 사용할 수 있습니까? 제안하십시오. 미리 감사드립니다. 간단한 커서 어댑터 android를 사용하여 목록보기에서 값을 가져올 수 없습니까?

<ListView android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"></ListView> 

클래스 파일에서 : list.xml 사용을위한

//list.xml 

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <ListView android:layout_width="fill_parent" android:id="@android:id/android:list" 
     android:layout_height="fill_parent" android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true"></ListView> 

    </RelativeLayout> 


    // textview.xml--> 

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:weightSum="1"> 
     <TextView android:id="@+id/column1" android:background="@drawable/mid" 
     android:layout_height="30dp" 
     android:layout_width="fill_parent" android:layout_weight="0.03"></TextView> 
     <TextView android:layout_height="wrap_content" 
     android:text="TextView" android:id="@+id/column2" android:background="@drawable/mid" 
     android:layout_weight="0.03" android:layout_width="fill_parent"></TextView> 


    </LinearLayout> 




    public class AddFood extends ListActivity{ 

     public void onCreate(Bundle savedInstanceState) 
    { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.listxml); 

      DataBaseHelper db = null; 
     try { 
      db = new DataBaseHelper(this); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
      SQLiteDatabase database= db.getReadableDatabase(); 
      Cursor c=database.query("food_list", new String[]{"_id","food_items"}, null, null, null,null,null); 
      startManagingCursor(c); 

     String[] displayfield=new String[]{"_id","food_items"}; 
     int[] displayViews = new int[] {R.id.column1,R.id.column2}; 

     SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(getApplicationContext(),R.layout.textview, c,displayfield , displayViews); 
      this.setListAdapter(mAdapter); 

      c.close(); 

    } 

    } 
+0

정확히 동일한 있자나으로 일어나고있다. –

+0

나는 목록보기 ID를 -> "@ android : id/list"로 지정했습니다. 그러나 아무것도 작동하지 않습니다. – picaso

답변

1

사용하지 않는 c.close();

사용 : 우리가 분명히 같은 배를 익사에있는 것처럼

SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.textview, c,displayfield , displayViews); 
      setListAdapter(mAdapter); 
관련 문제