2011-11-03 119 views
1

數據我已經閱讀並試圖從這個論壇十幾個解決方案,但沒有人似乎工作 - 我不能在我的列表中顯示的數據 - view.findViewById(RESOURCEID)在bindView空每次返回 - 例如,當使用TextView t =(TextView)view.findViewById(R.id.leftCell)時。這是奇怪的行爲,考慮到我在Logcat中沒有錯誤,並且視圖本身不爲空。我究竟做錯了什麼? 2個LinearLayouts每個纏繞一個ListView:定製SimpleCursorAdapter不顯示在屏幕上

如下我將我的佈局。我已經爲這兩個列表定義了一個自定義行結構,可以在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

文檔說這有關您建議的方法: 「注意:不鼓勵使用此功能。通過標識符而不是名稱檢索資源效率更高。」 這就是說 - 中頻選擇否則工作。取決於它是一個database_item還是一個list_item,它將通過正確的選擇。 –

0

就像Yashwanth,我的直覺告訴我什麼是了if(layout==R.layout.list_item)。您是否考慮過爲每個列表使用單獨的適配器?

+0

兩個獨立的適配器是一個選項 - 我會嘗試下一步。使用單個類來處理這兩個(相對簡單的)情況會更簡單。 –

+0

好 - 現在使用兩個獨立的適配器。 TextView不再爲空。但是,該活動仍然不會在列表中顯示數據。我試圖在兩個應該填充屏幕的TextView中做t.setText(「test」)。在logcat中調試顯示每次調用setText。然而沒有任何東西顯示在名單上 –

+0

它不顯示任何一個列表中的數據,或者只是其中的一個? –