Tony Ming
Tony Ming

Reputation: 101

Finding view from multiple custom layout

I am generating 5 radio button according to the rows of data. Now I would like to select the radio button, how do I find them?

Perhaps I had to reorganize the generating code by adding an array, but how?

My concept is to make something like this

TextView a[]=new TextView[1];

But it not workable

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

How should I code it? Below is my view generation code

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

Edit 1
My initial code for the radio button is this: (If this is simpler to understand)

RadioButton rb1 = linearLayout.findViewById(R.id.rate1);
        RadioButton rb2 = linearLayout.findViewById(R.id.rate2);
        RadioButton rb3 = linearLayout.findViewById(R.id.rate3);
        RadioButton rb4 = linearLayout.findViewById(R.id.rate4);
        RadioButton rb5 = linearLayout.findViewById(R.id.rate5);
        rb1.setSelected(true);

        rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    answer.set(finalI,1);
                }
            }
        });
        rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    answer.set(finalI, 2);
                }
            }
        });
        rb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    answer.set(finalI, 3);
                }
            }
        });
        rb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    answer.set(finalI, 4);
                }
            }
        });
        rb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    answer.set(finalI, 5);
                }
            }
        });

Is there any way to make this possible?

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

Upvotes: 0

Views: 63

Answers (3)

Shubham
Shubham

Reputation: 185

Ofcourse

TextView a[]

is not workable, '[]' is used for primary data types like int, string and char. Your datatype is an object here so use an ArrayList or List interface

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

for your second code, use what sergio has recommended or

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(){...}
}

Upvotes: 1

NhatVM
NhatVM

Reputation: 2124

First step, create a class : AnswerComponent like this:

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;
}

}

In createView() method, you can inflate your xml layout. Then, user this class as below:

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

}`

Upvotes: 0

Sergio
Sergio

Reputation: 461

Maybe

getChildCount()

can help... It allows you to search all the view childs from a ViewGroup

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

Upvotes: 0

Related Questions