2012-01-15 2 views
0

사용자 정의 SimpleCursorAdapter를 사용하여 DB 커서의 데이터를 사용하여 격자보기를 채우려고합니다. 커서에 데이터가 있지만 GridView에 아무 것도 표시되지 않고 getView() 메서드도 호출되지 않습니다.사용자 정의 SimpleCursorAdapter 문제

아무도 도와 드릴 수 있습니까? getView()가 호출되지 않는 이유는 무엇입니까?

감사

활동

dbAdapter = new DBAdapter(this);  
dbAdapter.open(); 

Cursor c; 
c = dbAdapter.fetchPCList(); 
startManagingCursor(c);  

String[] from = new String[] {}; 
int[] to = new int[] {}; 

GridView gridview = (GridView) findViewById(R.id.gridview); 
gridview.setAdapter(new PCIconAdapter(this, R.layout.pc_icon, c, from, to)); 

c.close(); 
dbAdapter.close(); 

어댑터

public class PCIconAdapter extends SimpleCursorAdapter { 
    private final Context mContext; 
    private final int mLayout; 
    private final Cursor mCursor; 
    private final int mPCIDIndex; 
    private final int mClassNameIndex; 
    private final LayoutInflater mLayoutInflater; 

    private final class ViewHolder { 
     public TextView pc_id_view; 
     public TextView clas_name_view; 
    } 

    public PCIconAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 

     this.mContext = context; 
     this.mLayout = layout; 
     this.mCursor = c; 
     this.mPCIDIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_LM_ID); 
     this.mClassNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_CLAS_NAME); 
     this.mLayoutInflater = LayoutInflater.from(mContext); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     if (mCursor.moveToPosition(position)) { 
      ViewHolder viewHolder; 

      if (convertView == null) { 
       convertView = mLayoutInflater.inflate(mLayout, null); 

       viewHolder = new ViewHolder(); 
       viewHolder.pc_id_view = (TextView) convertView.findViewById(R.id.pc_id); 
       viewHolder.clas_name_view = (TextView) convertView.findViewById(R.id.clas_name); 

       convertView.setTag(viewHolder); 
      } 
      else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
      } 

      String pc_id = mCursor.getString(mPCIDIndex); 
      String clas_name = mCursor.getString(mClassNameIndex); 

      viewHolder.pc_id_view.setText(pc_id); 
      viewHolder.clas_name_view.setText(clas_name); 
     } 

     return convertView; 
    } 
} 
+0

결코 손으로 무시 된 메서드를 추가하면 문제가 발생할 수 있습니다. – JoxTraex

+0

@ JoxTrax : 무슨 소리 야? – jul

답변

0

에 그건 아마도 이유는 못하고있다 콜백 ...

0

CursorAdapter가의 getView 방법이 없기 때문에. newView 메소드가 있습니다. 그렇게 SimpleCursorAdapter를 확장하고

public class ContactListCursorAdapter extends SimpleCursorAdapter implements Filterable { 

    private Context context; 

    private int layout; 

    public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.context = context; 
     this.layout = layout; 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 

     Cursor c = getCursor(); 

     final LayoutInflater inflater = LayoutInflater.from(context); 
     View v = inflater.inflate(layout, parent, false); 

     int nameCol = c.getColumnIndex(People.NAME); 

     String name = c.getString(nameCol); 

     /** 
     * Next set the name of the entry. 
     */  
     TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
     if (name_text != null) { 
      name_text.setText(name); 
     } 

     return v; 
    } 

    @Override 
    public void bindView(View v, Context context, Cursor c) { 

     int nameCol = c.getColumnIndex(People.NAME); 

     String name = c.getString(nameCol); 

     /** 
     * Next set the name of the entry. 
     */  
     TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
     if (name_text != null) { 
      name_text.setText(name); 
     } 
    } 

    @Override 
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 
     if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); } 

     StringBuilder buffer = null; 
     String[] args = null; 
     if (constraint != null) { 
      buffer = new StringBuilder(); 
      buffer.append("UPPER("); 
      buffer.append(People.NAME); 
      buffer.append(") GLOB ?"); 
      args = new String[] { constraint.toString().toUpperCase() + "*" }; 
     } 

     return context.getContentResolver().query(People.CONTENT_URI, null, 
       buffer == null ? null : buffer.toString(), args, People.NAME + " ASC"); 
    } 
} 
+0

이 질문에 대한 답변 : http://stackoverflow.com/questions/5183813/android-issue-with-newview-and-bindview-in-custom-simplecursoradapter, Romain Guy는 "overriding getView()는 완벽하게 유효합니다 ". 어쨌든 newView() 및 bindView()를 사용해 보겠습니다. 감사. – jul

0

귀하의 열 이름의 배열 및 뷰 배열이 비어있는 것처럼 newWiev 및 다른 방법을 재정의합니다. 따라서 뷰 매핑에 대한 열 이름은 절대로 발생하지 않습니다. 내가

c.close(); 
dbAdapter.close(); 

작동 ...

을 제거 그래서이 커서를 닫습니다? 하죠 경우 경우의 getView

1

나는 똑같은 문제가 있었다. ArrayAdapter를 사용하고 SimpleCursorAdapter로 변경했지만 화면에 아무 것도 표시하지 않습니다.

나는
c.close(); 
dbAdapter.close(); 

지금 그 작업 벌금을 제거했습니다.