Help us understand the problem. What is going on with this article?

RubyMotion for AndroidでlistLiewを使ってみる

More than 5 years have passed since last update.

listViewを使ってみたのでただのメモ。

ArrayAdapterを使った場合

ただArrayAdapterを使うだけだとつまらないので、
ArrayAdapterを継承した HelloArrayAdapterを使う

main_activity.rb
class MainActivity < Android::App::Activity
  Members = ['Kanako', 'Ayaka', 'Shiori', 'Momoka', 'Reni']

  def onCreate(savedInstanceState)
    super

    list = Android::Widget::ListView.new(self)
    list.adapter = HelloArrayAdapter.new(self, Android::R::Layout::Simple_list_item_1, Members)

    layout = Android::Widget::LinearLayout.new(self)
    layout.addView(list, Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::MATCH_PARENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))

    self.contentView = layout
  end
end

hello_array_adapter.rb
class HelloArrayAdapter < Android::Widget::ArrayAdapter
  def getView(position, convertView, parent)
    textView = Android::Widget::TextView.new(context)
    textView.text = self.getItem(position)

    layout = Android::Widget::LinearLayout.new(context)
    layout.addView(textView, Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::MATCH_PARENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))

    layout
  end
end

BaseAdapterを使った場合

ArrayAdapterじゃなくて、BaseAdapterを継承してAdapterを使うことが多いので、こちらも使ってみる
ItemClickListenerとかも設定してみた。

main_activity.rb
class MainActivity < Android::App::Activity
  def onCreate(savedInstanceState)
    super
    listView = Android::Widget::ListView.new(self)
    listView.adapter = HelloListAdapter.new
    listView.onItemClickListener = self
    listView.choiceMode = Android::Widget::AbsListView::CHOICE_MODE_SINGLE

    textView = Android::Widget::TextView.new(self)
    textView.text = "Momoiro Clover Z"

    layout = Android::Widget::LinearLayout.new(self)
    layout.orientation = Android::Widget::LinearLayout::VERTICAL
    layout.addView(textView, Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::MATCH_PARENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))
    layout.addView(listView, Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::MATCH_PARENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))

    self.contentView = layout    
  end

  def onItemClick(parent, view, position, id)
    p position
  end
end
hello_list_adapter.rb
class HelloListAdapter < Android::Widget::BaseAdapter 
  Data = ["Kanako", "Aayaka", "Shiori", "Momoka", "Reni"]

  def getCount()
    Data.size
  end

  def getItem(position)
    Data[position]
  end

  def getItemId(position)
    position
  end

  def getView(position, convertView, parent)
    context = parent.context
    if convertView.nil?
      textView1 = Android::Widget::TextView.new(context)
      textView1.text = (position + 1).to_s

      textView2 = Android::Widget::TextView.new(context)
      textView2.text = getItem(position)

      layout = Android::Widget::LinearLayout.new(context)
      layout.orientation = Android::Widget::LinearLayout::HORIZONTAL
      layout.addView(textView1,  Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::WRAP_CONTENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))
      layout.addView(textView2,  Android::Widget::LinearLayout::LayoutParams.new(Android::View::ViewGroup::LayoutParams::MATCH_PARENT, Android::View::ViewGroup::LayoutParams::WRAP_CONTENT))
      layout
    else
      convertView
    end
  end
end

このぐらいサクッとかけるかなと思ったけど、慣れてないので意外と時間かかりました。
特に、addViewのLayoutParamsとかちょっと間違えても、気がつかなかったりするので、ハマったりします。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away