用户操作
[即时聊天] [发私信] [加为好友]
Jun_XuID:Jun_Xu
903次访问,排名2万外,好友0人,关注者0人。
Jun_Xu的文章
原创 3 篇
翻译 0 篇
转载 0 篇
评论 0 篇
最近评论
文章分类
    收藏
      相册
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky
      订阅到鲜果
      订阅到Google
      订阅到抓虾
      订阅到BlogLines
      订阅到Yahoo
      订阅到GouGou
      订阅到飞鸽
      订阅到Rojo
      订阅到newsgator
      订阅到netvibes

      原创 GPhone--Android学习笔记(二):练习(1) Notepad V1收藏

      新一篇: GPhone--Android学习笔记(三):练习(2) Notepad V2 | 旧一篇: GPhone--Android学习笔记(一):Android概况及工具

      本文与SDK中的帮助才不多,但不是直接的翻译。

      练习目标:
      1. 使用ListActivities,并使用菜单
      2. 学习使用操作SQLite数据库
      3. 使用ArrayAdapter绑定数据到ListView中
      4. 掌握一些基本的操作,如菜单的显示,菜单命令的处理,增加数据项等。


      第一步:
      在SDK中下载获得Notepadv1的代码,并导入到Eclipse中。
      导入步骤:
       a. 在Package Explorer中,右键选择Import.../General/Existing Projects into Workspace
       b. 点Browse按钮,选择Notepadv1 的目录,并点OK
       c. 你将会看到Notepadv1被列在项目区中,默认会被打勾,如果没有打勾,请手动勾上。
       d. 点Finish
       e. Notepadv1将被列在Package Explorer中
       f. 如果有提示关于AndroidManifest.xml的错误,请选中此项目,并右键选择Android Tools->Fix Project,他将会自动帮你修复错
      误。

      第二步:
      看一下数据库操作类:DBHelper,还是比较简单的,自己看去 :)。

      第三步:
      打开res/layout/notepad_list.xml 这个文件,快速的看下就可以了:
       a.<?xml version="1.0" encoding="utf-8"?>, XML文件的固定头,地球人都知道...
       b.一个Layout的定义,这里是 LinearLayout,但不一定是这个,可以是其他的Layout
       c.Android里固定的一个定义:xmlns:android="http://schemas.android.com/apk/res/android"


      第四步:
      在上面的那个文件中加入:
        <ListView id="@id/android:list"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
        <TextView id="@id/android:empty"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/no_notes"/>

      这里ListView和TextView好像是ListActivity里默认存在的,如果没有好像会Crash,有高人的话补充下...
       a.ListView和TextView不会同是显示,如果没有数据,则默认显示TextView(这个View里会显示一个字符串)。如果有数据,则会显示

      ListView。
       b.@是默认的关键字,XML解析器将会自动替换这个符号后面的ID
       c.android:list 和android:empty 是android平台预定义好的ID,如果你想显示空的TextView,可以调用setEmptyView().

      第五步:
      建立一个新文件res/layout/notes_row.xml,文件内容如下:
      <?xml version="1.0" encoding="utf-8"?>
      <TextView id="@+id/text1"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>

      然后保存,R.java下将会自动刷新。

      第六步
      改变继承类
      public class Notepadv1 extends ListActivity

      第七步:
      看一下这三个事件:
      onCreate():界面初始化的时候调用
      onCreateOptionsMenu():按了Menu按钮的时候调用
      onOptionsItemSelected() :选择了一个菜单项的时候调用

      第八步:
      改写OnCreate函数:
       
          private DBHelper dbHelper;

          @Override
          public void onCreate(Bundle icicle)
          {
              super.onCreate(icicle);
              setContentView(R.layout.notepad_list);
              dbHelper = new DBHelper(this);
              fillData();
          }


      第九步:
      在strings.xml 中增加:<string name="menu_insert">Add Item</string>

      并在Notepadv1类中加入:public static final int INSERT_ID = Menu.FIRST;

      改写onCreateOptionsMenu()
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              boolean result = super.onCreateOptionsMenu(menu);
              menu.add(0, INSERT_ID, R.string.menu_insert);
              return result;
          }


      第十步:
      改写onOptionsItemSelected()
      @Override
          public boolean onOptionsItemSelected(Item item) {
              switch (item.getId()) {
              case INSERT_ID:
                  createNote();
                  break;
              }
            
              return super.onOptionsItemSelected(item);
          }

      第十一步:
      实现两个函数:
      private void createNote() {
              String noteName = "Note " + noteNumber++;
              dbHelper.createRow(noteName, "");
              fillData();
          }

      private void fillData() {
              // We need a list of strings for the list items
              List<String> items = new ArrayList<String>();

              // Get all of the rows from the database and create the item list
              List<Row> rows = dbHelper.fetchAllRows();
              for (Row row : rows) {
                  items.add(row.title);
              }
             
              // Now create an array adapter and set it to display using our row
              ArrayAdapter<String> notes =
                  new ArrayAdapter<String>(this, R.layout.notes_row, items);
              setListAdapter(notes);
             
          }

      第十二步:
      运行:Run As -> Android Application
       

      发表于 @ 2007年11月23日 12:02:00|评论(loading...)|编辑

      新一篇: GPhone--Android学习笔记(三):练习(2) Notepad V2 | 旧一篇: GPhone--Android学习笔记(一):Android概况及工具

      评论:没有评论。

      发表评论  


      当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
      Csdn Blog version 3.1a
      Copyright © Jun_Xu