[英]android ViewBinder with SimpleCursorAdapter
I would like to know if my solution using a ViewBinder is correct and suitable for my problem. 我想知道我使用ViewBinder的解决方案是否正确并且适合我的问题。
I have implememented a ContentProvider. 我已经实现了ContentProvider。 The startactivity extends ListActivity and displays all the entries from a sqliteDB using my contentprovider.
startactivity扩展了ListActivity并使用我的contentprovider显示了来自sqliteDB的所有条目。 A Click on one of the entries in the listview starts another activity showing detailinformation about this entry:
单击列表视图中的一个条目,将启动另一个活动,显示有关此条目的详细信息:
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);
}
This is the layout of the Detail-Activity: 这是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>
The Detail - Activity Class: 详细信息-活动类别:
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);
}
Within the activity I implemented an inner class for die ViewBinder: 在活动中,我为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;
}
}
The binding works yet the data is bound to the listview. 绑定有效,但数据已绑定到列表视图。 The other ViewElements stay empty.
其他ViewElement保持为空。 I can't really understand, why I set the Text of the view "R.id.txtV_PhZnr" and this text appears in the listview-element while the txtV_PhZnr stays empty.
我真的不明白,为什么我设置视图“ R.id.txtV_PhZnr”的文本,并且此文本出现在listview元素中,而txtV_PhZnr保持为空。
Why do I need a listView-element. 为什么我需要一个listView元素。 As this is a master-detail relationship, only one dataset can be shown in the detail view.
由于这是一个主从关系,因此详细视图中只能显示一个数据集。
Thank you very much! 非常感谢你!
Why do I need a listView-element.
为什么我需要一个listView元素。
No, you do not need one. 不,您不需要一个。 You can just load the data from your content provider and bind it to the
TextView
s that you have defined (the ones with ids txtV_xxx
). 您只需从内容提供者加载数据并将其绑定到您已定义的
TextView
( txtV_xxx
的那些)。 Do this in the onLoadFinished()
method. 在
onLoadFinished()
方法中执行此操作。
I can't really understand, why I set the Text of the view "R.id.txtV_PhZnr" and this text appears in the listview-element while the txtV_PhZnr stays empty.
我真的不明白,为什么我设置视图“ R.id.txtV_PhZnr”的文本,并且此文本出现在listview元素中,而txtV_PhZnr保持为空。
The view
parameter passed to setViewValue()
method of ViewBinder
will only have one of the ids from the array of view ids that you instantiate the SimpleCursorAdapter
with. 的
view
传递给参数setViewValue()
的方法ViewBinder
将只具有与实例化视图id的阵列的ID之一SimpleCursorAdapter
用。 In your case the view
parameter will only have the id android.R.id.text1
since the passed-in array contains only this view id. 在您的情况下,由于传入的数组仅包含此视图ID,因此
view
参数将仅具有ID android.R.id.text1
。 Thus the body of if(view.getId() == R.id.txtV_PhZnr) {...}
will never get executed. 因此,
if(view.getId() == R.id.txtV_PhZnr) {...}
将永远不会执行。 Hence txtV_PhZnr
stays empty. 因此,
txtV_PhZnr
保持为空。
ViewBinder
is used only when you need to alter the binding of data to views based on some condition. 仅当您需要根据某些条件更改数据与视图的绑定时,才使用
ViewBinder
。 As far as your case goes, there is neither a need for a ListView
nor a ViewBinder
. 就您的情况而言,既不需要
ListView
也不需要ViewBinder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.