1

I've read and tried a dozen solutions from this forum but none of them seem to work - I cannot display data on my lists - view.findViewById(resourceid) returns null each time in bindView - for example when using TextView t = (TextView) view.findViewById(R.id.leftCell). It is strange behaviour considering i have no errors in Logcat, and the view itself isn't null. What am I doing wrong?

I've divided my layout as follows: two LinearLayouts each wrapping a ListView. I've defined a custom row structure for both lists, found in database_item.xml and list_item.xml.

Here's my code.

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>     

Relevant code from 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);

and 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));
        }
    } 
}
Petteri Pertola
  • 281
  • 2
  • 6
  • 23
  • This may help: http://stackoverflow.com/questions/1832290/android-id-naming-convention-lower-case-with-underscore-vs-camel-case – lyschoening Nov 03 '11 at 19:03

3 Answers3

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

The right way to access a resource id is

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

this will return you the resource id of R.layout.list_item . I wanted to just let you know, but i don't know if this is the exact problem.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • The documentation says this about your suggested method: "Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name." That said - the IF selection does work otherwise. Depending on whether it's a database_item or a list_item it goes through the correct selection. – Petteri Pertola Nov 03 '11 at 19:10
0

Much like Yashwanth, my intuition is telling me something is up with if(layout==R.layout.list_item). Have you considered just using separate adapters for each list?

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • Two separate adapters is an option - I will try that next. It would just be simpler to use a single class to handle both (relatively simple) cases. – Petteri Pertola Nov 03 '11 at 19:12
  • ok - now using two separate adapters. The TextView is no longer null. However, the activity still does not display data on the list. I tried to simply do t.setText("test") in both TextViews which should populate the screen. Debugging in logcat shows the setText being called each time. Yet nothing shows on the list! – Petteri Pertola Nov 03 '11 at 19:28
  • It doesn't display data in either of the lists, or just one of them? – Kurtis Nusbaum Nov 03 '11 at 19:51
  • Solved. The problem was in main.xml. Removing the TableLayout from above ListView displays both lists just fine. Would anybody be able to explain why? – Petteri Pertola Nov 03 '11 at 19:59
0

Solved. The problem seemed to be in 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>

Somehow covered the ListView that was supposed to be below it for both the top frame and the bottom frame. Removing TableLayout shows both lists just fine.

Would anybody be able to explain why?

Petteri Pertola
  • 281
  • 2
  • 6
  • 23