我使用自定义ListView
填充了CursorAdapter
,该自定义ListItem
读取我的数据库以获取数据。
TextView
包含一个EditTexts
,其中包含上面读取的数据和两个<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/good_name"
android:layout_width="wrap_content"
android:layout_height="@dimen/height_of_list_item"
android:paddingLeft="@dimen/padding"
android:gravity="center"
tools:text="Frozen Chicken"/>
<EditText
android:id="@+id/sold_edit_text"
android:layout_width="wrap_content"
android:layout_height="@dimen/height_of_list_item"
android:paddingLeft="@dimen/padding"
android:gravity="center"
android:inputType="number"
android:hint="@string/hint_remove_quantity"/>
<EditText
android:id="@+id/received_edit_text"
android:layout_width="wrap_content"
android:layout_height="@dimen/height_of_list_item"
android:paddingLeft="@dimen/padding"
android:gravity="center"
android:inputType="number"
android:hint="@string/hint_add_quantity"/>
</LinearLayout>
,我想从中读取数据,并使用其中的值更新数据库。
这是updater_list_item.xml:
// Find the views
ListView list = (ListView) findViewById(R.id.list_updater);
TextView textView = (TextView) findViewById(R.id.updater_empty_view);
// Set the empty view
list.setEmptyView(textView);
// Set the ListView to the custom CursorAdapter
mAdapter = new UpdaterCursorAdapter(this, null);
list.setAdapter(mAdapter);
// the loader requests the data from the database and add it to the adapter
getLoaderManager().initLoader(STORE_LOADER, null, this);
OnCreate中:
public class UpdaterCursorAdapter extends CursorAdapter {
public UpdaterCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = (TextView) view.findViewById(R.id.good_name);
EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text);
EditText receivedEditText = (EditText) view.findViewById(R.id.received_edit_text);
int nameColumnIndex = cursor.getColumnIndex(StoreEntry.COLUMN_GOOD_NAME);
String goodName = cursor.getString(nameColumnIndex);
nameTextView.setText(goodName);
}
}
编辑: 这是自定义CursorAdapter的代码
EditTexts
如何从Item
中的每个ListView
获取两个MenuItem
的数据?!
注意:我希望在点击onItemClickListener
时将数据保存在我的数据库中,以便addTextChangedListener
或 "users": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "!data.exists() || auth.uid === $uid",
// grants read access to any user who is logged in with Facebook
".read": "auth !== null && (auth.provider === 'facebook' || auth.provider === 'google')"
}
}
无法完成工作。
答案 0 :(得分:0)
您只需要将编辑文本添加到适配器,然后添加textWatcher并像往常一样更新数据库。
public class UpdaterCursorAdapter extends CursorAdapter {
public UpdaterCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = (TextView) view.findViewById(R.id.good_name);
int nameColumnIndex = cursor.getColumnIndex(StoreContract.StoreEntry.COLUMN_GOOD_NAME);
String goodName = cursor.getString(nameColumnIndex);
nameTextView.setText(goodName);
EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text);
//Set the soldEditText to whatever column your data is in the database same as text view.
soldEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
//This is where you would save the new text your database just
}
});
}
}