【问题标题】:Custom Adapter scroll do not save state of group button of Listview in Android (Status does not save when I scroll down.)自定义适配器滚动不保存Android中Listview的组按钮状态(向下滚动时状态不保存。)
【发布时间】:2018-05-28 02:14:25
【问题描述】:

当我向下滚动时,状态不会保存。

public class OnooltActivity2 extends AppCompatActivity implements View.OnClickListener {

    ListView simpleList;
    String[] questions;
     ArrayList<String> array = new ArrayList<String>();
     ArrayList<String> onoolts = new ArrayList<String>();
    ArrayList<String> passArray = new ArrayList<String>();
    FirebaseDatabase database;

    CustomAdapter customAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onoolt_activity_2);
        simpleList = (ListView) findViewById(R.id.simpleListView);
        Parcelable state = simpleList.onSaveInstanceState();
        submit = (Button) findViewById(R.id.submit);
        customAdapter = new CustomAdapter(getApplicationContext(), questions, array);
        simpleList.setAdapter(customAdapter);
        getDavaa();// this get data. In function array.add(onoolt.getBaruunTal()); customAdapter.notifyDataSetChanged();
        simpleList.onRestoreInstanceState(state);
..........

自定义

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;


public class CustomAdapter extends BaseAdapter {
    Context context;
    String[] questionsList;
    LayoutInflater inflter;
    public static ArrayList<String> selectedAnswers;

    public CustomAdapter(Context applicationContext, String[] questionsList,  ArrayList<String> array) {
        this.context = context;
        this.questionsList = questionsList;

        selectedAnswers = new ArrayList<>();
        for (int i = 0; i < questionsList.length; i++) {
            selectedAnswers.add(array.get(i));
        }
        inflter = (LayoutInflater.from(applicationContext));

    }

    @Override
    public int getCount() {
        return questionsList.length;
    }

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

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.radio_group_item, null);
        TextView question = (TextView) view.findViewById(R.id.question);
        RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
        RadioButton no = (RadioButton) view.findViewById(R.id.no);
        yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked)
                    selectedAnswers.set(i, "Yes");
            }
        });
        no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    selectedAnswers.set(i, "No");

            }
        });
       // yes.setText("  ");
      //  no.setText("  ");
        question.setText(questionsList[i]);
        return view;
    }
}

主要问题是: 主要问题是: 主要问题是: 主要问题是: 主要问题是: 主要问题是: 向下滚动时不保存状态。帮我。 首先,我检查了每个项目的列表。然后我向下滚动。然后向上滚动。但没有选择。 此图错误->

【问题讨论】:

    标签: android listview adapter android-adapter custom-adapter


    【解决方案1】:

    您必须在 getView() 中设置复合按钮的状态 以便回收视图明智地获取状态索引,例如:

    yes.setChecked(true)
    

    【讨论】:

      【解决方案2】:

      您没有设置每个答案的状态。在getView 方法中设置RadioButton 状态(选择/未选择)。

      @Override
      public View getView(final int i, View view, ViewGroup viewGroup) {
          view = inflter.inflate(R.layout.radio_group_item, null);
          TextView question = (TextView) view.findViewById(R.id.question);
          RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
          RadioButton no = (RadioButton) view.findViewById(R.id.no);
      
          // setting previous sate
          yes.setSelected(false);
          no.setSelected(true);
          if(selectedAnswers.get(i).equals("Yes")){
             yes.setSelected(true);
          }else if(selectedAnswers.get(i).equals("No")){
             no.setSelected(true);
          }
      
          yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      
                  if (isChecked)
                      selectedAnswers.set(i, "Yes");
              }
          });
          no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked)
                      selectedAnswers.set(i, "No");
      
              }
          });
         // yes.setText("  ");
        //  no.setText("  ");
          question.setText(questionsList[i]);
          return view;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多