android - 动态创建自定义 ListView 问题

标签 android android-layout listview android-studio android-listview

我们正在开发一个项目,其中有一个调查问卷部分。问题及其选项是动态生成的。为了测试,我们创建了一个主类和一个自定义适配器。在适配器中,我们尝试动态创建单选按钮。 问题是,滚动 ListView 时,选项会重复或合并。为什么会发生这种情况。我已经显示了下面的代码。

主类:

public class MainActivity extends AppCompatActivity {
    CustomAdapter cadp;
    ListView list_questions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list_questions= (ListView) findViewById(R.id.list_question);
        cadp=new CustomAdapter(MainActivity.this);
        list_questions.setAdapter(cadp);


    }

}

自定义适配器:

public class CustomAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    SingleHolder sh;



    public CustomAdapter(Context context) {
        this.context=context;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_choice_layout, null);
            sh = new SingleHolder();
            sh.single_question_name = (TextView) convertView.findViewById(R.id.single_question_name);
            sh.single_radiogroup = (RadioGroup) convertView.findViewById(R.id.single_radiogroup);
            convertView.setTag(sh);

        }
        else
        {
            sh = (SingleHolder) convertView.getTag();
        }
        sh.single_question_name.setText("question name");

        for (int j = 0; j < 3; j++)
        {
            RadioButton rb = new RadioButton(convertView.getContext());
            rb.setText("value");
            sh.single_radiogroup.addView(rb);
        }


        return convertView;
    }
    private   class SingleHolder
    {
        TextView single_question_name;
        RadioGroup single_radiogroup;
    }
}

单个列表元素布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_question_name"
        />
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_radiogroup"
        android:orientation="vertical"></RadioGroup>
</LinearLayout>

最佳答案

The issue is that on scrolling the listview the options get duplicated or get merged

这里:

for (int j = 0; j < 3; j++)
  {
    RadioButton rb = new RadioButton(convertView.getContext());
    rb.setText("value");
    sh.single_radiogroup.addView(rb);
  }

这些行会导致问题,因为当 convertView 不等于 null 时,意味着 single_radiogroup 中的 RadioButton 已经可用,无需再次添加。

convertView为null时添加RadioButton:

 if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_choice_layout, null);
            sh = new SingleHolder();
           ....
         // add RadioButton here
        }
        else
        {
            sh = (SingleHolder) convertView.getTag();
        }

编辑:

根据您的要求,在添加新 View 之前从 RadioGroup 中删除旧 View :

sh.single_radiogroup.clearCheck();
sh.single_radiogroup.removeAllViews();
for (int j = 0; j < 3; j++)
   {
      RadioButton rb = new RadioButton(convertView.getContext());
      rb.setText("value");
      sh.single_radiogroup.addView(rb);
   }

关于android - 动态创建自定义 ListView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715427/

相关文章:

android - RecyclerView 之间的距离太远

android - 如何以编程方式验证 Play 商店上的应用程序版本?

android-layout - Android 开发 - 为多种屏幕尺寸设计用户界面

android - 布局已经存在使用不同的限定符

c++ - 使用 MoveWindow 或 SetWindowPos 时,编辑控件/组合框的窗口大小未正确调整

php - 如何使用 PHP 从 Mysql 数据库获取结果创建多项选择 Android ListView ?

android - 刷新android时listview数据消失

java - 不使用 int onebyte 将字符串写入 OutputStream

android - 在 Android 中同时使用 GPS 和 Network Provider

android - 如何在android中解决这个图像缩放问题?