2013-05-07 2 views
0

사용자 정의 CursorAdaptor를 사용하여 이미지를 일부 오른쪽 데이터로 표시하는 목록을 y sqlite db에서 가져 오려고합니다. 그러나 지금까지는 빈 화면이나 결과없이 로딩 원을 회전시킵니다. 쿼리 내 데이터베이스 관리 프로그램에 완벽하게 작동한다Custom CursorAdaptor가 아무것도 표시하지 않습니다.

public class FragMvp extends SherlockListFragment { 

    private DataBaseManager dataBase; 
    private MyCursorAdapter mAdapter; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     // creates and open the database so we can use it 
     dataBase = DataBaseManager.instance(); 

     String query = "SELECT mo._id,mo.sprite,mo.iName FROM Mobs AS mo WHERE mo.MobType = 2 ORDER BY mo.iName ASC;"; 

     Cursor cursor = dataBase.select(query); 

     if (cursor != null) { 
      mAdapter = new MyCursorAdapter(ApplicationContextProvider.getContext(), cursor, false); 
      setListAdapter(mAdapter); 

      cursor.close(); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     // Do something with the data 
    } 
} 

내가 ArrayAdapter와를 사용하는 경우, 그것은 작동 및 데이터를 보여줍니다

여기 내 조각 활동이다. 그래서 문제는 데이터베이스와 관련이 없다고 생각합니다.

public class MyCursorAdapter extends CursorAdapter { 

    private final LayoutInflater mInflater; 

    public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) { 
     super(context, c, autoRequery); 
     mInflater = LayoutInflater.from(context); 
     mContext = context; 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return mInflater.inflate(R.layout.my_adapter, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     String mobSprite = cursor.getString(cursor.getColumnIndex("sprite")); 
     String mobName = cursor.getString(cursor.getColumnIndex("iName")); 
     int lvl = cursor.getInt(cursor.getColumnIndex("LVL")); 
     String hp = cursor.getString(cursor.getColumnIndex("HP")); 

     ImageView imageView = (ImageView) view.findViewById(R.id.mobSprite); 
     TextView mobNameView = (TextView) view.findViewById(R.id.mobName); 
     TextView levelView = (TextView) view.findViewById(R.id.lvl); 
     TextView hpView = (TextView) view.findViewById(R.id.hp); 

     int resID = context.getResources().getIdentifier(mobSprite, "drawble", context.getPackageName()); 
     imageView.setImageResource(resID); 
     mobNameView.setText(mobName); 
     levelView.setText(lvl); 
     hpView.setText(hp); 
    } 
} 

그리고 단지의 경우에 XML :

그런 다음, 여기에 CursorAdapter입니다. 아직 끝나지 않았지만, 먼저 몇 가지 결과를보고 어떻게 보이는지보고 싶습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/mobSprite" 
     android:layout_width="55dp" 
     android:layout_height="55dp" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="4dp" > 
    </ImageView> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/mobName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="4dp" 
      android:layout_marginTop="4dp" 
      android:text="@+id/mobName" 
      android:textSize="20sp" /> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/lvlString" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dp" 
       android:text="@string/lvl" 
       android:textSize="12sp" /> 

      <TextView 
       android:id="@+id/lvl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/lvlString" 
       android:text="@+id/lvl" 
       android:textSize="12sp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/hpString" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="15dp" 
       android:text="@string/hp" 
       android:textSize="12sp" /> 

      <TextView 
       android:id="@+id/hp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/hpString" 
       android:text="@+id/hp" 
       android:textSize="12sp" /> 

     </RelativeLayout> 
    </LinearLayout> 

</LinearLayout> 

어디에 문제가 있는지 알 수는 없지만 수많은 자습서를 찾고 항상 동일합니다. CursorAdapter를 실행하고 커서 및 컨텍스트를 전달하고 listAdapter ...를 설정하지만 작동하지 않습니다.

답변

1

문제가 있습니다.

Cursor cursor = dataBase.select(query); 
if (cursor != null) { 
    mAdapter = new MyCursorAdapter(ApplicationContextProvider.getContext(), cursor, false); 
    setListAdapter(mAdapter); 

    cursor.close(); <======== 
} 

커서를 너무 일찍 닫고 있습니다. 여기를 닫으면 어댑터의 결과가 0이됩니다. CursorAdapter는 완료 될 때 또는 커서를 새로운 커서로 변경할 때 커서를 닫는 것을 처리합니다.

희망이 도움이됩니다.

+0

오, 이런 ... 이제 다른 이유로 충돌하고 있지만, 적어도 나는 더 이상 붙어 있지 않습니다! 고마워요! 이제 새로운 문제를 해결하러 가자 ... : D – Deses

관련 문제