編輯:關於Android編程
下面是一個LinearLayout,裡面的內容超級簡單,就是一個ListView
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <listview android:id="@+id/Lv" android:layout_height="fill_parent" android:layout_width="fill_parent"> </listview> </linearlayout>
為了使程序更有擴展性,我這裡創建了一個model包,裡面包含一個Person類,就是數據庫的信息名稱
package com.neusoft.ListView.model; public class Person { private String _id; private String name; private String salary; private String phone; public Person(String _id, String name, String salary, String phone) { super(); this._id = _id; this.name = name; this.salary = salary; this.phone = phone; } @Override public String toString() { return _id + "," + name + "," + salary+ "," + phone; } public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }下面的這個MyOpenHelper類就是老套路了,大家都知道。他是為了創建數據庫和升級數據庫等等對數據庫的操作的類
package com.neusoft.ListView; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "people.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(_id integer primary key autoincrement,name char(20),salary char(20),phone char(20))"); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { System.out.print("數據庫升級了"); } }
package com.neusoft.ListView; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.neusoft.ListView.model.*; public class MainActivity extends Activity { private ListView Lv; ListpersonList;//創建一個List集合,將所有數據庫中每個person加入到此集合中 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //insert(); //從數據庫中取出數據 personList=new ArrayList (); MyOpenHelper oh=new MyOpenHelper(this); SQLiteDatabase db=oh.getWritableDatabase(); Cursor cursor=db.query("person",null, null, null, null, null,null); while(cursor.moveToNext()) { //循環取出每個person的值 String _id=cursor.getString(cursor.getColumnIndex("_id")); String name=cursor.getString(cursor.getColumnIndex("name")); String salary=cursor.getString(cursor.getColumnIndex("salary")); String phone=cursor.getString(cursor.getColumnIndex("phone")); Person p=new Person(_id, name, salary, phone); personList.add(p);//將值加入到集合中 } Lv=(ListView) findViewById(R.id.Lv); //將數據顯示到屏幕 Lv.setAdapter(new MyAdapter()); } //創建一個內部類MyAdapter,適配器,就是將數據顯示到屏幕的工具。 class MyAdapter extends BaseAdapter { @Override public int getCount() { return personList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { TextView tv=new TextView(MainActivity.this); Person person=personList.get(arg0); tv.setText(person.toString()); return tv; } } //剛開始插入的數據 public void insert() { MyOpenHelper oh=new MyOpenHelper(this); SQLiteDatabase db=oh.getWritableDatabase(); for(int i=0;i<100;i++) { ContentValues values=new ContentValues(); values.put("name", "張宇翔"+i); values.put("salary", "1500"+i); values.put("phone","13408"+i); db.insert("person", null, values); } } }
android中提供了4中動畫: AlphaAnimation 透明度動畫效果 ScaleAnimation 縮放動畫效果 TranslateAnimation 位移動畫
Android應用開發-小巫CSDN博客客戶端開發開篇 2014年9月8日 八月十五 祝各位中秋節快樂 小巫斷斷續續花了幾個星期的時間開發了這麼一款應用——小巫CSDN博
android 程序打包成apk,如果在是命令行方式,一般都要經過如下步驟:1.用aapt命令生成R.java文件2.用aidl命令生成相應java文件3.用javac命
RecyclerView已經出來很久了,許許多多的項目都開始從ListView轉戰RecyclerView,那麼,上拉加載和下拉刷新是一件很有必要的事情。在ListVie