当前位置: 编程技术>移动开发
本页文章导读:
▪透过NSInputStream读取大文件的指定起始内容 通过NSInputStream读取大文件的指定起始内容
转载自:http://www.cocoachina.com/bbs/read.php?tid-60598.html
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
[fileHandle seekToFileOffset:1000];
NSData *d.........
▪ ListView兑现简单的过滤,排序等控制 ListView实现简单的过滤,排序等控制
ArrayAdapter实际上已经实现了过滤,排序等功能,只是ArrayAdapter太弱了,我们通常不去用它。本人参考了ArrayAdapter源码写的一个adpater,可以有以下功能:.........
▪ ListView兑现简单的关键字高亮显示 ListView实现简单的关键字高亮显示
这个是网上找的.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import andr.........
[1]透过NSInputStream读取大文件的指定起始内容
来源: 互联网 发布时间: 2014-02-18
通过NSInputStream读取大文件的指定起始内容
转载自:http://www.cocoachina.com/bbs/read.php?tid-60598.html
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; [fileHandle seekToFileOffset:1000]; NSData *data = [fileHandle readDataOfLength:2000];
[2] ListView兑现简单的过滤,排序等控制
来源: 互联网 发布时间: 2014-02-18
ListView实现简单的过滤,排序等控制
ArrayAdapter实际上已经实现了过滤,排序等功能,只是ArrayAdapter太弱了,我们通常不去用它。
本人参考了ArrayAdapter源码写的一个adpater,可以有以下功能:
1,排序(最普通的String排序)
2,过滤(头匹配过滤,尾匹配过滤,任意位置匹配过滤)
3,指定返回需要的项数
3,可以指定其中不允许操作的Item
虽然该类功能也不算强(事实上也没办法强,很多东西都得自定义,无法涵盖所有情况),但总算实现了基本的功能,大家可以根据自己的实际情况在此基础上改。
ArrayAdapter源码:http://gundumw100.iteye.com/admin/blogs/1023902
ArrayAdapter实际上已经实现了过滤,排序等功能,只是ArrayAdapter太弱了,我们通常不去用它。
本人参考了ArrayAdapter源码写的一个adpater,可以有以下功能:
1,排序(最普通的String排序)
2,过滤(头匹配过滤,尾匹配过滤,任意位置匹配过滤)
3,指定返回需要的项数
3,可以指定其中不允许操作的Item
虽然该类功能也不算强(事实上也没办法强,很多东西都得自定义,无法涵盖所有情况),但总算实现了基本的功能,大家可以根据自己的实际情况在此基础上改。
ArrayAdapter源码:http://gundumw100.iteye.com/admin/blogs/1023902
package com.ql.app; import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; import android.widget.TextView; public class App extends Activity { List<String> dataList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dataList=Array2List(mStrings); // ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, dataList); //用法 ControllableSimpleAdapter adapter=new ControllableSimpleAdapter(this,dataList); adapter.getFilter().filter("!"); adapter.setMax(ControllableSimpleAdapter.ALL); adapter.setNeedSort(true); adapter.setFilterType(ControllableSimpleAdapter.FILTER_BODY); ListView listview=(ListView)findViewById(R.id.listview); listview.setAdapter(adapter); } /** * 数组转成List * @param <T> * @param tArray * @return */ public static <T extends Object> List<T> Array2List(T[] tArray) { List<T> tList = Arrays.asList(tArray); // List<T> tList = new ArrayList<T>(Arrays.asList(tArray)); return tList; } /** * 可控制的简单Adapter * @author admin * */ private class ControllableSimpleAdapter extends BaseAdapter implements Filterable{ public static final int ALL=-1; //全部需要 public static final int FILTER_HEAD=0;//从头开始过滤 public static final int FILTER_FOOT=1;//从后开始过滤 public static final int FILTER_BODY=2;//任意过滤 private Context context; private List<String> list; private boolean needSort=false;//是否需要排序 private ArrayFilter filter;//过滤器 private int max=ALL;//最多需要的项,默认全部需要 private int filterType = FILTER_HEAD;//过滤类别,默认从头开始过滤 public ControllableSimpleAdapter(Context context,List<String> list) { this.context = context; this.list=list; } //返回false后Item间的分割线消失 @Override public boolean areAllItemsEnabled() { return false; } //带有“-”的不可操作,通常和areAllItemsEnabled一起使用。 @Override public boolean isEnabled(int position) { return !list.get(position).startsWith("-");//此处根据需求灵活处理 } public int getCount() { return list.size(); } public Object getItem(int position) { return list.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TextView tv; if (convertView == null) { tv = (TextView) LayoutInflater.from(context).inflate( android.R.layout.simple_expandable_list_item_1, parent, false); } else { tv = (TextView) convertView; } tv.setText(list.get(position)); return tv; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public boolean isNeedSort() { return needSort; } public void setNeedSort(boolean needSort) { this.needSort = needSort; } private List<String> dosort(List<String> list) { Collections.sort(list, comparator); return list; } private Comparator<String> comparator = new Comparator<String>() { private final Collator collator = Collator.getInstance(); public int compare(String s1, String s2) { return collator.compare(s1, s2); } }; @Override public Filter getFilter() { // TODO Auto-generated method stub if (filter == null) { filter = new ArrayFilter(); } return filter; } public int getFilterType() { return filterType; } public void setFilterType(int filterType) { this.filterType = filterType; } public int getMax() { return max; } public void setMax(int max) { this.max = max; } private class ArrayFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { // TODO Auto-generated method stub FilterResults results = new FilterResults(); if (prefix == null || prefix.length() == 0) {//没有过滤符就不过滤 //new ArrayList<String>()表示让ListView一开始的时候什么都没有,而不是全部显示到ListView中 //new ArrayList<String>(list)表示一开始就让Item全部显示到ListView中 ArrayList<String> l = new ArrayList<String>(list); results.values = l; results.count = l.size(); } else { if(needSort&&!list.isEmpty()){//如果需要过滤的话,需要先排序,后过滤,可能会影响性能。 dosort(list); } String prefixString = prefix.toString().toLowerCase(); final ArrayList<String> values = new ArrayList<String>(list); final int count = values.size(); final ArrayList<String> newValues = new ArrayList<String>(count); for (int i = 0; i < count; i++) { final String value = values.get(i);//原始字符串 final String valueText = value.toString().toLowerCase(); // 匹配核心代码,需要自己实现,这里只提供了基本的过滤类型 switch (filterType) { case FILTER_HEAD: if (!valueText.startsWith(prefixString)) {//过滤掉不需要的项 newValues.add(value); } break; case FILTER_FOOT: if (!valueText.endsWith(prefixString)) {//过滤掉不需要的项 newValues.add(value); } break; case FILTER_BODY: if (valueText.indexOf(prefixString)==-1) {//过滤掉不需要的项 newValues.add(value); } break; default: break; } if(max>ALL){//不是全部 if(newValues.size()>max-1){//超过了就舍去 break; } } } results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { // TODO Auto-generated method stub list = (List<String>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } } //test data private String[] mStrings = { "----------", "Doppelrhamstufel", "Dorset Blue Vinney", "----------", "Babybel", "----------", "Caerphilly", "----------", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "!Afuega'l Pitu", "!Airag", "!Airedale", "!Aisy Cendre", "!Allgauer Emmentaler", "!Alverca", "Ambert", "American Cheese!", "Ami du Chambertin!", "Anejo Enchilado!", "Anneau du Vic-Bilh", "Anthoriro", }; }
[3] ListView兑现简单的关键字高亮显示
来源: 互联网 发布时间: 2014-02-18
ListView实现简单的关键字高亮显示
这个是网上找的.
这个是网上找的.
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.os.CountDownTimer; import android.text.Editable; import android.text.Html; import android.text.Spanned; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.SlidingDrawer; import android.widget.TextView; public class DemoHomeActivity extends Activity { private static final String TAG = "DemoHomeActivity"; private SlidingDrawer mDrawer; private TextView input_tv; private EndClosedMyDrawer mClosedDrawer; private ListView listview; private String [] test = new String[]{"010668971455","020132456","01058585896","02168000000","02168000001","1372160485","195369871"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); mClosedDrawer = new EndClosedMyDrawer(5000,1000); new UpdateListTask().execute(getTextNumber()); } protected void onRestart() { new UpdateListTask().execute(getTextNumber()); super.onRestart(); } public void init(){ mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer); input_tv = (TextView)findViewById(R.id.input_dialpad_tv); listview = (ListView)findViewById(R.id.listview); input_tv.addTextChangedListener(watcher); } private TextWatcher watcher = new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { new UpdateListTask().execute(getTextNumber()); } }; private void keyPressed ( int keyCode ){ mDrawer.open(); try{ mClosedDrawer.cancel(); }catch (Exception e) { Log.i(TAG, "This is the first run updatelist Countdown!"); } mClosedDrawer.start(); } public boolean onKeyDown(int keyCode, KeyEvent event){ switch (keyCode){ case KeyEvent.KEYCODE_0 : keyPressed(KeyEvent.KEYCODE_0); input_tv.append("0"); break; case KeyEvent.KEYCODE_1 : keyPressed(KeyEvent.KEYCODE_1); input_tv.append("1"); break; case KeyEvent.KEYCODE_2 : keyPressed(KeyEvent.KEYCODE_2); input_tv.append("2"); break; case KeyEvent.KEYCODE_3 : keyPressed(KeyEvent.KEYCODE_3); input_tv.append("3"); break; case KeyEvent.KEYCODE_4 : keyPressed(KeyEvent.KEYCODE_4); input_tv.append("4"); break; case KeyEvent.KEYCODE_5 : keyPressed(KeyEvent.KEYCODE_5); input_tv.append("5"); break; case KeyEvent.KEYCODE_6 : keyPressed(KeyEvent.KEYCODE_6); input_tv.append("6"); break; case KeyEvent.KEYCODE_7 : keyPressed(KeyEvent.KEYCODE_7); input_tv.append("7"); break; case KeyEvent.KEYCODE_8 : keyPressed(KeyEvent.KEYCODE_8); input_tv.append("8"); break; case KeyEvent.KEYCODE_9 : keyPressed(KeyEvent.KEYCODE_9); input_tv.append("9"); break; case KeyEvent.KEYCODE_STAR : keyPressed(KeyEvent.KEYCODE_STAR); input_tv.append("*"); break; case KeyEvent.KEYCODE_POUND : keyPressed(KeyEvent.KEYCODE_POUND); input_tv.append("#"); break; case KeyEvent.KEYCODE_DEL: keyPressed(KeyEvent.KEYCODE_DEL); CharSequence str = ""; str = input_tv.getText(); if(str != null && str.length() > 0){ String temp = str.toString(); if(temp.length() == 1){ input_tv.setText(""); }else if(temp.length() > 1){ input_tv.setText(temp.subSequence(0, temp.length()-1)); } } break; default: return super.onKeyDown(keyCode, event); } return true; } /** * Customize Adapter, so that realize highlighted keywords */ private class HightKeywordsAdapter extends BaseAdapter{ private List list; private Context context; private String[] from; private int[] to; private int layoutid; private HashMap info; LayoutInflater myInflater; HashMap item; public HightKeywordsAdapter(Context context, List list, int layoutid, String[] from,int[] to){ this.context = context; this.list = list; this.from = from; this.to = to; this.layoutid = layoutid; } public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { myInflater = LayoutInflater.from(context); try { item = (HashMap) list.get(position); convertView = myInflater.inflate(layoutid, null); convertView.setTag(item); }catch (Exception e) { Log.e(TAG, "Hight Key Error! "); } View number = convertView.findViewById(to[0]); if (number instanceof TextView) { TextView number_tv = (TextView) number; number_tv = (TextView) number; String number_temp = ""; number_temp = item.get(from[0]).toString(); String input = input_tv.getText().toString(); if(number_temp.contains(input)){ //Methods 1 int index = number_temp.indexOf(input); int len = input.length(); Spanned temp = Html.fromHtml(number_temp.substring(0, index) + "<u><font color=#5DB43B>" + number_temp.substring(index, index + len) + "</font></u>" + number_temp.substring(index + len, number_temp.length())); //Methods 2 /*int start = number.indexOf(input); SpannableStringBuilder style=new SpannableStringBuilder(number); style.setSpan(new Tex(Color.RED), start, start + input.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); */ number_tv.setText(temp); } } return convertView; } } private synchronized List updateNumberslist(String input) { List list = new ArrayList<HashMap<String, String>>(); if(input == null) { for(int i=0;i<test.length;i++){ HashMap<String, String> number_info = new HashMap<String, String>(); number_info.put("number", test[i]); list.add(number_info); } } else { for(int i=0;i<test.length;i++){ if(test[i].contains(input)){ HashMap<String, String> number_info = new HashMap<String, String>(); number_info.put("number", test[i]); list.add(number_info); } } } return list; } private String getTextNumber(){ if(input_tv.getText() != null && input_tv.getText().length() > 0){ return input_tv.getText().toString(); }else{ return null; } } private class UpdateListTask extends AsyncTask<String, Integer, BaseAdapter> { protected BaseAdapter doInBackground(String... params) { BaseAdapter listAdapter = new HightKeywordsAdapter(DemoHomeActivity.this, updateNumberslist(getTextNumber()), android.R.layout.simple_list_item_1, new String[] {"number" }, new int[] {android.R.id.text1 }); return listAdapter; } protected void onPostExecute(BaseAdapter result) { listview.setAdapter(result); } } //计数器 private class EndClosedMyDrawer extends CountDownTimer { public EndClosedMyDrawer(long millisInFuture, long countDownInterval){ super(millisInFuture, countDownInterval); } public void onFinish(){ //Log.i(TAG, "Start asyncTask to updateList !"); mDrawer.close(); } public void onTick(long millisUntilFinished){ } } }
最新技术文章: