Android 从多个自定义布局查找视图

Android 从多个自定义布局查找视图,android,Android,我根据数据行生成5个单选按钮。 现在我想选择单选按钮,如何找到它们 也许我必须通过添加数组来重新组织生成代码,但是如何 我的想法是做这样的东西 TextView a[]=new TextView[1]; 但这是行不通的 View linearLayout[] = LayoutInflater.from(this).inflate(R.layout.layout_rating_question, null)[i]; 我应该如何编码?下面是我的视图生成代码 public void questio

我根据数据行生成5个单选按钮。 现在我想选择单选按钮,如何找到它们

也许我必须通过添加数组来重新组织生成代码,但是如何

我的想法是做这样的东西

TextView a[]=new TextView[1];
但这是行不通的

View linearLayout[] = LayoutInflater.from(this).inflate(R.layout.layout_rating_question, null)[i];
我应该如何编码?下面是我的视图生成代码

public void questionArray(){
    rows = new ArrayList<>();
    CSVReader csvReader = new CSVReader(MainActivity.this, "q32.csv");
    try {
        rows = csvReader.readCSV();
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < rows.size(); i++) {

        answer.add(0);

        View linearLayout = LayoutInflater.from(this).inflate(R.layout.layout_rating_question, null);

        if ((i % 2) == 0) {
            linearLayout.setBackgroundColor(Color.parseColor("#EFECCB"));
        }

        TextView tv = linearLayout.findViewById(R.id.textView4);
        TextView tv2 = linearLayout.findViewById(R.id.textView5);

        tv.setText(String.valueOf(rows.get(i)[1]));
        tv2.setText(String.valueOf(rows.get(i)[2]));


        final int finalI = i;

        for (int j =0; j<5; j++){
            int id = getResources().getIdentifier("rate"+j, "id", getApplicationContext().getPackageName());
            final int finalJ = j;
            RadioButton rb = linearLayout.findViewById(id);
            rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        answer.set(finalI, finalJ +1);
                    }
                }
            });
        }
        ll.addView(linearLayout);
    }
}
有什么办法可以做到这一点吗

View linearLayout[] = LayoutInflater.from(this).inflate(R.layout.layout_rating_question, null)[i];
也许吧

getChildCount

可以帮助。。。它允许您从视图组中搜索所有视图子对象

  for (int i = 0; i < linearLayout.getChildCount(); i++) {
        if (linearLayout.getChildAt(i) instanceof RadioButton) {
          //Your code 
         }
       }
当然

text查看a[]

不可用,“[]”用于int、string和char等主要数据类型。这里的数据类型是一个对象,所以请使用ArrayList或List接口

List<View> linearlayout = new ArrayList<>(); 
.
.
for(int i = 0; i<yourlist.size(); i++){
View view = LayoutInflater.from(this).inflate(R.layout.layout_rating_question, null);
linearlayout.add(view);
}
对于第二个代码,请使用sergio推荐的或

List<RadioButton> rbList = new ArrayList<>();

rbList.add(rb1);
.
.
.
rbList.add(rb5);

for(int i = 0; i<rbList.size(); i++){
RadioButton rb = rbList.get(i);
rb.setOnCheckChangedLister(){...}
}

第一步,创建一个类:AnswerComponent,如下所示:

public class AnswerComponent {

public interface AnswerCallback {

    public void answerChanged(int index, int answer, boolean isChecked);
}

protected int mIndex;
protected String mValue1;
protected String mValue2;
protected Context mContext;
protected AnswerCallback mAnswerCallback;

public AnswerComponent(Context context) {
    mContext = context;
}

public View createView() {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View rootView = inflater.inflate(R.layout.component_anser, null, false);

    TextView tv = rootView.findViewById(R.id.tv);
    tv.setText(mValue1);

    TextView tv2 = rootView.findViewById(R.id.tv2);
    tv2.setText(mValue2);

    RadioButton rdb1 = rootView.findViewById(R.id.rdb1);
    rdb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (null != mAnswerCallback) {
                mAnswerCallback.answerChanged(mIndex, 1, isChecked);
            }
        }
    });
    RadioButton rdb2 = rootView.findViewById(R.id.rdb1);
    rdb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (null != mAnswerCallback) {
                mAnswerCallback.answerChanged(mIndex, 2, isChecked);
            }
        }
    });
    RadioButton rdb3 = rootView.findViewById(R.id.rdb1);
    rdb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (null != mAnswerCallback) {
                mAnswerCallback.answerChanged(mIndex, 3, isChecked);
            }
        }
    });

}

public void setIndex(int index) {
    mIndex = index;
}

public void setValue1(String value1) {
    mValue1 = value1;
}

public void setValue2(String value2) {
    mValue2 = value2;
}

public void setAnswerCallback(AnswerCallback answerCallback) {
    mAnswerCallback = answerCallback;
}
}

在createView方法中,可以膨胀xml布局。 然后,按如下方式使用该类:

 public void questionArray() {
    rows = new ArrayList<>();
    CSVReader csvReader = new CSVReader(MainActivity.this, "q32.csv");
    try {
        rows = csvReader.readCSV();
    } catch (IOException e) {
        e.printStackTrace();
    }
    answer.add(0);
    for (int i = 0; i < rows.size(); i++) {
        String value1 = String.valueOf(rows.get(i)[1]);
        String value2 = String.valueOf(rows.get(i)[2]);
        AnswerComponent answerComponent = new AnswerComponent(ll.getContext());
        answerComponent.setIndex(i);
        answerComponent.setValue1(value1);
        answerComponent.setValue2(value2);
        answerComponent.setAnswerCallback(new AnswerCallback() {
            @Override
            public void answerChanged(int index, int answer, boolean isChecked) {
                if(isChecked){
                    answer.set(index, answer);
                }
                else{
                    answer.remove(index, answer);
                }
            }
        });
        View view = answerComponent.createView();
        ll.addView(view);
    }

}`

这看起来很复杂。为什么不使用带有行和ArrayAdapter的Listview?我为database.csv中的每个条目生成5个单选按钮。但如果有更好的方法,请随时指导我。我尝试了您的解决方案,非静态方法不能在所有集合方法中从静态上下文引用,例如:answerComponent.setIndexi;AnswerComponent不是自定义视图,兄弟。我为索引、值1、值2创建了setter。请在AnswerComponent课堂上再读一遍。如果您仍然无法实施,请通过电子邮件nhat与我联系。thtb@gmail.com或者skype:live:be7b4688eeca5187。这是一个很好的线性布局列表答案。这就解决了我的问题!
 public void questionArray() {
    rows = new ArrayList<>();
    CSVReader csvReader = new CSVReader(MainActivity.this, "q32.csv");
    try {
        rows = csvReader.readCSV();
    } catch (IOException e) {
        e.printStackTrace();
    }
    answer.add(0);
    for (int i = 0; i < rows.size(); i++) {
        String value1 = String.valueOf(rows.get(i)[1]);
        String value2 = String.valueOf(rows.get(i)[2]);
        AnswerComponent answerComponent = new AnswerComponent(ll.getContext());
        answerComponent.setIndex(i);
        answerComponent.setValue1(value1);
        answerComponent.setValue2(value2);
        answerComponent.setAnswerCallback(new AnswerCallback() {
            @Override
            public void answerChanged(int index, int answer, boolean isChecked) {
                if(isChecked){
                    answer.set(index, answer);
                }
                else{
                    answer.remove(index, answer);
                }
            }
        });
        View view = answerComponent.createView();
        ll.addView(view);
    }

}`