当前位置: 编程技术>移动开发
本页文章导读:
▪SectionIndexer 的施用(联系人分类索引) SectionIndexer 的使用(联系人分类索引)
// 获取标题栏索引
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true; .........
▪ xcode4 设置调试异常信息小结 xcode4 设置调试错误信息小结
原文出处:http://blog.csdn.net/cococoolwhj/article/details/6944572soulutions: 1. 设置环境变量NSZombieEnabled-YES 2.设置环境变量MallocStackLogging-YES 出错的时候shell malloc_histor.........
▪ NSPredicate及正则表达式的施用心得 分享 NSPredicate及正则表达式的使用心得 分享
判断字符串首字母是否为字母。Objective-c代码NSString *regex = @"[A-Za-z]+";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];if ([predicate evalu.........
[1]SectionIndexer 的施用(联系人分类索引)
来源: 互联网 发布时间: 2014-02-18
SectionIndexer 的使用(联系人分类索引)
// 获取标题栏索引
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true;
}
// 设置调整到指定区域
list.setSelection(position);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<android.test.SideBar
android:id = "@+id/sideBar"
android:layout_height="fill_parent"
android:layout_width="22px"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="80sp"
android:textSize="45sp"
/>
</LinearLayout>
package android.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter implements SectionIndexer {
private ArrayList<String> stringArray;
private Context context;
public MyAdapter(Context _context, ArrayList<String> arr) {
stringArray = arr;
context = _context;
}
public int getCount() {
return stringArray.size();
}
public Object getItem(int arg0) {
return stringArray.get(arg0);
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflate = ((Activity) context).getLayoutInflater();
View view = (View) inflate.inflate(R.layout.listview_row, null);
LinearLayout header = (LinearLayout) view.findViewById(R.id.section);
String label = stringArray.get(position);
char firstChar = label.toUpperCase().charAt(0);
if (position == 0) {
setSection(header, label);
} else {
String preLabel = stringArray.get(position - 1);
char preFirstChar = preLabel.toUpperCase().charAt(0);
if (firstChar != preFirstChar) {
setSection(header, label);
} else {
header.setVisibility(View.GONE);
}
}
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(label);
return view;
}
private void setSection(LinearLayout header, String label) {
TextView text = new TextView(context);
header.setBackgroundColor(0xffaabbcc);
text.setTextColor(Color.WHITE);
text.setText(label.substring(0, 1).toUpperCase());
text.setTextSize(20);
text.setPadding(5, 0, 0, 0);
text.setGravity(Gravity.CENTER_VERTICAL);
header.addView(text);
}
public int getPositionForSection(int section) {
if (section == 35) {
return 0;
}
for (int i = 0; i < stringArray.size(); i++) {
String l = stringArray.get(i);
char firstChar = l.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
public int getSectionForPosition(int arg0) {
return 0;
}
public Object[] getSections() {
return null;
}
}
package android.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class SideBar extends View {
private char[] l;
private SectionIndexer sectionIndexter = null;
private ListView list;
private final int m_nItemHeight = 29;
public SideBar(Context context) {
super(context);
init();
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
setBackgroundColor(0x44FFFFFF);
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setListView(ListView _list) {
list = _list;
sectionIndexter = (SectionIndexer) _list.getAdapter();
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / m_nItemHeight;
if (idx >= l.length) {
idx = l.length - 1;
} else if (idx < 0) {
idx = 0;
}
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
if (sectionIndexter == null) {
sectionIndexter = (SectionIndexer) list.getAdapter();
}
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true;
}
list.setSelection(position);
}
return true;
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xFFA6A9AA);
paint.setTextSize(20);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / 2;
for (int i = 0; i < l.length; i++) {
canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);
}
super.onDraw(canvas);
}
}
package android.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.myListView);
ArrayList<String> stringList = InitListViewData();
MyAdapter adapter = new MyAdapter(this, stringList);
list.setAdapter(adapter);
SideBar indexBar = (SideBar) findViewById(R.id.sideBar);
indexBar.setListView(list);
}
private ArrayList<String> InitListViewData() {
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("aback");
stringList.add("abash");
stringList.add("abbey");
stringList.add("abhor");
stringList.add("abide");
stringList.add("abuse");
stringList.add("candidate");
stringList.add("capture");
stringList.add("careful");
stringList.add("catch");
stringList.add("cause");
stringList.add("celebrate");
stringList.add("forever");
stringList.add("fable");
stringList.add("fidelity");
stringList.add("fox");
stringList.add("funny");
stringList.add("fail");
stringList.add("jail");
stringList.add("jade");
stringList.add("jailor");
stringList.add("january");
stringList.add("jasmine");
stringList.add("jazz");
stringList.add("zero");
stringList.add("zoo");
stringList.add("zeus");
stringList.add("zebra");
stringList.add("zest");
stringList.add("zing");
return stringList;
}
}
1. Main.xml
2. listview_row.xml
3. MyAdapter.java
4. SideBar.java
5. Main.java
[2] xcode4 设置调试异常信息小结
来源: 互联网 发布时间: 2014-02-18
xcode4 设置调试错误信息小结
原文出处:
http://blog.csdn.net/cococoolwhj/article/details/6944572
soulutions:
1. 设置环境变量NSZombieEnabled-YES
2.设置环境变量MallocStackLogging-YES
出错的时候shell malloc_history pid address
pid: ProcessID
address: 变量的内存地址
原文出处:
http://blog.csdn.net/cococoolwhj/article/details/6944572
soulutions:
1. 设置环境变量NSZombieEnabled-YES
2.设置环境变量MallocStackLogging-YES
出错的时候shell malloc_history pid address
pid: ProcessID
address: 变量的内存地址
[3] NSPredicate及正则表达式的施用心得 分享
来源: 互联网 发布时间: 2014-02-18
NSPredicate及正则表达式的使用心得 分享
判断字符串首字母是否为字母。
Objective-c代码
NSString *regex = @"[A-Za-z]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:aString]) {
}
NSString *regex = @"[A-Za-z]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:aString]) {
}
判断Array中是否包含某一规则的对象,并返回一个数组:
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", regex];
并调用:- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法即可。 本文来自织梦
获得一个数组中某些对象除外的数组:
NSPredicate *notPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter2];且还是要调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法。
同样,如果我们想找出某个范围内的对象,创建如下Predicate (这里可以用到所有的比较操作符): NSPredicate *pre = [NSPredicate predicateWithFormat:@"self.*** < 5"];
并调用:- (BOOL)evaluateWithObject:(id)object;方法。
在这里啰嗦一句,如果只是在数组中查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound.
字符串替换:
Objective-c代码
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(encoding=\")[^\"]+(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = [regex stringByReplacingMatchesInString:sample
options:0
range:NSMakeRange(0, sample.length)
withTemplate:@"$1utf-8$2"];
NSLog(@"Result:%@", result);
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(encoding=\")[^\"]+(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = [regex stringByReplacingMatchesInString:sample
options:0
range:NSMakeRange(0, sample.length)
withTemplate:@"$1utf-8$2"];
NSLog(@"Result:%@", result);
from:http://lovelysimon.blog.sohu.com/190483498.html
判断字符串首字母是否为字母。
Objective-c代码
NSString *regex = @"[A-Za-z]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:aString]) {
}
NSString *regex = @"[A-Za-z]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:aString]) {
}
判断Array中是否包含某一规则的对象,并返回一个数组:
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", regex];
并调用:- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法即可。 本文来自织梦
获得一个数组中某些对象除外的数组:
NSPredicate *notPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter2];且还是要调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法。
同样,如果我们想找出某个范围内的对象,创建如下Predicate (这里可以用到所有的比较操作符): NSPredicate *pre = [NSPredicate predicateWithFormat:@"self.*** < 5"];
并调用:- (BOOL)evaluateWithObject:(id)object;方法。
在这里啰嗦一句,如果只是在数组中查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound.
字符串替换:
Objective-c代码
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(encoding=\")[^\"]+(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = [regex stringByReplacingMatchesInString:sample
options:0
range:NSMakeRange(0, sample.length)
withTemplate:@"$1utf-8$2"];
NSLog(@"Result:%@", result);
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(encoding=\")[^\"]+(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = [regex stringByReplacingMatchesInString:sample
options:0
range:NSMakeRange(0, sample.length)
withTemplate:@"$1utf-8$2"];
NSLog(@"Result:%@", result);
from:http://lovelysimon.blog.sohu.com/190483498.html
最新技术文章: