2011-11-03 3 views
1

이 포럼에서 12 가지 솔루션을 읽고 사용해 보았지만 그 중 아무 것도 작동하지 않는 것으로 나타났습니다. 목록에 데이터를 표시 할 수 없습니다. view.findViewById (resourceid)는 매번 bindView에서 null을 반환합니다. 예를 들어 TextView를 사용할 때 t = (TextView) view.findViewById (R.id.leftCell). Logcat에서 오류가없는 것으로 간주하고 뷰 자체가 null이 아니므로 이상한 동작입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?사용자 정의 SimpleCursorAdapter가 화면에 데이터를 표시하지 않습니다.

다음과 같이 레이아웃을 나눠 봤습니다. 각각 ListView를 래핑하는 두 개의 LinearLayouts입니다. database_item.xml 및 list_item.xml에있는 두 목록에 대한 사용자 정의 행 구조를 정의했습니다.

여기 내 코드가 있습니다.

main.xml에 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:id="@+id/topFrame"> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Devices loaded from Database: "></TextView> 
     <TableLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1"> 
      <TableRow> 
       <TextView android:text="Name" 
       android:padding="3dip" 
       android:gravity="center"/> 
       <TextView android:text="Remote Address" 
       android:padding="3dip" 
       android:gravity="center"/> 
       <TextView android:text="Type" 
       android:padding="3dip" 
       android:gravity="center"/> 
      </TableRow> 
     </TableLayout> 
    <ListView android:id="@+id/databaseList" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
    </ListView> 
</LinearLayout> 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1.2" 
    android:id="@+id/bottomFrame"> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Detected Xbee nodes via 'introduce' command:"></TextView> 
     <TableLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1"> 
      <TableRow> 
       <TextView android:text="Address" 
       android:padding="3dip" 
       android:gravity="center"/> 
       <TextView android:text="Type" 
       android:padding="3dip" 
       android:gravity="center"/> 
      </TableRow> 
     </TableLayout> 
    <ListView android:id="@+id/detectedNodesList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
    </ListView> 
</LinearLayout> 

database_item.xml :

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:stretchColumns="1"> 
<TableRow> 
    <CheckBox android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="visible" 
     android:id="@+id/checkBox"></CheckBox> 
    <TextView android:id="@+id/topLeftCell" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:gravity="center"></TextView> 
    <TextView android:id="@+id/topCenterCell" 
     android:padding="3dip" 
     android:gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
    <TextView android:id="@+id/topRightCell" 
     android:gravity="center" 
     android:padding="3dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
</TableRow>  
</TableLayout> 

list_item.xml :

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:stretchColumns="1"> 
<TableRow> 
    <TextView android:id="@+id/leftCell" 
     android:padding="3dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
    <TextView android:id="@+id/rightCell" 
     android:gravity="left" 
     android:padding="3dip" 
     android:text="TESTING" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
</TableRow>  

에서 onCreate에서

관련 코드 :

ListView topList = (ListView) findViewById(R.id.databaseList); 
ListView bottomList = (ListView) findViewById(R.id.detectedNodesList); 
String[] topColumns = new String[] {"name", "my", "type"}; 
    String[] bottomColumns = new String[] {"my","type"}; 

    int[] toTop = new int[] { R.id.topLeftCell, R.id.topCenterCell, R.id.topRightCell}; 
    int[] toBottom = new int[] { R.id.leftCell, R.id.rightCell}; 

    DbCursorAdapter e = new DbCursorAdapter(this, R.layout.database_item, cursor, topColumns, toTop); 
    DbCursorAdapter d = new DbCursorAdapter(this, R.layout.list_item, cursor, bottomColumns, toBottom); 

    topList.setAdapter(e); 
    bottomList.setAdapter(d); 

및 DbCursorAdapter.java :

