【问题标题】:Custom SimpleCursorAdapter not displaying data on screen自定义 SimpleCursorAdapter 不在屏幕上显示数据
【发布时间】:2011-11-03 18:47:37
【问题描述】:

我已经阅读并尝试了该论坛的十几种解决方案,但它们似乎都不起作用 - 我无法在我的列表中显示数据 - view.findViewById(resourceid) 在 bindView 中每次都返回 null - 例如使用 TextView t = (TextView) view.findViewById(R.id.leftCell)。考虑到我在 Logcat 中没有错误,并且视图本身不为空,这是一种奇怪的行为。我做错了什么?

我将布局划分如下:两个 LinearLayout,每个包含一个 ListView。我已经为两个列表定义了一个自定义行结构,可以在 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));
        }
    } 
}

【问题讨论】:

标签: android simplecursoradapter


【解决方案1】:
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。我只是想让你知道,但我不知道这是否是确切的问题。

【讨论】:

  • 文档说明了您建议的方法:“注意:不鼓励使用此功能。按标识符检索资源比按名称检索资源效率更高。”也就是说 - IF 选择确实有效。根据它是 database_item 还是 list_item,它会通过正确的选择。
【解决方案2】:

很像 Yashwanth,我的直觉告诉我 if(layout==R.layout.list_item) 出了点问题。您是否考虑过为每个列表使用单独的适配器?

【讨论】:

  • 两个单独的适配器是一个选项——我接下来会尝试。使用单个类来处理两种(相对简单的)情况会更简单。
  • 好的 - 现在使用两个单独的适配器。 TextView 不再为空。但是,活动仍然没有在列表中显示数据。我试图在两个应该填充屏幕的 TextView 中简单地执行 t.setText("test") 。在 logcat 中调试会显示每次调用的 setText。然而列表中没有任何显示!
  • 这两个列表都不显示数据,还是只显示其中一个?
  • 已解决。问题出在 main.xml 中。从 ListView 上方删除 TableLayout 可以很好地显示两个列表。谁能解释一下原因?
【解决方案3】:

解决了。问题似乎出在 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 可以很好地显示两个列表。

谁能解释一下原因?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多