ListActivityラーニング

3709 ワード

1. public class ListActivity extends Activity;
2.ListActivityには、次の2つの重要なフィールドが含まれています.
  protected ListAdapter mAdapter; --アダプタ
  protected ListView mList;      -- ListView
3.一般的な方法
  1) protected void onListItemClick(ListView l, View v, int position, long id)
  This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
この方法は、ListViewのいずれかのエントリが選択(クリック)するとトリガーされる.このメソッドはサブクラスで書き直さなければならない.サブクラスではgetListView()を呼び出すことができる.getItemAtPositionメソッドは、選択する(クリック)itemに関連付けられたデータを取得する.
  2) public void onContentChanged()
   Updates the screen state (current list and other views) when the content changes.
コンテンツが変更すると、このメソッドを呼び出す、画面の状態(現在のlistおよび他のviews)を更新することができる.
  3) public void setListAdapter(ListAdapter adapter)
  Provide the cursor for the list view.
リストにadapterを指定します.
  4) public ListView getListView()
  Get the activity's list view widget.
Activityのlist viewオブジェクトを取得します.
  5) public ListAdapter getListAdapter()
  Get the ListAdapter associated with this activity's ListView.
このactivityのListViewに関連付けられたAdapterの取得
記憶:ほとんどの方法にはListが含まれている.
 
/**
*
*/
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getApplicationContext(), "Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
    // Return true to consume the click event. true
    return true;
    }
});

 
/**
 * ListView item ( , ListView item , )
 * getItem(position) item
 * getItemAtPosition(position) getItem(position)
 */
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  // Get the item that was clicked
  Object obj = this.getListAdapter().getItem(position);
  HashMap<String, Object> item = (HashMap<String, Object>) obj;

  //
  // HashMap<String, Object> item = (HashMap<String, Object>) l.getItemAtPosition(position);
  int iId = (Integer) item.get("id");
  String name = (String) item.get("name");
  System.out.println("iId = " + iId + " :: position = " + position + " :: name = " + name);
}