編輯:關於Android編程
示例代碼:
這是一個讀取聯系人的代碼:
[java]
package com.ui.domain;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
public class CallPhone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.call_phone);
//聯系人列表
final List<String> dataList=new ArrayList<String>();
// 獲得所有的聯系人
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
while (cur.moveToNext()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String phoneNumber=cur.getString(1);
String disPlayName = cur.getString(displayNameColumn);
dataList.add(disPlayName+":"+phoneNumber);
Log.i("聯系人名稱:", disPlayName);
}
cur.close();//關閉游標
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(new ListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
// TODO Auto-generated method stub
EditText text=new EditText(CallPhone.this);
text.setBackgroundColor(Color.WHITE);
text.setText(dataList.get(paramInt));
return text;
}
@Override
public int getItemViewType(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public boolean isEnabled(int paramInt) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.call_phone, menu);
return true;
}
}
package com.ui.domain;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
public class CallPhone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.call_phone);
//聯系人列表
final List<String> dataList=new ArrayList<String>();
// 獲得所有的聯系人
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
while (cur.moveToNext()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String phoneNumber=cur.getString(1);
String disPlayName = cur.getString(displayNameColumn);
dataList.add(disPlayName+":"+phoneNumber);
Log.i("聯系人名稱:", disPlayName);
}
cur.close();//關閉游標
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(new ListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
// TODO Auto-generated method stub
EditText text=new EditText(CallPhone.this);
text.setBackgroundColor(Color.WHITE);
text.setText(dataList.get(paramInt));
return text;
}
@Override
public int getItemViewType(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public boolean isEnabled(int paramInt) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.call_phone, menu);
return true;
}
}
這個listView有個setAdapter 適配器,裡面可以直接實現接口,或者寫個類,實現[java] view plaincopyprint?ListAdapter
ListAdapter這個接口
[java]
?listView.setAdapter
listView.setAdapter
在定義一個list 集合 泛型為String
接口中有個方法:
[java]
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
return 就是 返回list的數據
paramInt 這是索引號
直接取list.get(索引)
這樣就可以了
還要實現上述的幾個方法
注意,如果list!>1 就會報錯
一、簡介上篇博客概括的介紹了硅谷商城項目的首頁技術要點。本篇內容給大家講解硅谷商城項目分類,分類頁面用的技術包括:采用FlycoTabLayout實現標簽和分類切換、布局
第1節 Broadcast Receiver概述很多時候,我們希望一個應用程序在它沒有運行起來的時候,也能感知系統狀態的某些變化,如果條件合適,就讓這個應用就運行起來。比
前面一篇我們介紹了常用的幾種適配器的簡單實現和ListView的簡單使用,這一篇中,我們介紹一下ListView的優化和一些其它的問題。ListView優化方法一在Lis
服務,作為Android四大組件之一,必然是重點。我們今天就來講解一下有關服務的生命周期、兩種開啟方式以及相關用法。 服務有兩種開啟方式,一種是正常開啟, 一種是以綁定的