如何以编程方式在自定义列表视图中进行更改,这些更改应立即反映出来?

如何解决如何以编程方式在自定义列表视图中进行更改,这些更改应立即反映出来?

我试图在自定义列表视图中放置一个按钮,该按钮应重置该行中小部件的所有值(例如:取消选中所有单选按钮)。我在 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值选中或取消选中单选按钮。

编辑

我正在编写完整的代码。

ArrayAdapter类

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 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"};
        else if (Month.month == 4 || Month.month == 6 || Month.month == 9 || Month.month == 11)
            a = new String[] {"a","a"};
        else {
            if (Open.year % 4 == 0) {
                a = new String[] {"a","a"};
            }
            else {
                a = new String[] {"a","a"};
            }
        }
        MyListAdapter adapter = new MyListAdapter(this,a);
        listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
}

以及在ListView中被夸大的Layout

<?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中的文本设置为“”。

解决方法

您的视图管理存在一般性问题。
每当列表需要视图以显示下一项时,就会调用getView(...)。这意味着,只能在此方法内访问此方法内使用的所有视图。但是您将它们存储在班级成员中。这导致一个项目的视图导致另一项目更改的行为。因为它是在加载item1时初始化的,但是在其onClick(...)回调中,类成员变量引用了item2,因为getView()在触发click事件之前被多次调用。如果要使用的视图全部属于同一布局,则必须在getView(...)范围内进行工作。

我注意到肯定会导致错误的另一件事是您对某些Adapters方法的实现。具体

@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的文档,也许还需要一些示例实现。然后正确重做适配器,所有问题也将消失。

请注意,notifyDataSet()应该将数据模型中的更改通知适配器。因此,仅当您实际更改数据时才调用它。更改RadioButton的checked- 状态不会操纵数据!
您应该将数据模型中的标志设置为true / false,然后将有关更改通知适配器。这将导致调用getView(...),然后根据数据模型将RadioButton设置为启用/禁用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-