- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为给定的项目列表实现选择 Activity 。每个项目都是可检查的,所以我有一个带有 TextView 和 CheckBox 的项目。我实现了一个用于显示所有选项的 ListView 和一个用于仅显示“前十名”选项的微调器,作为同一列表的子集。现在我在 ListView 和 Spinner 中显示所有项目。
我希望当用户在 Spinner 中选择一个项目时更新 ListView 中的项目(注意:反向路径工作正常,因为 Spinner 每次下拉时都会获取更新的 ArrayList) .我尝试为我的 Spinner 实现 setOnItemSelectedListener,并在 Listener 中为我的 ListViewAdapter 调用 notifyOnDataSetChanged()。但是 Listener 仅在崩溃时被调用,我收到一条奇怪的(可能不相关的)警告消息。
Spinner 的 onItemSelectedListener 仅在 Spinner 折叠时运行。但是 notifyOnDataSetChanged() 似乎忽略了项目的检查状态作为更改。我如何才能在每次检查项目时运行第一个选项并让 ListAdapter 正确接收更改?
这是 Activity.java 代码:
public class TriageReasonActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triage_reason);
final String[] select_qualification = {
"Select Qualification", "10th / Below", "12th", "Diploma", "UG",
"PG", "Phd"};
Spinner spinner = (Spinner) findViewById(R.id.top_reasons_spinner);
ListView symptoms_list = (ListView) findViewById(R.id.view_list_symptoms);
ArrayList<Symptoms> listVOs = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
Symptoms reason = new Symptoms();
reason.setTitle(select_qualification[i]);
reason.setSelected(false);
listVOs.add(reason);
}
SymptomsListAdapter mListAdapter = new SymptomsListAdapter(this, 0,
listVOs);
SymptomsSpinnerAdapter mSpinnerAdapter = new SymptomsSpinnerAdapter(this, 0,
listVOs);
symptoms_list.setAdapter(mListAdapter);
spinner.setAdapter(mSpinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.i("Item selected", "but not cahnged");
symptoms_list.invalidateViews();
mListAdapter.notifyDataSetInvalidated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
Log.i("Not item selected", "but actually it did");
}
});
}
SpinnerCustom 适配器代码:
public class SymptomsSpinnerAdapter extends ArrayAdapter<Symptoms>{
private Context mContext;
private ArrayList<Symptoms> listState;
private SymptomsSpinnerAdapter myAdapter;
private boolean isFromView = false;
/*@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//mNotifyOnChange = true;
}*/
public SymptomsSpinnerAdapter(Context context, int resource, List<Symptoms> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<Symptoms>) objects;
this.myAdapter = this;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.item_reasons, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
@Override
public int getCount() {
return listState.size();
}
@Override
public Symptoms getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return listState.get(position-1);
}
}
@Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
这是(几乎相同的)ListAdapter:
public class SymptomsListAdapter extends BaseAdapter implements ListAdapter {
private Context mContext;
private ArrayList<Symptoms> listState;
private boolean isFromView = false;
public SymptomsListAdapter(Context context, int resource, List<Symptoms> objects) {
this.mContext = context;
this.listState = (ArrayList<Symptoms>) objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.item_reasons, null);
holder = new SymptomsListAdapter.ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (SymptomsListAdapter.ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
@Override
public int getCount() {
return listState.size();
}
@Override
public Symptoms getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return listState.get(position-1);
}
}
@Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
public TextView mTextView;
public CheckBox mCheckBox;
}
}
这是我收到的警告:
W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
编辑:添加布局和模型类以防它们可能导致问题: Activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="demo.hb.activity.visit.TriageReasonActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textFontWeight="6dp"
android:textSize="30sp"
android:layout_margin="20dp"
android:textAlignment="center"
android:textColor="#000000"
android:text="What is the reason for your visit?" />
<Spinner
android:id="@+id/top_reasons_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="end">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_list_symptoms"
android:layout_above="@+id/next_btn"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="text"
android:textAlignment="gravity" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true" />
</RelativeLayout>
模型类:
public class Symptoms {
private String title;
private boolean selected;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
最佳答案
没有任何变化的原因是您还没有实现处理数据集变化的方法。您需要处理数据在适配器中的重新加载方式:
public class SymptomsListAdapter extends BaseAdapter implements ListAdapter {
...
public void refreshData(ArrayList<Symptoms> objects){
this.listState = (ArrayList<Symptoms>) objects;
notifyDataSetChanged();
}
...
}
这link很好地解释了 notifyDataSetInvalidated()
的工作原理(或者在您的情况下,为什么它不起作用)。
关于java - Android:Spinner 和 ListView 具有相同的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57433568/
将一个数组列表分配给另一个数组列表与在两个数组列表之间使用 addAll 方法有什么区别? 1 > arrayList = arrayList;//应该将后面的arrayList的值分配给第一个。 2
所以我在将 ArrayList 添加到我的 ArrayList 时遇到了一些问题。将其想象成一张 table 。 下面是一些示例代码: ArrayList currentRow = new Arra
我一直在尝试转换 ArrayList> to ArrayList> 的字符串 这是我尝试构建的代码。 public void convertString (ArrayList> templist) {
我有一个 ArrayList (alal) 和一个 ArrayList(al) 的 ArrayList。我想将 al 插入 alal,但我希望 alal.get(0) 包含 al 拥有的所有内容以及添
很抱歉标题太长,如果您能想到更好的标题,请告诉我! 我正在做的是尝试创建一个 ArrayList 的 ArrayList 并将 ArrayList 逐个添加到其中。两个AL>我所拥有的称为三角形和正方
我有带有值的 mList2。存在具有相同 id 的值。如何获取具有相同 id 的对象分组的 List 或 ArrayList 并将其添加到 ArrayList>? List mList2 = list
我正在创建一个 ArrayList的 ArrayList并添加 ArrayLists给它。但每次我对 ArrayList 进行更改时, 它反射(reflect)在 ArrayList 中. 示例: L
谁能解释一下ArrayList之间的区别是什么? , ArrayList和 ArrayList是什么时候使用它们?它们在实现层面上是相同的还是各自具有不同的含义? 最佳答案 ArrayList 特别是
这个问题在这里已经有了答案: Java generics: List> = new LinkedList>() is prohibited? (3 个答案) 关闭 9 年前。 为什么这段代码可以编译
我的 arraylistS 在覆盖数组列表中的行为类似于同一个实例。 我用其中一个来操作 i=0; manupulate((ArrayList)theCoveringRootArrayList.get
我们遇到这个错误 java.lang.NullPointerException at java.util.ArrayList.(Unknown Source) at de.mystuf
据我了解,ArrayList 类继承其父“List”类的 equals() 函数来查找两个成员对象是否相同。这是否意味着“contains()”线性搜索(使用“equal”)来查找 ArrayList
这个问题已经有答案了: What is the diamond operator in Java? (2 个回答) 已关闭 7 年前。 正如标题所说,在Java中,这两种语句有什么区别吗? 通常我都能
我正在尝试求解帕斯卡三角形。我有两个用 Java 编写的代码片段,第一个创建 inner ArrayList 几次并且对我来说效果很好。 但是在代码的第二个版本中,如果我修改 inner ArrayL
正如标题所示,我有两个 ArrayList。奇怪的是,在一个数组列表上设置一个值会改变另一个数组列表的值。 一些信息:这些是 Entry 类型的 ArrayList,每个列表都包含一个金额和一个值(这
我已经添加了一个项目到列表 a,然后添加了列表 a 到列表 b 并再次做了同样的事情。 我的问题是,如果我打印 b.get(0) 和 b.get(1),我会得到相同的列表,这两个项目都是 “一”和“二
我正在创建一个 ArrayList of ArrayList of ArrayList 的 ArrayList 并按以下方式填充它。它正确地填充它。我已经通过调试和 println 弄清楚了这一点。但
实现可以在 Arraylist 和 Integer 中存储任何级别的 ArrayList 的 ArrayList 的最佳方法是什么。 List> list = ArrayList(); 仅允许列表中最
在下面的示例中,我将如何将 ArrayList al4 的内容与其他 ArrayList 中的任何一个进行比较?以同样的方式,我将 al1 与 al2 进行了比较。 import java.util.
好的,所以我之前发布了一个线程,它回答了我的很多问题并帮助我改进了我的代码,但是,我遇到了另一个问题,我不知道为什么,但我认为也许该副本只是指向原始对象..(尽管我已尽力避免这种情况) 在我的游戏代码
我是一名优秀的程序员,十分优秀!