2012-10-24 2 views
1

두 개의 개별 데이터베이스 쿼리를 하나의 listView로 결합하는 방법에 대해서는 상당히 혼란 스럽습니다.두 데이터베이스 테이블의 데이터를 하나의 listView로 결합하는 방법

현재 내 목록보기 내 데이터베이스에서 손상된 구성 요소 테이블을 조회하고 특정 위치에 대한 손상된 구성 요소의 목록을 제공하는 다음과 같은 어댑터에 의해 채워집니다

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item: 

    public MyListAdapter(Context context, Cursor cursor) { 
     super(context, R.layout.row_location, cursor); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView text_first_line = (TextView) view.findViewById(R.id.location_row_item_main_text); 
     TextView text_second_line = (TextView) view.findViewById(R.id.location_row_item_secondary_text); 
     ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon); 

     String row_text_component = cursor.getString(cursor.getColumnIndex(RMDbAdapter.COMPONENT)); 
     String row_text_position = ", Position " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.POSITION)); 
     if(row_text_position.equals(", Position Not Applicable")){ 
      row_text_position = ""; 
     } 
     String row_text_action = " - " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.ACTION_REQUIRED)); 

     text_first_line.setText(row_text_component + row_text_position + row_text_action); 
     text_second_line.setText("Dexion Speedlock, S Duty, 3000mm"); 

     String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK)); 
     if (risk.equals("Red Risk")){ 
      flagIcon.setImageResource(R.drawable.red_flag); 
     } 
     else if (risk.equals("Green Risk")){ 
      flagIcon.setImageResource(R.drawable.green_flag); 
     } 
     else if (risk.equals("No Risk")){ 
      flagIcon.setImageResource(R.drawable.note); 
     } 

    } 
} 

내가 전화 할 때이 트리거 다음 활동이 시작될 때 :

private void setAdapter(){ 

    // Get a Cursor for the list items 

    Cursor listComponentCursor = rmDbHelper.fetchDamagedComponentsForLocation(locationId); 
    componentCursorSize = listComponentCursor.getCount(); 
    startManagingCursor(listComponentCursor); 
    // set the custom list adapter  
    setListAdapter(new MyListAdapter(this, listComponentCursor)); 

} 

그래서 나는 또한 별도의 테이블에 두 번째 쿼리 (이 시간 문제 표)를 만들고 손상된 구성 요소 목록에서 목록보기에이를 추가하고 싶습니다.

Listview from multiple tables?을 읽으면서 Join 또는 병합 커서를 사용해야한다고 생각합니다. 그러나, 내가 조사한 바에 따르면, 나는 두 코드를 내 코드에 통합하는 방법을 모른다.

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

답변

2

표시하지 않더라도 커서 어댑터의 newView 메서드를 무시했다고 가정합니다. MergeCursor는 한 쿼리의 결과를 다음 쿼리의 맨 위에 쌓을 것입니다. CursorJoiner는 결과를 나란히 배치합니다. MergeCursor가 필요한 것 같습니다.

두 번째 쿼리의 결과를 첫 번째 쿼리로 연결하려는 경우 두 쿼리를 모두 수행하고 MergeCursor를 만듭니다. 다른 레이아웃이 필요하거나 열 이름이 다른 경우 어댑터의 getItemViewType 메서드를 재정의해야합니다. 그런 다음 newView 메소드에서 유형을 기반으로 다른보기를 확장 할 수 있습니다. bindView에서 유형을 확인하고 올바른 값을 올바른보기에 적용해야합니다.

코드에 대한 임의 팁 몇 :

1)이 ViewHolder 패턴을 사용, 그것은 빠른 방법입니다. 리터럴 문자열이 (이들을 통합 할 것이다 우리는 내가 기회를 얻을)는, NullPointerException에게 빠른 응답 및 팁에 대한 감사를

+0

안녕 toadzky을 던질 수 없기 때문에

2)는 "literal string".equals(variable) 대신 variable.equals("literal string")을하는 것이 좋습니다. 당신이 보는 것은 listView를 채우는 모든 코드입니다 - 나는 newView를 오버라이드하지 않았습니다 (전에 들어 보지 못했습니다). 이 요소에 대해 자세히 설명해 주시겠습니까? 또한 각 테이블마다 다른 열 이름을 사용하여 getItemViewType을 재정의하는 방법에 대한 예가 있습니까? 지금까지 귀하의 도움에 감사드립니다. – Scamparelli

+0

CursorAdapter를 사용하여 재정의해야하는 두 가지 메소드 newView 및 bindView가 있습니다. newView는 뷰를 팽창 시키거나 생성한다. bindView는 뷰에 값을 지정합니다. ResourceCursorAdapter 서브 클래스가 당신을 대신해 처리하는 것처럼 보입니다. 해당 하위 클래스를 사용할 특별한 이유가 있습니까? 그렇지 않다면 CursorAdapter를 확장하십시오. – toadzky

+0

코드가 현재 작동하고 있다면 newView가 필요합니까? – Scamparelli

관련 문제