请解释阵列适配器及其用途.更好 [英] Please explain Array Adapters and their purpose. Even better

查看:56
本文介绍了请解释阵列适配器及其用途.更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有更好的人可以逐点解释此程序吗?

Better yet can someone explain this program point by point?

package com.paad.todolist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate your view
    setContentView(R.layout.main);

    // Get references to UI widgets
    ListView myListView = (ListView)findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);

    // Create the array list of to do items
    final ArrayList<String> todoItems = new ArrayList<String>();
    // Create the array adapter to bind the array to the listview
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  todoItems);
    // Bind the array adapter to the listview.
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (event.getAction() == KeyEvent.ACTION_DOWN)
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
            {
              todoItems.add(0, myEditText.getText().toString());
              aa.notifyDataSetChanged();
              myEditText.setText("");
              return true;
            }
          return false;
        }
      });
  }
}

推荐答案

ToDoList 是定义用户看到的屏幕UI元素的活动.

ToDoList is the activity that defines the UI elements of the screen that user sees.

UI元素在布局/res/layout/main.xml

布局main.xml中的UI元素之一是ListView,其ID为 myListView

One of the UI element in the layout main.xml is a ListView whose ID is myListView

ListView 可以充当列表项的容器.因此,所有列表视图都需要知道的是,列表中有多少项,以及每个列表项的外观如何?

ListView can be something that acts as a container for list items. So all list view needs to know is, how many items are in the list, and how the each list item looks like?

适配器是有关列表项以及如何在屏幕上表示或绘制每个列表项的知识.

An adapter is something that knows about the list items and how to represent or draw each list item on the screen.

上面的示例利用ArrayAdapter,其构造函数采用3个参数,这些参数包含有关列表项的信息

Above example makes use of ArrayAdapter and its constructor takes 3 parameters having information about the list items

final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
                              android.R.layout.simple_list_item_1,
                              todoItems);

第一个参数是 Context ,以访问系统服务和资源(您需要使用布局修饰符来创建列表项视图)

First argument is Context to access system services and resources ( you need layout inflater to create list item view )

第二个参数定义列表的布局,该布局定义列表项在 listview 中的显示方式.这里使用由框架定义的布局 android.R.layout.simple_list_item_1 .

Second argument defines the layout of the list that defines how the list item appears in listview. Here layout android.R.layout.simple_list_item_1 which is defined by framework is used.

第三个参数是有关列表项的信息,通常,此信息用于创建列表项的视图.

Third argument is the information about the list item, typically this information is used to create view for the list item.

最后创建的 Adapter 被赋予 ListView

myListView.setAdapter(aa);

现在, ListView 调用 Adapter 的函数来获取列表项的视图并填充到容器中.

Now ListView calls the functions of Adapter to get the views of list item and populates in the container.

如果列表项(此处为待办事项列表)已更改,则适配器可以通过调用 notifyDataSetChanged 让ListView知道它.

If the list items are changed ( here todo list ) Adapter can let the ListView know about it by calling notifyDataSetChanged.

aa.notifyDataSetChanged();

您可以查看 ArrayAdapter的实现以获取更清晰.

You can have a look at the implementation of ArrayAdapter to get more clarity.

希望这对您有帮助!

这篇关于请解释阵列适配器及其用途.更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