Skip to content

Commit

Permalink
some tests with lists and remote data
Browse files Browse the repository at this point in the history
  • Loading branch information
soyoh committed Sep 8, 2016
1 parent ff907cd commit a8b1e6b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Motion::Project::App.setup do |app|

app.files += Dir.glob(File.join(app.project_dir, 'lib/**/*.rb'))

app.permissions = [:internet]

app.gradle do
dependency 'com.android.support:appcompat-v7:24.2.0'
dependency 'com.android.support:design:24.2.0'
Expand Down
7 changes: 7 additions & 0 deletions app/adapters/ListAdapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ListAdapter < Android::Widget::ArrayAdapter

def data=(data)
@data = data
end

end
42 changes: 37 additions & 5 deletions app/fragments/tab_one_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class TabOneFragment < Android::Support::V4::App::ListFragment
attr_accessor :has_been_already_loaded

def onCreateView(inflater, container, savedInstanceState)
@list = []
@full_data_list = []
self.hasOptionsMenu = true

view = inflater.inflate(R::Layout::Tab_one, container, false)
Expand All @@ -14,13 +16,14 @@ def onCreateView(inflater, container, savedInstanceState)
end

def onActivityCreated(savedInstanceState)
super(savedInstanceState)
setListAdapter(adapter)
# getListView().setOnItemClickListener(this)
super(savedInstanceState)
@adapter = Android::Widget::ArrayAdapter.new(activity, Android::R::Layout::Simple_list_item_1, [])
setListAdapter(@adapter)
end

def adapter
Android::Widget::ArrayAdapter.new(activity, Android::R::Layout::Simple_list_item_1, States.all)
def onListItemClick(parent, view, position, id)
puts @list[position]
puts @full_data_list[position]
end

def onCreateOptionsMenu(menu, inflater)
Expand All @@ -32,9 +35,38 @@ def setUserVisibleHint(is_fragment_visible)
if (self.isVisible)
if (is_fragment_visible && !@has_been_already_loaded)
puts "---> Fragment #{self.class} visible"

get_demo_data

@has_been_already_loaded = true
end
end

def get_demo_data
requests = MotionVolley::Request.new
url = "http://jsonplaceholder.typicode.com/posts"
requests.json_array(url, activity, self)

# Simple Post
# requests.json(:get, 'http://jsonplaceholder.typicode.com/posts/12', activity, self)
end

def data_response(response)
puts "======== Success Response "
puts response

@list = []
@full_data_list = []
for i in 0..(response.length-1)
object = response.get(i)
@full_data_list << object
@list << object.getString('title')
end

@adapter.clear()
@adapter.addAll(@list)
@adapter.notifyDataSetChanged()
end

end
end
25 changes: 25 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Post

def initialize(data = {})
@data = data
end

def title
fetch('title')
end

def body
fetch('body')
end

def id
fetch('id')
end

private

def fetch(key, no_text = 'N/a')
@data.fetch(key, no_text)
end

end

0 comments on commit a8b1e6b

Please sign in to comment.