1

我想知道我使用 ViewBinder 的解决方案是否正确并适合我的问题。

我已经实现了一个 ContentProvider。startactivity 扩展了 ListActivity 并使用我的 contentprovider 显示来自 sqliteDB 的所有条目。单击列表视图中的一个条目会启动另一个活动,显示有关此条目的详细信息:

    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // Arzneimittel wurde durch Klick ausgewählt --> Detailansicht anzeigen
    Intent intent = new Intent(this, MedikamenteDetailActivity.class);
    intent.putExtra("_ID", id);
    startActivity(intent);
}

这是 Detail-Activity 的布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtV_heading_statusinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading_status_info"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/datalist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <TextView
        android:id="@+id/txtV_PhZnr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_phznr"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtV_Znr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/znr"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtV_SName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/sname"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtV_OName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/oname"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtV_DoLC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/dolc"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

细节 - 活动类:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medikamente_detail);

        _id = String.valueOf(getIntent().getExtras().getLong("_ID"));

        mAdapter = new SimpleCursorAdapter(
                this,               
                android.R.layout.simple_list_item_1, 
                null,
                mFromColumns, 
                new int[]{android.R.id.text1},
                0); 
        mCallbacks = this;

        ListView mListView = (ListView) findViewById(R.id.datalist);
        mAdapter.setViewBinder(new ViewBinder());
        mListView.setAdapter(mAdapter);

        getLoaderManager().initLoader(URL_LOADER, null, mCallbacks);
    }

在活动中,我为 ViewBinder 实现了一个内部类:

private class ViewBinder implements SimpleCursorAdapter.ViewBinder {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(view.getId() == R.id.txtV_PhZnr) {
                    TextView phznr = (TextView)view;
                    phznr.setText(cursor.getString(columnIndex));
                    return true;
            }
            return false;
        }

    }

绑定有效,但数据绑定到列表视图。其他 ViewElement 保持为空。我真的不明白,为什么我设置视图“R.id.txtV_PhZnr”的文本,而该文本出现在 listview-element 中,而 txtV_PhZnr 保持为空。

为什么我需要一个 listView 元素。由于这是一种主从关系,因此只能在详细视图中显示一个数据集。

非常感谢!

4

1 回答 1

0

为什么我需要一个 listView 元素。

不,你不需要一个。您可以从内容提供者加载数据并将其绑定到TextView您定义的 s (带有 ids 的那些txtV_xxx)。在onLoadFinished()方法中执行此操作。

我真的不明白,为什么我设置视图“R.id.txtV_PhZnr”的文本,而该文本出现在 listview-element 中,而 txtV_PhZnr 保持为空。

view传递给setViewValue()方法的参数ViewBinder将仅具有您实例化的视图 ID 数组中的一个 ID SimpleCursorAdapter。在您的情况下,view参数将仅具有 id android.R.id.text1,因为传入的数组仅包含此视图 id。因此,身体if(view.getId() == R.id.txtV_PhZnr) {...}永远不会被执行。因此txtV_PhZnr保持空白。

ViewBinder仅当您需要根据某些条件更改数据与视图的绑定时使用。就您的情况而言,既不需要 aListView也不需要 a ViewBinder

于 2015-11-16T17:43:54.790 回答