public class DbCursorAdapter extends SimpleCursorAdapter { 
private Context context; 
private int layout; 
private final String TAG = "DbCursorAdapter"; 
LayoutInflater inflater; 

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

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    View v = inflater.inflate(layout, parent, false); 
    return v; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    Cursor c = cursor; 
    if (layout == R.layout.list_item) { 
     int addressCol = c.getColumnIndex("my"); 
     int typeCol = c.getColumnIndex("type"); 
     int address = c.getInt(addressCol); 
     int type = c.getInt(typeCol); 
     TextView t = (TextView) view.findViewById(R.id.leftCell); 
     TextView t2 = (TextView) view.findViewById(R.id.rightCell); 
     if (t != null) { 
      t.setText(Integer.toString(address)); 
     } 
     if (t2 != null) { 
      t2.setText(Integer.toString(type)); 
     } 
    } 
    if (layout == R.layout.database_item) { 
     int nameCol = c.getColumnIndex("name"); 
     int addressCol = c.getColumnIndex("my"); 
     int typeCol = c.getColumnIndex("type"); 
     String name = c.getString(nameCol); 
     int my = c.getInt(addressCol); 
     int type = c.getInt(typeCol); 
     CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox); 
     checkBox.setVisibility(CheckBox.VISIBLE); 
     TextView t = (TextView) view.findViewById(R.id.topLeftCell); 
     TextView t2 = (TextView) view.findViewById(R.id.topCenterCell); 
     TextView t3 = (TextView) view.findViewById(R.id.topRightCell); 
     if (t != null) { 
      t.setText(name); 
     } 
     if (t2 != null) { 
      t2.setText(Integer.toString(my)); 
     } 
     if (t3 != null) { 
      t3.setText(Integer.toString(type)); 
     } 
    } 
} 
+0

이 도움이 될 수 있습니다 : 이 http://stackoverflow.com/questions/1832290/android-id-naming-convention-lower-case-with-underscore-vs-camel-case – lyschoening

답변

0

해결. 문제는 main.xml에 될 듯 :

<TableLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> 
     <TableRow> 
      <TextView android:text="Name" 
      android:padding="3dip" 
      android:gravity="center"/> 
      <TextView android:text="Remote Address" 
      android:padding="3dip" 
      android:gravity="center"/> 
      <TextView android:text="Type" 
      android:padding="3dip" 
      android:gravity="center"/> 
     </TableRow> 
    </TableLayout> 

은 어떻게 든 상기 상부 프레임과 상기 하부 프레임 모두 그 아래에 있어야했다 ListView에 덮여. TableLayout을 제거하면 두 목록 모두 잘 보입니다.

아무도 이유를 설명 할 수 있습니까?

0
if (layout == R.layout.list_item) 

올바른 방법은 리소스 ID에 액세스 할이 당신에게 돌아갑니다

int list_item_id = context.getResources.getIdentifier(list_item,"layout", getPackageName()); 
if (layout == list_item_id) 

입니다 R.layout.list_item의 자원 ID. 나는 당신에게 알려주고 싶었다. 그러나 이것이 정확한 문제인지 나는 모른다.

+0

문서는 이것에 대해 말한다 귀하의 제안 된 방법 : "참고 :이 기능의 사용은 권장하지 않습니다. 식별자를 기준으로 리소스를 검색하는 것이 이름보다 훨씬 효율적입니다." 그렇습니다. IF 선택은 그렇지 않으면 작동합니다. 그것이 database_item인지 list_item인지에 따라 올바른 선택을합니다. –

0

야쉬 완트 (Yashwanth)와 마찬가지로 내 직감은 if(layout==R.layout.list_item)과 관련 있다고 말하고 있습니다. 각 목록에 대해 별도의 어댑터를 사용하는 것으로 생각하십니까?

+0

두 개의 개별 어댑터가 옵션입니다. 다음에 시도해 보겠습니다. 두 클래스 모두를 처리하는 데는 단일 클래스를 사용하는 것이 더 간단 할 것입니다. –

+0

확인 - 이제 두 개의 별도 어댑터를 사용합니다. TextView는 더 이상 null이 아닙니다. 그러나 활동은 여전히 ​​목록에 데이터를 표시하지 않습니다. 두 개의 TextViews에서 t.setText ("test")를 수행하여 화면을 채 웁니다. logcat의 디버깅은 매번 호출되는 setText를 보여줍니다. 그러나 아무것도 목록에 표시되지 않습니다! –

+0

목록에 데이터가 표시되지 않거나 목록 중 하나에 데이터가 표시되지 않습니까? –

관련 문제