Android下显示本地Contact Number信息(续)
前文的代码只能显示联系人的第一个号码,很不方便。如果要列出联系人的所有号码,需要将People.CONTENT_URI改为Phones.CONTENT_URI。具体代码如下:
package name.dohkoos;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class ContactKit extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ListView contactList = (ListView)findViewById(R.id.contactList);
Cursor contactsCursor = managedQuery(Phones.CONTENT_URI,
null, null, null, null);
startManagingCursor(contactsCursor);
String[] columnsToMap = new String[] {Phones.NAME, Phones.NUMBER};
int[] mapTo = new int[] {R.id.contactName, R.id.contactNumber};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.contact_entry, contactsCursor,
columnsToMap, mapTo);
contactList.setAdapter(mAdapter);
}
}
Random Posts
如何获取Android手机的型号
怎么样才能获的Android手机的型号呢?例如HTC Hero或HTC Magic等名字。android.os.Build类有很多静态属性可以获得机器的信息,其中的MODEL属性就指代手机型号。
package name.dohkoos.lesson;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
public class QueryProductModel extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Product Model: " + Build.MODEL);
setContentView(tv);
}
}
在我的英雄机上显示的是
Product Model: HTC Hero
Random Posts
Android下显示本地Contact Number信息
首先要在AndroidManifest.xml中配置允许读取联系人的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
contact_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/contactList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
contact_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/contactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ContactKit.java
package name.dohkoos;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class ContactKit extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ListView contactList = (ListView)findViewById(R.id.contactList);
Cursor contactsCursor = managedQuery(People.CONTENT_URI,
null, null, null, null);
startManagingCursor(contactsCursor);
String[] columnsToMap = new String[] {People.NAME, People.NUMBER};
int[] mapTo = new int[] {R.id.contactName, R.id.contactNumber};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.contact_entry, contactsCursor,
columnsToMap, mapTo);
contactList.setAdapter(mAdapter);
}
}
Related Posts
用Rails 2.3打造简单记账软件(12)
在这以前我们把tags作为Entry的一个字段来实现,这样做有很大的局限性,而且还有好多插件可以实现这个功能,现在我们就来把它替换用插件acts_as_taggable_on_steroids实现。
安装插件
script/plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git script/generate acts_as_taggable_migration rake db:migrate
在Entry中添加一句代码
class Entry < ActiveRecord::Base acts_as_taggable belongs_to :user end
还要将所有视图中原来的tags字段替换成tag_list字段。
index.html.erb
<td><%=h entry.tag_list %></td>
edit.html.erb
<%= f.text_field :tag_list %>
new.html.erb
<%= f.text_field :tag_list %>
还要将entry.rb中的验证字段tags改成tag_list。做完这些插件也就安装完成了。
既然用了插件,那么原来的tags字段就不需要了,写个迁移任务把它处理掉吧!
script/generate migration remove_tags_from_entry
迁移代码如下:
class RemoveTagsFromEntry < ActiveRecord::Migration
def self.up
remove_column :entries, :tags
end
def self.down
add_column :entries, :tags, :string
end
end
Related Posts
用Rails 2.3打造简单记账软件(11)
当网站出现问题时,让它发送一份错误报告到你的邮箱是件挺不错的事,这样你就不必再担心不能及时发现问题了。
exception_notification插件的功能就是当你的Rails应用出错时,它会向指定的邮箱地址发送错误日志。
使用很简单,安装插件后配置一下邮件接受者就可以了。
安装插件
script/plugin install git://github.com/rails/exception_notification.git
在config/environment.rb中增加邮件接收者的地址
Rails::Initializer.run do |config| ... end ExceptionNotifier.exception_recipients = %w(youremail@domain.com)
然后告诉Exception Notifier哪些controller出错才发送日志,当然是全部啦:
class ApplicationController < ActionController::Base include ExceptionNotifiable ... end
Exception Notifier采用Rails中的ActionMailer发送邮件,所以使用的前提是确保ActionMailer可以正常发送邮件。还有就是Exception Notifier默认只在production环境下才会生效,如果想要在development下也生效,需要将development.rb文件中的
config.action_mailer.raise_delivery_errors = false
修改为
config.action_mailer.raise_delivery_errors = true
Related Posts
用Rails 2.3打造简单记账软件(10)
从2.2版本起Rails开始内置支持i18n,因此以后实现国际化/本地化就可以不再需要各种各样的插件了。
Rails默认的locale文件夹是config/locales,假设你要支持中文和英文,那么你需要在这个文件夹下放置zh.yml和en.yml两个文件。
相应的入门教程网上有不少,我也就不多讲了。这里主要说一下如何在记账应用中实现可以让用户指定语言的i18n实现,即当用户选择English,那么界面就切换成英文界面,并且以后打开的页面也是以英文出现,反之亦如此。
首先在header区添加以下代码:
<%= link_to 'Chinese', :locale => 'zh' %> <%= link_to 'English', :locale => 'en' %>
点击其中某个链接后,浏览器就会传递对应的参数zh或en到后台。
然后在app/controllers/application_controller.rb中添加以下代码:
before_filter :set_locale def set_locale I18n.locale = params[:locale] end
不过这里有个问题就是url中必须得带着参数,不然的话用户的选择就会失效。要想使选择达到持续的效果,可以考虑把这些信息保存在session中,改进后的代码如下:
def set_locale I18n.locale = params[:locale] || session[:locale] session[:locale] = I18n.locale end
现在这样就基本上差不多了。剩下的就是处理第一次访问时的语言,这个可以从http头中的ACCEPT_LANGUAGE参数获取。最终实现的代码:
def set_locale
accept_lang = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
I18n.locale = params[:locale] || session[:locale] || accept_lang || 'zh'
session[:locale] = I18n.locale
end
不过当用户设置locale后,在新建Entry时会报出以下的异常:
can't convert Symbol into String
Extracted source (around line #8):
6: <p>
7: <%= t(:effective_date) %><br />
8: <%= f.date_select :effective_date %>
查找资料后发现可以通过在date_select中加入order来解决。但日期下拉列表中的月份还有问题,出现的不是正常月份,而是一些随机数。这个可以在locale文件中加入month_names。
new.html.erb
<p> <%= t(:effective_date) %><br /> <%= f.date_select :effective_date %> </p>
en.yml
en:
date:
month_names: [~, January, February, ..., November, December]
order: [ :year, :month, :day ]
zh.yml
zh:
date:
month_names: [~, 一月, 二月, ..., 十一月, 十二月]
order: [ :year, :month, :day ]
Related Posts
升级Rails到2.3.5
许多网上的Rails开源项目使用的版本都是2.0.x,因此在2.3.5下运行的时候会出现如
uninitialized constant ApplicationController
这样的错误。解决该问题的办法是修改config/environment.rb中的RAILS_GEM_VERSION为2.3.5,再运行:
rake rails:update
即可。该任务会把app/controller下的application.rb改名为application_controller.rb。
Related Posts
如何导入/导出Heroku中应用的数据
导入/导出Heroku上的数据需要安装Taps包:
sudo gem install taps
导入数据到Heroku里:
heroku db:push
导出数据到本地:
heroku db:pull
其中导入/导出数据由config/database.yml配置设定。
Heroku还支持主机到主机的数据库传输,具体细节可以参考http://docs.heroku.com/taps。
Related Posts
如何联机调试Android程序
1. 首先你需要一台开发手机,如果不是的话可以刷ROM
2. 在Settings -> Applications -> Development下激活USB debugging选项
3. 下载并安装USB驱动程序
http://handheld.softpedia.com/progDownload/HTC-Hero-Drivers-Download-81097.html
4. 用USB数据线连接手机与电脑
5. 在AndroidManifest.xml中的<application>里添加android:debuggable="true"
6. 然后你就可以像平常一样调试你的Android代码了
Related Posts
用Rails 2.3打造简单记账软件(9)
没啥多说的,继续开始干活。
修改app/views/entries/index.html.erb为:
<h1>Listing entries</h1>
<table>
<thead>
<tr>
<th class="center">Date</th>
<th class="center">Amount</th>
<th class="center">Tags</th>
<th class="center">Comment</th>
<th class="center">Action</th>
</tr>
</thead>
<tbody>
<% @entries.each do |entry| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="center"><%= entry.effective_date %></td>
<td class="right"><%= number_to_currency(entry.amount) %></td>
<td><%= entry.tags %></td>
<td><%= entry.comment %></td>
<td class="center">
<%= link_to 'Edit', edit_entry_path(entry) %>
<%= link_to 'Destroy', entry, :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br />
<%= link_to 'New entry', new_entry_path %>
将app/views/layouts/目录下的entries.html.erb改名为application.html.erb,并修改文件内容为:
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Money: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= stylesheet_link_tag 'money' %>
<%= stylesheet_link_tag 'table' %>
</head>
<body>
<div id="container">
<%= render :partial => 'layouts/header' %>
<% if logged_in? %>
<%= render :partial => 'layouts/default' %>
<% else %>
<%= render :partial => 'layouts/main' %>
<% end %>
<%= render :partial => 'layouts/footer' %>
</div>
</body>
</html>
创建app/views/layouts/_header.html.erb文件:
<div id="header">
<div id="usernav">
<% if logged_in? %>
<strong>You are logged in as <%= current_user.login %></strong>
<%= link_to 'Logout', logout_path %>
<% else %>
<%= link_to 'Login', login_path %> |
<%= link_to 'Sign Up', signup_path %>
<% end %>
</div>
</div>
创建app/views/layouts/_footer.html.erb文件:
<div id="footer"> Copyright © 2009 example.com. All rights reserved. </div>
创建app/views/layouts/_main.html.erb文件:
<div id="main"> <%= yield %> </div>
创建app/views/layouts/_default.html.erb文件:
<div id="boider">
<div id="content">
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</div>
<div id="sidebar">
<ul>
<li><%= link_to 'All entries', entries_path %></li>
</ul>
</div>
</div>
修改public/stylesheets/money.css为:
body {
padding: 0;
margin: 0;
}
#container {
width: 960px;
margin: 5px auto;
}
#header {
background: url(/images/money.png) no-repeat 30px;
height: 97px;
}
#tagline {
float: left;
margin-left: 230px;
}
#tagline h2 {
font-size: 26px;
color: #0d4988;
line-height: 28px;
text-align: center;
}
#header #usernav {
float: right;
}
#navbar {
background: #efefef url(/images/line.png) repeat-x left bottom;
height: 33px;
}
#main, #bodier {
clear: both;
}
#main {
background: #efefef;
}
#content {
float: right;
width: 720px;
}
#sidebar {
background-color: #efefef;
float: left;
width: 230px;
padding-top: 18px;
}
#footer {
border-top: 1px solid #bbb;
clear: both;
text-align: center;
}
h1 {
font-size: 18px;
line-height: 20px;
}
h3 {
font-size: 12px;
line-height: 24px;
text-align: center;
}
img {
border-style: none;
}
.left {
text-align: left;
}
.center {
text-align: center;
}
.right {
text-align: right;
}
新建public/stylesheets/table.css,内容为:
table {
border-collapse: collapse;
text-align: left;
width: 90%;
}
thead {
height: 30px;
}
thead th {
padding: 5px;
background: #efefef;
}
tbody td {
padding: 5px;
}
th, td {
border: 1px solid #e6e6e6;
}
tr.odd {
background: #fff;
}
tr.even {
background: #f9f9f9;
}

