这里的问题是选择新类型后,用户向下滚动列表,然后向上备份,然后类型返回到更改前的类型。
这个问题可以总结如下:
例如,我有以下类型的列表:
很抱歉,但是我可能应该包括整个实现。在诉诸于删除Spinner并使用其他操作之前,我正在寻找实现中的错误。
每当用户滚动并根据当前选定的项目进行更改时,都会调用setOnItemSelectedListener
这个问题(我认为)。
我尝试了许多不同的解决方案;其中大多数都包含在here中。
但这不会引起我看到的问题,因为代码会查询数据库中的值;因此,如果用户选择新值时数据库中的值已更新,则应将该新值调用到微调器中,但不会。它总是叫旧的。
使用日志记录,我验证了在数据库中正确设置了新值,并在调用update之前和之后进行了记录。
public class TypeFragment extends ListFragment implements AdapterView.OnItemClickListener {
private static final String TAG = "TypeFragment";
private TypeAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_type_list, container, false);
SwipeRefreshLayout refreshLayout = view.findViewById(R.id.swiperefresh);
refreshLayout.setOnRefreshListener(() -> {
if(refreshLayout.isRefreshing()){
refreshLayout.setRefreshing(false);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<dbRun> typesList = ExampleDatabase.getExampleDatabase().getTypeDao().getTypes();
ArrayList<String> typeOptions = new ArrayList<>();
typeOptions.add(getString(R.string.type_x));
typeOptions.add(getString(R.string.type_y));
typeOptions.add(getString(R.string.type_z));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(Objects.requireNonNull(getContext()),
R.layout.support_simple_spinner_dropdown_item, typeOptions);
adapter = new TypeAdapter(typesList, typeOptions, arrayAdapter);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
class TypeAdapter extends BaseAdapter {
List<dbType> typesList;
ArrayList<String> typeOptions;
ArrayAdapter<String> arrayAdapter;
public TypeAdapter(List<dbType> typesList, ArrayList<String> typeOptions, ArrayAdapter<String> arrayAdapter) {
this.typesList = typesList;
this.typeOptions = typeOptions;
this.arrayAdapter = arrayAdapter;
}
public int getCount() {
if (this.typesList != null) {
return this.typesList.size();
}
return 0;
}
public Object getItem(int position) {
return this.typesList.get(position);
}
public long getItemId(int position) {
return this.typesList.get(position).id;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.types_row_layout, parent, false);
}
TextView typeTV = convertView.findViewById(R.id.type_tv_label);
Spinner typeSpinner = convertView.findViewById(R.id.type_spinner);
typeSpinner.setAdapter(this.arrayAdapter);
dbType currentType = this.typesList.get(position);
// here I ideally would set the Type to the one currently in the DB
//typeSpinner.setSelection(arrayAdapter.getPosition(ExampleDatabase.getExampleDatabase().getTypeDao().getType(currentType.id));
// most popular solution from StackOverflow questions:
typeSpinner.setSelected(false);
typeSpinner.setSelection(Adapter.NO_SELECTION, true);
// set a spinner with its' listener on each row in the list
typeSpinner.post(() -> typeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent1, View view, int position1, long id) {
int item = typeSpinner.getSelectedItemPosition();
Log.d(TAG, "called onItemSelected, position " + position1 + " item: " + item);
ExampleDatabase.getExampleDatabase().getTypeDao().updateType(typeSpinner.get(item), currentType.id);
typeSpinner.setSelection(arrayAdapter.getPosition(arrayAdapter.getPosition(ExampleDatabase.getExampleDatabase().getTypeDao().getType(currentType.id)));
}
@Override
public void onNothingSelected(AdapterView<?> parent1) {}
}));
return convertView;
}
}
}
数据库文件:
dbType :
@Entity(tableName="types")
public class dbType {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
public int id;
@ColumnInfo(name = "type")
public String type;
TypeDao :
@Dao
public interface TypeDao {
@Query("SELECT * FROM types;")
List<dbType> getTypes();
@Query("SELECT type FROM types WHERE id = :typeID;")
String getType(int typeID);
@Query("UPDATE types SET type = :typeStr WHERE id = :typeID;")
void updateType(String typeStr, int typeID);
ExampleDatabase (为简洁起见,不包括迁移)
@Database(entities = {dbType.class})
@TypeConverters({dbTypeConverters.class})
public abstract class ExampleDatabase extends RoomDatabase {
public abstract TypeDao getTypeDao()
private static ExampleDatabase INSTANCE;
public static CoriolisDatabase getCoriolisDatabase() {
return INSTANCE;
}
types_row_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/type_tv_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_gravity="center_vertical"
android:text="@string/runs_list_runvolerror_column"
app:layout_constraintStart_toEndOf="@id/netvolume_column"
app:layout_constraintEnd_toStartOf="@id/run_info_btn"/>
<android.support.constraint.Barrier
android:id="@+id/barrier_run_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="netvolume_column, runvolerror_column"/>
<Spinner
android:id="@+id/type_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ellipsize="end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/run_info_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier_run_row" />
</android.support.constraint.ConstraintLayout>
编辑-以下是一些日志记录,用于验证数据库调用是否正常运行,因为在上面的主体中添加代码似乎会使代码的整体结构/格式混乱:
// before update check:
Log.d(TAG, "DB type before updating: type ID is " + currentType.id + ", type is " + ExampleDatabase.getExampleDatabase().getTypeDao().getTypeWithID(currentType.id).type);
// update happens
Log.d(TAG, "DB type after updating: type ID is " + currentType.id + ", type is " + ExampleDatabase.getExampleDatabase().getTypeDao().getTypeWithID(currentType.id).type);
此日志记录表明滚动时类型已更改,并且数据库更新卡住了。
初次滚动时的输出(注意未单击时如何调用,并以相同的值进行更新):
DB type before updating: type ID is 57, type is Type X
DB type after updating: type ID is 57, type is Type X
同样,在点击并更改值为类型Y后,
DB type before updating: type ID is 57, type is Type X
DB type after updating: type ID is 57, type is Type Y
看起来它更新得很好...
然后,再次滚动实际更新它(注意它如何从以前恢复为Type X):
DB type before updating: type ID is 57, type is Type Y
DB type after updating: type ID is 57, type is Type X