如何以编程方式在自定义列表视图中进行应立即反映的更改?
How to make changes in a custom listview programmatically that should immediately reflect?
我正在尝试在自定义列表视图中放置一个按钮,该按钮应重置该行中小部件的所有值(例如:取消选中所有单选按钮)。我在 getView()
中使用了类似的东西
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick() {
editor.putBoolean("rb1"+position,false).apply();
editor.putBoolean("rb2"+position,false).apply();
rb1.setChecked(false);
rb2.setChecked(false);
et.setText("");
notifyDataSetChanged();
}
}
但更改不会立即反映出来。当我点击按钮时,单选按钮没有被选中。相反,另一行中的单选按钮有时会被取消选中。但是当我向下滚动并再次回到那个地方时,我看到了变化。谁能说说怎么解决这个问题?
注意:在 getView() 方法中,我还提到应根据 SharedPreferences 值选中或取消选中单选按钮。
编辑
我正在写完整的代码。
数组适配器class
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity activity;
View rowView;
TextView tv;
RadioGroup rg;
RadioButton rb1, rb2;
EditText etQty;
ImageView imageView;
public MyListAdapter(Activity activity, String[] a) {
super(activity, R.layout.activity_row, a);
this.activity = activity;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
if(convertView == null)
rowView = inflater.inflate(R.layout.activity_row, parent, false);
else
rowView = convertView;
tv = rowView.findViewById(R.id.textView);
rg = rowView.findViewById(R.id.radioGroup);
rb1 = rowView.findViewById(R.id.rbFB);
rb2 = rowView.findViewById(R.id.rbCB);
etQty = rowView.findViewById(R.id.etQty);
imageView = rowView.findViewById(R.id.imageView);
imageView.setClickable(true);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notifyDataSetChanged();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",1).apply();
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notifyDataSetChanged();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",2).apply();
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",0).apply();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "dz",0).apply();
rb1.setChecked(false);
rb2.setChecked(false);
etQty.setText("");
notifyDataSetChanged();
}
});
etQty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!charSequence.toString().equals("")) {
Month.editor.putInt(Worker.name + Open.year + Month.month + (position + 1) + "dz", Integer.parseInt(charSequence.toString())).apply();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
tv.setText((position+1)+"/"+Month.month+"/"+"20"+Open.year);
if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "item",0) == 1) {
rb1.setChecked(true);
notifyDataSetChanged();
}
else if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "item",0) == 2) {
rb2.setChecked(true);
notifyDataSetChanged();
}
if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "dz",0) != 0) {
etQty.setText(Integer.toString(Month.settings.getInt(Worker.name + Open.year + Month.month + (position + 1) + "dz", 0)));
notifyDataSetChanged();
}
return rowView;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public String getItem(int position) {
return getItem(position);
}
}
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] a;
if (Month.month == 1 || Month.month == 3 || Month.month == 5 || Month.month == 7 || Month.month == 8 || Month.month == 10 || Month.month == 12)
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
else if (Month.month == 4 || Month.month == 6 || Month.month == 9 || Month.month == 11)
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
else {
if (Open.year % 4 == 0) {
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
}
else {
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
}
}
MyListAdapter adapter = new MyListAdapter(this,a);
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
以及正在 ListView 中膨胀的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="@+id/etQty"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.348"
app:layout_constraintStart_toEndOf="@+id/etQty"
app:layout_constraintTop_toTopOf="@+id/etQty"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/delete" />
<TextView
android:id="@+id/textView"
android:layout_width="110dp"
android:layout_height="40dp"
android:textSize="20sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.053"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.46" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="130dp"
android:layout_height="40dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.074"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.0">
<RadioButton
android:id="@+id/rbFB"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="FB"
android:textSize="20sp" />
<RadioButton
android:id="@+id/rbCB"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:text="CB"
android:textSize="20sp" />
</RadioGroup>
<EditText
android:id="@+id/etQty"
android:layout_width="50dp"
android:layout_height="40dp"
android:ems="10"
android:inputType="phone"
android:textSize="20sp"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.118"
app:layout_constraintStart_toEndOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="@+id/radioGroup"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
ImageView 的目的是取消选中所有单选按钮,并在单击时将 EditText 中的文本设置为“”。
您的 View-Management 有一个普遍的问题。
每当列表需要一个视图来显示下一项时,就会调用 getView(...)
。这意味着,该方法中使用的所有视图都只能在该方法中访问。但是您将它们存储在 class 成员中。这就是导致一个项目的视图导致另一个项目发生变化的行为。因为它是在加载item1的时候初始化的,但是在它的onClick(...)
回调中class成员变量引用了item2,因为getView()
在点击事件触发前被调用了多次。如果您希望您使用的视图都属于同一布局,则必须在 getView(...)
的范围内工作。
我注意到的另一件肯定会导致错误的事情是您对某些适配器方法的实现。具体
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public String getItem(int position) {
return getItem(position);
}
阅读他们的文档,然后相应地实施它们。
getItemViewType()
肯定不应该 return 一个位置。
getViewTypeCount()
应该是 return 不同视图类型的计数,而不是 getCount()
return 的项目计数。
getItem()
被覆盖并称其为 self。因此 getItem()
会 运行 进入无限循环,直到线程因堆栈溢出而崩溃。
这些都是错误执行的。我建议查看 ArrayAdapter 的文档,也许还有一些 exmaple 实现。然后正确地重做你的适配器,你所有的问题也会消失。
请注意,notifyDataSet()
应该通知适配器有关 数据模型 的更改。所以只有当你真的改变了数据时才调用它。更改 RadioButton 的选中-状态不是操纵数据!
您应该将数据模型中的标志设置为 true/false,然后将更改通知适配器。这将导致调用 getView(...)
,然后根据您的数据模型设置 RadioButton
enabled/disabled。
我正在尝试在自定义列表视图中放置一个按钮,该按钮应重置该行中小部件的所有值(例如:取消选中所有单选按钮)。我在 getView()
中使用了类似的东西btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick() {
editor.putBoolean("rb1"+position,false).apply();
editor.putBoolean("rb2"+position,false).apply();
rb1.setChecked(false);
rb2.setChecked(false);
et.setText("");
notifyDataSetChanged();
}
}
但更改不会立即反映出来。当我点击按钮时,单选按钮没有被选中。相反,另一行中的单选按钮有时会被取消选中。但是当我向下滚动并再次回到那个地方时,我看到了变化。谁能说说怎么解决这个问题?
注意:在 getView() 方法中,我还提到应根据 SharedPreferences 值选中或取消选中单选按钮。
编辑
我正在写完整的代码。
数组适配器class
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity activity;
View rowView;
TextView tv;
RadioGroup rg;
RadioButton rb1, rb2;
EditText etQty;
ImageView imageView;
public MyListAdapter(Activity activity, String[] a) {
super(activity, R.layout.activity_row, a);
this.activity = activity;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
if(convertView == null)
rowView = inflater.inflate(R.layout.activity_row, parent, false);
else
rowView = convertView;
tv = rowView.findViewById(R.id.textView);
rg = rowView.findViewById(R.id.radioGroup);
rb1 = rowView.findViewById(R.id.rbFB);
rb2 = rowView.findViewById(R.id.rbCB);
etQty = rowView.findViewById(R.id.etQty);
imageView = rowView.findViewById(R.id.imageView);
imageView.setClickable(true);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notifyDataSetChanged();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",1).apply();
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notifyDataSetChanged();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",2).apply();
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "item",0).apply();
Month.editor.putInt(Worker.name + Open.year + Month.month + (position+1) + "dz",0).apply();
rb1.setChecked(false);
rb2.setChecked(false);
etQty.setText("");
notifyDataSetChanged();
}
});
etQty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!charSequence.toString().equals("")) {
Month.editor.putInt(Worker.name + Open.year + Month.month + (position + 1) + "dz", Integer.parseInt(charSequence.toString())).apply();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
tv.setText((position+1)+"/"+Month.month+"/"+"20"+Open.year);
if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "item",0) == 1) {
rb1.setChecked(true);
notifyDataSetChanged();
}
else if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "item",0) == 2) {
rb2.setChecked(true);
notifyDataSetChanged();
}
if (Month.settings.getInt(Worker.name + Open.year + Month.month + (position+1) + "dz",0) != 0) {
etQty.setText(Integer.toString(Month.settings.getInt(Worker.name + Open.year + Month.month + (position + 1) + "dz", 0)));
notifyDataSetChanged();
}
return rowView;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public String getItem(int position) {
return getItem(position);
}
}
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] a;
if (Month.month == 1 || Month.month == 3 || Month.month == 5 || Month.month == 7 || Month.month == 8 || Month.month == 10 || Month.month == 12)
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
else if (Month.month == 4 || Month.month == 6 || Month.month == 9 || Month.month == 11)
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
else {
if (Open.year % 4 == 0) {
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
}
else {
a = new String[] {"a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"};
}
}
MyListAdapter adapter = new MyListAdapter(this,a);
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
以及正在 ListView 中膨胀的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="@+id/etQty"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.348"
app:layout_constraintStart_toEndOf="@+id/etQty"
app:layout_constraintTop_toTopOf="@+id/etQty"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/delete" />
<TextView
android:id="@+id/textView"
android:layout_width="110dp"
android:layout_height="40dp"
android:textSize="20sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.053"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.46" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="130dp"
android:layout_height="40dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.074"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.0">
<RadioButton
android:id="@+id/rbFB"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="FB"
android:textSize="20sp" />
<RadioButton
android:id="@+id/rbCB"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:text="CB"
android:textSize="20sp" />
</RadioGroup>
<EditText
android:id="@+id/etQty"
android:layout_width="50dp"
android:layout_height="40dp"
android:ems="10"
android:inputType="phone"
android:textSize="20sp"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.118"
app:layout_constraintStart_toEndOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="@+id/radioGroup"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
ImageView 的目的是取消选中所有单选按钮,并在单击时将 EditText 中的文本设置为“”。
您的 View-Management 有一个普遍的问题。
每当列表需要一个视图来显示下一项时,就会调用 getView(...)
。这意味着,该方法中使用的所有视图都只能在该方法中访问。但是您将它们存储在 class 成员中。这就是导致一个项目的视图导致另一个项目发生变化的行为。因为它是在加载item1的时候初始化的,但是在它的onClick(...)
回调中class成员变量引用了item2,因为getView()
在点击事件触发前被调用了多次。如果您希望您使用的视图都属于同一布局,则必须在 getView(...)
的范围内工作。
我注意到的另一件肯定会导致错误的事情是您对某些适配器方法的实现。具体
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public String getItem(int position) {
return getItem(position);
}
阅读他们的文档,然后相应地实施它们。
getItemViewType()
肯定不应该 return 一个位置。
getViewTypeCount()
应该是 return 不同视图类型的计数,而不是 getCount()
return 的项目计数。
getItem()
被覆盖并称其为 self。因此 getItem()
会 运行 进入无限循环,直到线程因堆栈溢出而崩溃。
这些都是错误执行的。我建议查看 ArrayAdapter 的文档,也许还有一些 exmaple 实现。然后正确地重做你的适配器,你所有的问题也会消失。
请注意,notifyDataSet()
应该通知适配器有关 数据模型 的更改。所以只有当你真的改变了数据时才调用它。更改 RadioButton 的选中-状态不是操纵数据!
您应该将数据模型中的标志设置为 true/false,然后将更改通知适配器。这将导致调用 getView(...)
,然后根据您的数据模型设置 RadioButton
enabled/disabled。