简体   繁体   English

如何在ListView中控制LinearLayout的子级?

[英]How to control children of LinearLayout in a ListView?

I just set up my ArrayAdapter to convertView to my favor , but now I wonder how to control the children of the LinearLayouts am using in that listview , lets say I have a TextView in the Linearlayout , I want to call setText , but I don't know how , or maybe I have a button which I want to call setOnClickListener , I don't know how ! 我只是将ArrayAdapter设置为convertView以得到我的青睐,但是现在我想知道如何控制在该listview中使用的LinearLayouts的子级,可以说我在Linearlayout中有一个TextView,我想调用setText,但是我不这样做。不知道如何操作,或者我有一个要调用setOnClickListener的按钮,我不知道如何!

Here is my getView code : 这是我的getView代码:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null){
            convertView = inf.inflate(R.layout.normal_row, parent , false);
        }
        switch(position){
        case(0):
            // here is where my first LinearLayout goes , containing an Image and Switch which I need to program in onCheckedChangeListener ...
            convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);

            break;

        case(1)
        // here I need to set the Text for the following two options on some basis , they are both linearLayouts containing a single textView which I want to call setText for each case  1 and 2.
             break;
            case(2)

        }

Let's say your normal_row layout has a TextView in it with the id @id/my_text_view. 假设您的normal_row布局中有一个ID为@ id / my_text_view的TextView。 Then you would set the text like 然后您将文本设置为

TextView view = (TextView) convertView.findViewById(R.id.my_text_view);
view.setText("Some text");

You can access anything in the layout you've inflated by its id in a similar way. 您可以以类似的方式访问通过其ID夸大的布局中的任何内容。

One thing to note though is there seem to be two types of views that you're inflating. 不过要注意的一件事是,您似乎夸大了两种类型的视图。 Let's say that you try to use the code I gave you, but my_text_view is only in normal_row. 假设您尝试使用我给您的代码,但是my_text_view仅在normal_row中。 Then if you've inflated the row_switch view, the findViewById() will return null because it can not find a view with the given id. 然后,如果您row_switchrow_switch视图,则findViewById()将返回null,因为它找不到具有给定id的视图。

Use onItemClick and find your view by the view that the OnItemClickListener provides you! 使用onItemClick并通过OnItemClickListener为您提供的视图找到您的视图!

For example: `mListView.setOnItemClickListener(new OnItemClickListener() { 例如:`mListView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

        }

    });`

By the passed View find your textview or your button inside every listView children. 通过传递的View,在每个listView子级中找到您的textview或按钮。

Example: TextView tempTextView = (TextView) arg1.findViewById(R.id.myTextView); 示例: TextView tempTextView = (TextView) arg1.findViewById(R.id.myTextView);

The id of the textview is the one you assigned to it in your inflated layout for every row! Textview的ID是您在展开式布局中为每一行分配的ID!

TextView textview = (TextView) convertView.findById(R.id.txt); 
text.setText("textview");

Button btn  = (Button) convertView.findById(R.id.btn); 

btn.setTag(textview); 

btn.setOnClickListener(...)
{
   onClick(..){
      TextView t = (Textview)btn.getTag():
      t.setText("new value")

   }
}

i suggest you to look at the question which i replied before. 我建议您看看我之前回答的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM