简体   繁体   English

Android 新手,请帮我理解这段代码

[英]Android newbie, help me understand this block of code please

I am following a book's examples to learn Android and came accross this code:我正在按照一本书的例子来学习 Android 并遇到了这段代码:

public class DynamicDemo extends ListActivity { 
  TextView selection; 
  private static final String[] items={"lorem", "ipsum", "dolor", 
          "sit", "amet"}
  @Override 
  public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    setListAdapter(new IconicAdapter()); 
    selection=(TextView)findViewById(R.id.selection); 
  } 

  public void onListItemClick(ListView parent, View v, 
                             int position, long id) { 
   selection.setText(items[position]); 
  } 

  class IconicAdapter extends ArrayAdapter<String> { 
    IconicAdapter() { 
      super(DynamicDemo.this, R.layout.row, R.id.label, items); 
    } 

    public View getView(int position, View convertView, 
                       ViewGroup parent) { 
      View row=super.getView(position, convertView, parent); 
      ImageView icon=(ImageView)row.findViewById(R.id.icon); 

      if (items[position].length()>4) { 
        icon.setImageResource(R.drawable.delete); 
      } 
      else { 
        icon.setImageResource(R.drawable.ok); 
      } 

      return(row); 
    } 
  }
} 

I actually understand the code except for in the constructor why do we say:我实际上理解除了构造函数中的代码为什么我们说:

super(DynamicDemo.this, R.layout.row, R.id.label, items);

That is the only part that is confusing me.这是唯一让我困惑的部分。
Thanks!谢谢!
R R

All that's doing is invoking the superclass constructor (ie ArrayAdapter ).所做的只是调用超类构造函数(即ArrayAdapter )。 This must be the first line in the subclass constructor.这必须是子类构造函数的第一行。


I recommend you read the Using the Keyword super tutorial.我建议您阅读使用关键字超级教程。

There are multiple things going on in there.里面发生了很多事情。

First off, that is a super call, so it is calling the ArrayAdapter 's constructor.首先,这是一个super调用,所以它调用了ArrayAdapter的构造函数。 It is calling this constructor: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, int, T[])它正在调用这个构造函数: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, int, T[])

The first parameter is a use of this that actually gets the current instance of the DynamicDemo class.第一个参数是使用this实际获取 DynamicDemo class 的当前实例。 This is used since DynamicDemo extends ListActivitty which extends Activity which eventually extends Context, the type of object you need there.这是因为 DynamicDemo 扩展 ListActivitty 扩展 Activity 最终扩展 Context,你需要的 object 类型。

The next two parameters are resource IDs which it asks for.接下来的两个参数是它要求的资源 ID。

The last parameter is an array it needs to populate the adapter.最后一个参数是填充适配器所需的数组。

You are passing the custom layout to your ArrayAdapter R.layout.row, which has the image field.您正在将自定义布局传递给具有图像字段的 ArrayAdapter R.layout.row。

While rendering this you are again asking the super view(custom view) and modifying the image as per your requirement.在渲染时,您再次询问超级视图(自定义视图)并根据您的要求修改图像。

I believe, you can create the view directly in your GetView without using the base constructor我相信,您可以直接在 GetView 中创建视图,而无需使用基本构造函数

If you separate the IconicAdapter in separate class, you will have to pass the items list while instantiating the class如果将 IconicAdapter 分开在单独的 class 中,则在实例化 class 时必须传递项目列表

super(DynamicDemo.this, R.layout.row, R.id.label, items);



@Param  DynamicDemo.this : context
@param R.layout.row : list item layout reference
@param R.id.label : id of textView for which you want to setText string at position i from String-Array (items)
@param items : arrays list containing strings you want to show in list

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

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