当前位置: 编程技术>移动开发
本页文章导读:
▪ContentProvider 示例 【原创】ContentProvider 示例
package com.firewings.smstools;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import android.content.ContentProvider;
import android.content.ContentUris;
import .........
▪ 兑现在popwindow中展示listview列表 实现在popwindow中展示listview列表
之前我实现了在listview中显示图片和checkbox,具体看这儿:http://gqdy365.iteye.com/blog/992340
接下来我将上述listview添加到popupwindow窗口中。关于这个listview我就再不.........
▪ 2.3 dialog 图标小时的有关问题 2.3 dialog 图标小时的问题
发现2.3里面没有android.R.drawable.ic_dialog_xxxx 这几个图标,如果直接调用的话是不行的
看来只能从以前的复制进来了
......
[1]ContentProvider 示例
来源: 互联网 发布时间: 2014-02-18
【原创】ContentProvider 示例
package com.firewings.smstools;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteTransactionListener;
import android.net.Uri;
import android.os.Binder;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.SyncStateContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
public class SmsProvider extends ContentProvider implements SQLiteTransactionListener {
private static final String TAG = "SmsProvider";
private static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE);
private DbHelper mDbHelper;
private SQLiteDatabase mDb;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int SMS = 1000;
private static final int SMS_ID = 1001;
public static final String SMS_ITEM_TYPE = "vnd.android.cursor.item/sms";
public static final String AUTHORITY = "com.firewings.smstools";
public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
public static final HashMap<string, string=""> sCountProjectionMap;
public static final HashMap<string, string=""> smsProjectionMap;
static {
final UriMatcher matcher = sUriMatcher;
matcher.addURI(AUTHORITY, "sms", SMS);
matcher.addURI(AUTHORITY, "sms/#", SMS_ID);
sCountProjectionMap = new LinkedHashMap<string, string="">();
sCountProjectionMap.put(BaseColumns._COUNT, "COUNT(*)");
smsProjectionMap = new LinkedHashMap<string, string="">();
smsProjectionMap.put(DbHelper.SmsColumns._ID, DbHelper.SmsColumns._ID);
smsProjectionMap.put(DbHelper.SmsColumns.ADDRESS, DbHelper.SmsColumns.ADDRESS);
smsProjectionMap.put(DbHelper.SmsColumns.PERSON, DbHelper.SmsColumns.PERSON);
smsProjectionMap.put(DbHelper.SmsColumns.DATE, DbHelper.SmsColumns.DATE);
smsProjectionMap.put(DbHelper.SmsColumns.TYPE, DbHelper.SmsColumns.TYPE);
smsProjectionMap.put(DbHelper.SmsColumns.BODY, DbHelper.SmsColumns.BODY);
smsProjectionMap.put(DbHelper.SmsColumns.SEND, DbHelper.SmsColumns.SEND);
}
private final ThreadLocal<boolean> mApplyingBatch = new ThreadLocal<boolean>();
private volatile boolean mNotifyChange;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
boolean applyingBatch = applyingBatch();
if (!applyingBatch) {
mDb = mDbHelper.getWritableDatabase();
mDb.beginTransactionWithListener(this);
try {
count = deleteInTransaction(uri, selection, selectionArgs);
if (count > 0) {
mNotifyChange = true;
}
mDb.setTransactionSuccessful();
} finally {
mDb.endTransaction();
}
onEndTransaction();
} else {
count = deleteInTransaction(uri, selection, selectionArgs);
if (count > 0) {
mNotifyChange = true;
}
}
return count;
}
protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
if (VERBOSE_LOGGING) {
Log.v(TAG, "updateInTransaction: " + uri);
}
int count = 0;
final int match = sUriMatcher.match(uri);
switch (match) {
case SMS: {
count = mDb.delete(DbHelper.Tables.SMS, selection, selectionArgs);
break;
}
case SMS_ID: {
long smsId = ContentUris.parseId(uri);
if (selection != null) {
selectionArgs = selectionArg(selectionArgs, String.valueOf(smsId));
count = mDb.delete(DbHelper.Tables.SMS, RawContacts._ID + "=?" + " AND (" + selection + ")", selectionArgs);
} else {
count = mDb.delete(DbHelper.Tables.SMS, RawContacts._ID + "=?", new String[] { String.valueOf(smsId) });
}
break;
}
default: {
throw new UnsupportedOperationException(exceptionMessage(uri));
}
}
return count;
}
@Override
public String getType(Uri uri) {
final int match = sUriMatcher.match(uri);
switch (match) {
case SMS:
return SMS_ITEM_TYPE;
default:
throw new IllegalArgumentException(exceptionMessage(uri));
}
}
/**
* Returns a detailed exception message for the supplied URI. It includes
* the calling user and calling package(s).
*/
public String exceptionMessage(Uri uri) {
return exceptionMessage(null, uri);
}
/**
* Returns a detailed exception message for the supplied URI. It includes
* the calling user and calling package(s).
*/
public String exceptionMessage(String message, Uri uri) {
StringBuilder sb = new StringBuilder();
if (message != null) {
sb.append(message).append("; ");
}
sb.append("URI: ").append(uri);
final PackageManager pm = getContext().getPackageManager();
int callingUid = Binder.getCallingUid();
sb.append(", calling user: ");
String userName = pm.getNameForUid(callingUid);
if (userName != null) {
sb.append(userName);
} else {
sb.append(callingUid);
}
final String[] callerPackages = pm.getPackagesForUid(callingUid);
if (callerPackages != null && callerPackages.length > 0) {
if (callerPackages.length == 1) {
sb.append(", calling package:");
sb.append(callerPackages[0]);
} else {
sb.append(", calling package is one of: [");
for (int i = 0; i < callerPackages.length; i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(callerPackages[i]);
}
sb.append("]");
}
}
return sb.toString();
}
private boolean applyingBatch() {
return mApplyingBatch.get() != null && mApplyingBatch.get();
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri result = null;
boolean applyingBatch = applyingBatch();
if (!applyingBatch) {
mDb = mDbHelper.getWritableDatabase();
mDb.beginTransactionWithListener(this);
try {
result = insertInTransaction(uri, values);
if (result != null) {
mNotifyChange = true;
}
mDb.setTransactionSuccessful();
} finally {
mDb.endTransaction();
}
onEndTransaction();
} else {
result = insertInTransaction(uri, values);
if (result != null) {
mNotifyChange = true;
}
}
return result;
}
protected Uri insertInTransaction(Uri uri, ContentValues values) {
if (VERBOSE_LOGGING) {
Log.v(TAG, "insertInTransaction: " + uri + " " + values);
}
final int match = sUriMatcher.match(uri);
long id = 0;
switch (match) {
case SMS: {
values.putNull(DbHelper.SmsColumns._ID);
id = mDb.insert(DbHelper.Tables.SMS, DbHelper.SmsColumns._ID, values);
break;
}
default: {
throw new UnsupportedOperationException(exceptionMessage(uri));
}
}
if (id < 0) {
return null;
}
return ContentUris.withAppendedId(uri, id);
}
@Override
public boolean onCreate() {
try {
return initialize();
} catch (RuntimeException e) {
Log.e(TAG, "Cannot start provider", e);
return false;
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if (VERBOSE_LOGGING) {
Log.v(TAG, "query: " + uri);
}
final SQLiteDatabase db = mDbHelper.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String groupBy = null;
String limit = getLimit(uri);
final int match = sUriMatcher.match(uri);
switch (match) {
case SMS: {
setTablesAndProjectionMapForSms(qb, uri);
break;
}
}
return query(db, qb, projection, selection, selectionArgs, sortOrder, groupBy, limit);
}
private Cursor query(final SQLiteDatabase db, SQLiteQueryBuilder qb, String[] projection, String selection, String[] selectionArgs, String sortOrder, String groupBy, String limit) {
if (projection != null && projection.length == 1 && BaseColumns._COUNT.equals(projection[0])) {
qb.setProjectionMap(sCountProjectionMap);
}
final Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, null, sortOrder, limit);
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), AUTHORITY_URI);
}
return c;
}
private void setTablesAndProjectionMapForSms(SQLiteQueryBuilder qb, Uri uri) {
StringBuilder sb = new StringBuilder();
sb.append(DbHelper.Tables.SMS);
qb.setTables(sb.toString());
qb.setProjectionMap(smsProjectionMap);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
boolean applyingBatch = applyingBatch();
if (!applyingBatch) {
mDb = mDbHelper.getWritableDatabase();
mDb.beginTransactionWithListener(this);
try {
count = updateInTransaction(uri, values, selection, selectionArgs);
if (count > 0) {
mNotifyChange = true;
}
mDb.setTransactionSuccessful();
} finally {
mDb.endTransaction();
}
onEndTransaction();
} else {
count = updateInTransaction(uri, values, selection, selectionArgs);
if (count > 0) {
mNotifyChange = true;
}
}
return count;
}
protected int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
if (VERBOSE_LOGGING) {
Log.v(TAG, "updateInTransaction: " + uri);
}
int count = 0;
final int match = sUriMatcher.match(uri);
switch (match) {
case SMS: {
count = mDb.update(DbHelper.Tables.SMS, values, selection, selectionArgs);
break;
}
case SMS_ID: {
long smsId = ContentUris.parseId(uri);
if (selection != null) {
selectionArgs = selectionArg(selectionArgs, String.valueOf(smsId));
count = mDb.update(DbHelper.Tables.SMS, values, RawContacts._ID + "=?" + " AND (" + selection + ")", selectionArgs);
} else {
count = mDb.update(DbHelper.Tables.SMS, values, RawContacts._ID + "=?", new String[] { String.valueOf(smsId) });
}
break;
}
default: {
throw new UnsupportedOperationException(exceptionMessage(uri));
}
}
return count;
}
/**
* Inserts an argument at the beginning of the selection arg list.
*/
private String[] selectionArg(String[] selectionArgs, String arg) {
if (selectionArgs == null) {
return new String[] { arg };
} else {
int newLength = selectionArgs.length + 1;
String[] newSelectionArgs = new String[newLength];
newSelectionArgs[0] = arg;
System.arraycopy(selectionArgs, 0, newSelectionArgs, 1, selectionArgs.length);
return newSelectionArgs;
}
}
private boolean initialize() {
final Context context = getContext();
mDbHelper = (DbHelper) DbHelper.getInstance(context);
return true;
}
private String getLimit(Uri uri) {
String limitParam = getQueryParameter(uri, "limit");
if (limitParam == null) {
return null;
}
// make sure that the limit is a non-negative integer
try {
int l = Integer.parseInt(limitParam);
if (l < 0) {
Log.w(TAG, "Invalid limit parameter: " + limitParam);
return null;
}
return String.valueOf(l);
} catch (NumberFormatException ex) {
Log.w(TAG, "Invalid limit parameter: " + limitParam);
return null;
}
}
/* package */static String getQueryParameter(Uri uri, String parameter) {
String query = uri.getEncodedQuery();
if (query == null) {
return null;
}
int queryLength = query.length();
int parameterLength = parameter.length();
String value;
int index = 0;
while (true) {
index = query.indexOf(parameter, index);
if (index == -1) {
return null;
}
index += parameterLength;
if (queryLength == index) {
return null;
}
if (query.charAt(index) == '=') {
index++;
break;
}
}
int ampIndex = query.indexOf('&', index);
if (ampIndex == -1) {
value = query.substring(index);
} else {
value = query.substring(index, ampIndex);
}
return Uri.decode(value);
}
public void onBegin() {
// TODO Auto-generated method stub
}
public void onCommit() {
// TODO Auto-generated method stub
}
public void onRollback() {
// TODO Auto-generated method stub
}
protected void onEndTransaction() {
if (mNotifyChange) {
mNotifyChange = false;
notifyChange(true);
}
}
protected void notifyChange(boolean syncToNetwork) {
getContext().getContentResolver().notifyChange(AUTHORITY_URI, null, syncToNetwork);
}
}
[2] 兑现在popwindow中展示listview列表
来源: 互联网 发布时间: 2014-02-18
实现在popwindow中展示listview列表
private
void
popAwindow(View parent) {
if
(window ==
null
) {
LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lay.inflate(R.layout.popupwindow, null
);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_view));
//初始化按钮
submit = (Button) v.findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
cancel = (Button) v.findViewById(R.id.cancel);
cancel.setOnClickListener(cancelListener);
//初始化listview,加载数据。
list=(ListView)v.findViewById(R.id.lv);
MyAdapter adapter=new
MyAdapter(Main.
this
);
list.setAdapter(adapter);
list.setItemsCanFocus(false
);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listClickListener);
window = new
PopupWindow(v,
500
,
260
);
}
//设置整个popupwindow的样式。
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
//使窗口里面的空间显示其相应的效果,比较点击button时背景颜色改变。
//如果为false点击相关的空间表面上没有反应,但事件是可以监听到的。
//listview的话就没有了作用。
window.setFocusable(true
);
window.update();
window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0
,
0
);
}
OnItemClickListener listClickListener = new
OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
ViewHolder vHollder = (ViewHolder) view.getTag();
// 在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。
vHollder.cBox.toggle();
MyAdapter.isSelected.put(position, vHollder.cBox.isChecked());
}
};
OnClickListener submitListener =
new
OnClickListener() {
@Override
public
void
onClick(View v) {
//这儿可以写提交数据的代码。
closeWindow();
}
};
OnClickListener cancelListener=new
OnClickListener(){
@Override
public
void
onClick(View v){
closeWindow();
}
};
private
void
closeWindow(){
//将每个checkbox的标记改为false,以便下次弹出window时是初始的状态。
for
(
int
j =
0
; j < MyAdapter.isSelected.size(); j++) {
MyAdapter.isSelected.put(j, false
);
ViewHolder vHollder = (ViewHolder) list.getChildAt(j).getTag();
vHollder.cBox.setChecked(false
);
}
//提交数据时关闭popupwindow。
if
(window !=
null
) {
window.dismiss();
}
}
<?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="fill_parent"
>
<TextView
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18dip"
android:background="@drawable/rounded_corners_list"
android:text="这是一个popupWindow的例子"
/>
<!-- 如果layout_width的值为fill_parent时,居中要用android:gravity="center"
-->
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners_list"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners_list"
>
<Button
android:id="@+id/submit"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="提交"
/>
<Button
android:id="@+id/cancel"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#ffffffff"
/>
<stroke android:width="3dp"
color=
"#ffff8080"
/>
<corners android:radius="10dp"
/>
<padding
android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp"
/>
</shape>
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lmain"
>
<Button
android:id="@+id/myButton1"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="显示"
/>
<Button
android:id="@+id/myButton2"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="隐藏"
/>
</LinearLayout>
OnClickListener bPop =
new
OnClickListener() {
@Override
public
void
onClick(View v) {
popAwindow(v);
}
};
OnClickListener boff = new
OnClickListener() {
@Override
public
void
onClick(View v) {
if
(window!=
null
){
window.dismiss();
}
}
};
之前我实现了在listview中显示图片和checkbox,具体看这儿:http://gqdy365.iteye.com/blog/992340
接下来我将上述listview添加到popupwindow窗口中。关于这个listview我就再不多说了,主要是实现popupwindow和美化popupwindow。
先看看我做截图:
1、创建一个popupwindow,并设置相应的样式。
Java代码
给按钮添加监听事件:
Java代码
在layout中新建popupwindow.xml文件,具体内容如下,主要是对window的布局:
Java代码
其中listView的布局的布局和实现请参考文章开头提到的另一篇文章。
新建rounded_corners_pop.xml,用于自定义窗口的样式文件,具体内容如下:
Java代码
这个就可以实现圆角的样式,周围的白边是通过在白的样式上面叠加黑色的来实现的。
其他样式文件大家可以参考上面的rounded_corners_pop.xml自己写。
2、在main.xml中添加按钮,一个用于显示window,一个用于隐藏window
Java代码
在activity中初始化这两个按钮,并添加监听事件:
Java代码
[3] 2.3 dialog 图标小时的有关问题
来源: 互联网 发布时间: 2014-02-18
2.3 dialog 图标小时的问题
发现2.3里面没有android.R.drawable.ic_dialog_xxxx 这几个图标,如果直接调用的话是不行的
看来只能从以前的复制进来了
最新技术文章: