dynamically name textview after inflating android

狂风中的少年 提交于 2019-12-12 02:29:04

问题


I m inflating 4 linear layouts multiple times in a scroll view. Each layout has a title in its header. The code snippet is given below.

for(int j=0;j<qType.length;j++)
    {           
        LinearLayout.LayoutParams siz = new LinearLayout.LayoutParams(width, height);;
        if(qType[j]==0)
        {               
            view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);                                       
        }
        else if(qType[j]==1)
        {                
            view1 = getLayoutInflater().inflate(R.layout.layout3, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);
        }
        else if(qType[j]==2)
        {           
            view1 = getLayoutInflater().inflate(R.layout.layout4, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);
        }
        else if(qType[j]==3)
        {           
            view1 = getLayoutInflater().inflate(R.layout.layout5, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/2);
        }       

        siz.topMargin = 25;
        main_layout.addView(view1, siz);            
    }
    scroll_layout.addView(main_layout);
    scroll_layout.setBackgroundResource(R.drawable.options_background);     
    setContentView(scroll_layout);  

Now there is textview in each layout and i want to change the text of it. If i access them by findviewbyid and give settext, only the first instance is being changed, i want to change the textview on all occasions.

    for(int k=0;k<NumberQuestions.length;k++)
    {
        TextView number_title = (TextView)main_layout.findViewById(R.id.number_title);
        TextView mcq_title = (TextView)main_layout.findViewById(R.id.mcq_title);
        TextView comment_title = (TextView)main_layout.findViewById(R.id.comment_title);
        TextView cam_title = (TextView)main_layout.findViewById(R.id.cam_title);
        if(qType[k]==0)
        {               
            number_title.setTypeface(myriadpro_bold);
            number_title.setText(NumberQuestions[k]);

        }
        if(qType[k]==1)
        {
            comment_title.setTypeface(myriadpro_bold);
            comment_title.setText(NumberQuestions[k]);              
        }
        else if(qType[k]==2)
        {
            mcq_title.setTypeface(myriadpro_bold);
            mcq_title.setText(NumberQuestions[k]);              
        }
        else if(qType[k]==3)
        {
            cam_title.setTypeface(myriadpro_bold);
            cam_title.setText(NumberQuestions[k]);              
        }           

Please help.


回答1:


probably the TextView android: id in all layouts are the same, thus explaining the behavior of the method findViewById always returns the first occurrence




回答2:


Disclaimer: I feel like you are trying to work against the Android Framework, as opposed to working with it. My suggestion is a tad bit of an overhaul, but I do feel like it is the correct way to go about things.

I would recommend using a ListView, since you are repeating the same format many times. A listview will typically have a container of objects of objects, and an adapter responsible for mapping an object to a particular row within the listview. When you change the data in the underlying container, you will call the callback notifyDataSetChanged() for your adapter to notify it that you changed the underlying data and alert it to update your listview.

2 great resources/tutorials from the android developer site: ListView: http://developer.android.com/guide/topics/ui/layout/listview.html Adapters: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews




回答3:


If i am right main_layout is a name view in your xml file.So You should use like this

 view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false);
 TextView tv = (TextView)main_layout.findViewById(R.id.tv1);

where tv is texview in layout1.xml



来源:https://stackoverflow.com/questions/15530755/dynamically-name-textview-after-inflating-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!