0

I has to ask you how to find my Views, I created them dynamically because all information about Texts were in my string-array, and now I would like to find it to store this in Class by getters, to use it in another classes, in feature I would like to make backend to store information thats why I chose class for temporary solution.

Thats my code

sportlist = getResources().getStringArray(R.array.btnsport);
    for(j=0;j<sportlist.length;j++){
        sporthash.put(j,sportlist[j]);
        System.out.println(sporthash);
    }


//Here I create my TableRow with TextView

    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
    lp.height = 120;
    lp.setMargins(8,8,8,8);

    for(j=0;j<sportlist.length;j++) {
        TableRow row = new TableRow(getActivity());
        row.setLayoutParams(lp);
        final TextView t = new TextView(getActivity());
        t.setPadding(10, 10, 10, 10);
        t.setText(sporthash.get(j));
        t.setGravity(Gravity.CENTER);
        t.setClickable(true);
        t.setTextSize(18);
        t.setId(j);
        t.setBackgroundResource(R.drawable.sport_false);
        t.setTextColor(Color.BLACK);

Then after this I have saving button, which send information to my Storage.class. I try to set t.setId(j) and find it by new TextView h, h.findViewById(j) but its return strage code. I would to find Views with t.BackgroundResource(R.drawable.sport_true) and send them to my Storage.class.

Thats my button, in for I tryed to find my views, so what solution would be best ?

 save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Storage sto = new Storage();
            sto.setAge(BirthDate.getText().toString());
            sto.setImage(civ);
            sto.setLocation(Map.getText().toString());
            System.out.println(Map.getText().toString());
            for(j=0;j<sportlist.length;j++) {



            }

            Intent newIntent = new Intent(getActivity(),ProfilActivity.class);
            startActivity(newIntent);
        }
    });
Kertuj
  • 33
  • 5
  • Store them in an array ... but obviously class called "Storage" shouldn't know about UI at all – Selvin Oct 30 '17 at 10:18
  • 1
    Possible duplicate of [How to get the id of a dynamically created textview?](https://stackoverflow.com/questions/14575169/how-to-get-the-id-of-a-dynamically-created-textview) – AskNilesh Oct 30 '17 at 10:18

1 Answers1

0

You can't use setId here, because setId should be used with generateViewId:

From documentation of generateViewId():

generateViewId

Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

When you're trying to set the id with setId() there's probability your id is colliding with auto-generated id by the Android Studio. So, you need to ensure the id is unique.

You can use setTag() and getTag() to avoid the problem with setId()

Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96