Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以编程方式在自定义listview中进行应立即反映的更改?_Java_Android_Listview_Android Listview_Custom Arrayadapter - Fatal编程技术网

Java 如何以编程方式在自定义listview中进行应立即反映的更改?

Java 如何以编程方式在自定义listview中进行应立即反映的更改?,java,android,listview,android-listview,custom-arrayadapter,Java,Android,Listview,Android Listview,Custom Arrayadapter,我试图在自定义listview中放置一个按钮,该按钮应重置该行中小部件的所有值(例如:取消选中所有单选按钮)。我在getView()中使用了类似的内容 但这些变化并没有立即反映出来。当我点击按钮时,单选按钮不会被取消选中。相反,另一行的单选按钮有时会被取消选中。但当我向下滚动并再次回到那个地方时,我看到了变化。有谁能说怎么解决这个问题吗 注意:在getView()方法中,我还提到应根据SharedReferences值选中或取消选中单选按钮。 编辑 我正在写完整的代码 阵列自适应类 import

我试图在自定义listview中放置一个按钮,该按钮应重置该行中小部件的所有值(例如:取消选中所有单选按钮)。我在getView()中使用了类似的内容

但这些变化并没有立即反映出来。当我点击按钮时,单选按钮不会被取消选中。相反,另一行的单选按钮有时会被取消选中。但当我向下滚动并再次回到那个地方时,我看到了变化。有谁能说怎么解决这个问题吗

注意:在getView()方法中,我还提到应根据SharedReferences值选中或取消选中单选按钮。

编辑

我正在写完整的代码

阵列自适应类

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);
    }
}
以及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>


图像视图的目的是取消选中所有单选按钮,并在单击时将编辑文本中的文本设置为“”。

您的视图管理存在一般性问题。
每当列表需要一个视图来显示下一项时,就会调用
getView(…)
。这意味着,此方法中使用的所有视图都只能在此方法中访问。但是你把它们存储在班级成员中。这会导致一个项目的视图导致另一个项目更改的行为。因为它是在加载item1时初始化的,但在其
onClick(…)
回调中,类成员变量引用item2,因为在触发click事件之前多次调用了
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()
当然不应该返回位置。
getViewTypeCount()
应该返回不同视图类型的计数,而不是
getCount()
返回的项目计数。
getItem()
被重写并调用它self。因此
getItem()
将运行到无限循环中,直到线程因堆栈溢出而崩溃。
这些都是错误的。我建议查阅ArrayAdapter的文档,也可以查阅一些exmaple实现。然后正确地重做适配器,所有问题也将消失

请注意,
notifyDataSet()
应该通知适配器有关数据模型的更改。因此,只有在您实际更改数据时才调用它。更改单选按钮的选中-状态不是在操作数据

您应该将数据模型中的标志设置为true/false,然后将更改通知适配器。这将导致调用
getView(…)
,然后根据您的数据模型将
单选按钮设置为启用/禁用。

您可以提供适配器吗?你从哪里得到你的位置和你的单选按钮的上下文在这里是相关的。此外,单选按钮是一个单独的小部件。它们将在不通知适配器的情况下更新。适配器只需要在数据集中的某些内容发生更改时得到通知(单选按钮的情况并非如此),因为您不会更改数据集中的值,而是直接将其设置为未选中。我已经编写了完整的代码。如果可能的话,请帮助我@巴斯蒂请检查这个问题@Shaistanaaz,但其他一切似乎都很好。唯一的问题是,当我单击ImageView时,其他小部件的状态不会改变。但当向下滚动并备份ListView时,它会正确更改。当单击ImageView时,有没有办法重新设置listview?适配器的notify方法正是这样做的。它们告诉适配器数据已更改。然后,它将重建ListView中映射到已更改数据的所有行(notifyDataSet告诉适配器刷新所有行)。您的问题源于适配器的错误实现。正如我在回答中所解释的,这就是导致错误行为的原因。正确地实现适配器,您的问题就会得到解决。将变量设置为本地变量可以解决问题。谢谢你,伙计@Basti@SusantaGanguly不客气。我仍然想指出,如果不在实现中解决其他问题,您可能还会遇到更多问题。如果幸运的话,它可以在不修复它们的情况下工作,因为你不会产生一个会导致问题的案例。但您应该始终以干净的实现为目标,而不是只适用于您的场景的实现。
<?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>
@Override
public int getViewTypeCount() {
    return getCount();
}
@Override
public int getItemViewType(int position) {
    return position;
}
@Override
public String getItem(int position) {
    return getItem(position);
}