0

我刚刚将我的 ArrayAdapter 设置为 convertView 对我有利,但现在我想知道如何控制在该 listview 中使用的 LinearLayouts 的子级,假设我在 Linearlayout 中有一个 TextView ,我想调用 setText ,但我没有不知道怎么做,或者我有一个我想调用 setOnClickListener 的按钮,我不知道怎么做!

这是我的 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)

        }
4

3 回答 3

0

假设您的 normal_row 布局中有一个 ID 为 @id/my_text_view 的 TextView。然后你会设置文本像

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

您可以以类似的方式访问您通过其 id 膨胀的布局中的任何内容。

需要注意的一件事是,您似乎在夸大两种类型的视图。假设您尝试使用我给您的代码,但my_text_view仅在 normal_row 中。然后,如果您膨胀了row_switch视图,findViewById() 将返回 null,因为它找不到具有给定 id 的视图。

于 2012-12-09T21:17:36.747 回答
0

使用 onItemClick 并通过 OnItemClickListener 为您提供的视图找到您的视图!

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

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

        }

    });`

通过传递的 View 在每个 listView 子项中找到您的 textview 或您的按钮。

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

textview 的 id 是您在每一行的膨胀布局中分配给它的那个!

于 2012-12-09T21:17:52.600 回答
0
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")

   }
}

我建议你看看我之前回答的问题

于 2012-12-09T21:19:11.950 回答