标签:android style http java 使用 strong
package com.example.testmyviewslistscursorpeople; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.database.Cursor; import android.net.LocalSocketAddress.Namespace; import android.os.Bundle; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.text.GetChars; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.example.testmyviewslistsactivateitems.R; /** * * @author Administrator 仿效果slow loading apiDemos -- Views -Lists - Slow Adapter */ public class Main extends ListActivity { private boolean mBusy = false; public static List<String> mStrings = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getPeople(); setListAdapter(new SlowAdapter(this)); // 设置选择模式为单选 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 首次加载设置选中items getListView().setItemChecked(0, true); getListView().setOnScrollListener(new OnScrollListener()); } protected class OnScrollListener implements ListView.OnScrollListener { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { // The view is not scrolling. case OnScrollListener.SCROLL_STATE_IDLE: mBusy = false; int first = view.getFirstVisiblePosition(); int count = view.getChildCount(); for (int i = 0; i < count; i++) { TextView t = (TextView) view.getChildAt(i); if (t.getTag() != null) { t.setText(mStrings.get(first + i)); t.setTag(null); } } break; // The user is scrolling using touch, and their finger is still on // the screen case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: mBusy = true; break; // The user had previously been scrolling using touch and had // performed a fling. // The animation is now coasting to a stop case OnScrollListener.SCROLL_STATE_FLING: mBusy = true; break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { getListView().setItemChecked(position, true); } // 自定义适配器 private class SlowAdapter extends BaseAdapter { private LayoutInflater mInflater; public SlowAdapter(Context context) { mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return mStrings.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView text; if (convertView == null) { text = (TextView) mInflater.inflate(R.layout.main, null, false); } else { text = (TextView) convertView; } if (!mBusy) { text.setText(mStrings.get(position).toString()); text.setTag(null); } else { text.setText("Loading..."); text.setTag(this); } return text; } } protected void getPeople() { final String[] CONTACT_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, }; Cursor mCursor = getContentResolver().query(Contacts.CONTENT_URI, CONTACT_PROJECTION, null, null, null); while(mCursor.moveToNext()) { String name = mCursor.getString(mCursor.getColumnIndex(Contacts.DISPLAY_NAME)); mStrings.add(name); } mCursor.close(); } }
protected void getPeople() { final String[] CONTACT_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME }; //联系人 Cursor mCursor = getContentResolver().query(Contacts.CONTENT_URI, CONTACT_PROJECTION, null, null, null); //联系人电话 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID, null, null); while (mCursor.moveToNext()) { // 获取联系人 String name = mCursor.getString(mCursor .getColumnIndex(Contacts.DISPLAY_NAME)); mStrings.add(name); if (phones.moveToNext()) { //获取电话 String phone = phones.getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); mStrings.add(phone); } } //关闭资源 mCursor.close(); phones.close(); }
Apidemos-->Views-Lists-Cursor(people)学习,布布扣,bubuko.com
Apidemos-->Views-Lists-Cursor(people)学习
标签:android style http java 使用 strong
原文地址:http://blog.csdn.net/aikongmeng/article/details/37656781