android 学习随笔二十八(应用小知识点小结 )

  • 去掉标题栏的方法

第一种:也一般入门的时候经常使用的一种方法
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
注意这句一定要写在setContentView()方法的前面,不然会报错的

第二种:在AndroidManifest.xml文件中定义
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
可以看出,这样写的话,整个应用都会去掉标题栏,如果只想去掉某一个Activity的标题栏的话,可以把这个属性加到activity标签里面

注:这种方法在android4.1.1的三星i9300手机上TabHost中不起作用,需要采用第一种方式加上setTheme一起控制。
setTheme(android.R.style.Theme_Black_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.api_tab_host);

第三种:自定义样式,在一般的应用中不常用,在res/values目录下面新建一个style.xml的文件
例如:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="notitle">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
相当于一个主题,然后在AndroidManifest.xml文件中定义

<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/notitle">
达到去掉标题栏的效果

第一种有的时候我们会看到,会先出现标题栏,然后再消失,因为是在activity的oncreate方法中定义的。
第二种相对第一种比较好一些,不会出现这种情况。
第三种可以把功能分开,便于维护和扩展,但比较麻烦。

------------------------------------------------------

  • 全屏的方法

第一种
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

第二种
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

第三种
application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/fullscreem"

  • ListView自动滑动

<activity android:name="com.itheima.momo.ui.activity.ConversationDetailActivity"
android:windowSoftInputMode="stateUnspecified|adjustResize"
></activity>

public void initView() {
setContentView(R.layout.activity_conversation_detail);

lv_conversation_detail = (ListView) findViewById(R.id.lv_conversation_detail);
et_conversation_detail = (EditText) findViewById(R.id.et_conversation_detail);
bt_conversation_detail_send = (Button) findViewById(R.id.bt_conversation_detail_send);

//只要ListView刷新,就会滑动
lv_conversation_detail.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
}

  • 发送短信与接收发送结果
package com.itheima.momo.dao;

import java.util.List;

import com.itheima.momo.globle.Constant;
import com.itheima.momo.receiver.SendSmsReceiver;

import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;

public class SmsDao {

    public static void sendSms(Context context, String address, String body){
        SmsManager manager = SmsManager.getDefault();
        List<String> smss = manager.divideMessage(body);
        
        Intent intent = new Intent(SendSmsReceiver.ACTION_SEND_SMS);
        //短信发出去后,系统会发送一条广播,告知我们短信发送是成功还是失败
        PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        for (String text : smss) {
            //这个api只负责发短信,不会把短信写入数据库
            manager.sendTextMessage(address, null, text, sentIntent, null);
        
            //把短信插入短信数据库
            insertSms(context, address, text);
        }
    }
    
    public static void insertSms(Context context, String address, String body){
        //只要插入address就可以了,thread_id不需要插入,这个会自动识别的。 
        ContentValues values = new ContentValues();
        values.put("address", address);
        values.put("body", body);
        values.put("type", Constant.SMS.TYPE_SEND);
        
        context.getContentResolver().insert(Constant.URI.URI_SMS, values);
    }
}
View Code
package com.itheima.momo.receiver;

import com.itheima.momo.utils.ToastUtils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class SendSmsReceiver extends BroadcastReceiver {
    
    public static final String ACTION_SEND_SMS = "com.itheima.momo.sendsms";
    @Override
    public void onReceive(Context context, Intent intent) {
        int code = getResultCode();
        if(code == Activity.RESULT_OK ){
            ToastUtils.ShowToast(context, "发送成功");
        }
        else{
            ToastUtils.ShowToast(context, "发送失败");
        }
    }

}
View Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.momo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.momo.ui.activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name="com.itheima.momo.ui.activity.ConversationDetailActivity"
            android:windowSoftInputMode="stateUnspecified|adjustResize"
            ></activity>
        
        <activity android:name="com.itheima.momo.ui.activity.NewMsgActivity"></activity>
        
        <receiver android:name="com.itheima.momo.receiver.SendSmsReceiver">
            <intent-filter >
                <action android:name="com.itheima.momo.sendsms"/>
            </intent-filter>
        </receiver>
        
        <provider android:name="com.itheima.momo.provider.GroupProvider"
            android:exported="true"
            android:authorities="com.itheima.momo"
            ></provider>
    </application>

</manifest>
View Code
  •  利用接口定义常量

public class Constant {

public interface URI{
Uri URI_SMS_CONVERSATION = Uri.parse("content://sms/conversations");
Uri URI_SMS = Uri.parse("content://sms");
Uri URI_GROUP_INSERT = Uri.parse("content://com.itheima.momo/groups/insert");
}
public interface SMS{
int TYPE_RECEIVE = 1;
int TYPE_SEND = 2;
}
}

  • 创建数据库
package com.itheima.momo.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class GroupOpenHelper extends SQLiteOpenHelper {

    //单例模式获取GroupOpenHelper实例
    private static GroupOpenHelper instance;
    
    public static GroupOpenHelper getInstance(Context context){
        if(instance == null){
            instance = new GroupOpenHelper(context, "group.db", null, 1);
        }
        return instance;
    }
    
    private GroupOpenHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建groups表
        db.execSQL("create table groups(" +
                "_id integer primary key autoincrement, " +
                "name varchar, " +
                "create_date integer, " +
                "thread_count integer" + 
                ")");
        //创建会话和群组的映射表
        db.execSQL("create table thread_group(" +
                "_id integer primary key autoincrement, " +
                "group_id integer, " +
                "thread_id integer" + 
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}
View Code
  • 定义内容提供者
public class GroupProvider extends ContentProvider {

    GroupOpenHelper helper;
    private SQLiteDatabase db;
    
    static final int CODE_GROUPS_INSERT = 0;
    
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    
    private final String authority = "com.itheima.momo";
    {
        //添加匹配规则
        //authority是主机名,//path是要匹配的路径
        matcher.addURI(authority , "groups/insert", CODE_GROUPS_INSERT);
    }
    @Override
    public boolean onCreate() {
        helper = GroupOpenHelper.getInstance(getContext());
        db = helper.getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        switch (matcher.match(uri)) {
        case CODE_GROUPS_INSERT:
            long rawId= db.insert("groups", null, values);
            //插入失败
            if(rawId == -1){
                return null;
            }
            else{
                //把返回的行id,拼接在uri的后面,然后返回
                return ContentUris.withAppendedId(uri, rawId);
            }
            
        default:
            break;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }

}
GroupProvider.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.momo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.momo.ui.activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name="com.itheima.momo.ui.activity.ConversationDetailActivity"
            android:windowSoftInputMode="stateUnspecified|adjustResize"
            ></activity>
        
        <activity android:name="com.itheima.momo.ui.activity.NewMsgActivity"></activity>
        
        <receiver android:name="com.itheima.momo.receiver.SendSmsReceiver">
            <intent-filter >
                <action android:name="com.itheima.momo.sendsms"/>
            </intent-filter>
        </receiver>
        
        <provider android:name="com.itheima.momo.provider.GroupProvider"
            android:exported="true"
            android:authorities="com.itheima.momo"
            ></provider>
    </application>

</manifest>
AndroidManifest.xml
import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.itheima.momo.R;
import com.itheima.momo.base.BaseFragment;
import com.itheima.momo.globle.Constant;

public class GroupFragment extends BaseFragment {

    private Button bt_group_newgroup;

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_group, null);
        bt_group_newgroup = (Button) view.findViewById(R.id.bt_group_newgroup);
        return view;
    }

    @Override
    public void initListener() {
        bt_group_newgroup.setOnClickListener(this);

    }

    @Override
    public void initData() {
        // TODO Auto-generated method stub

    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
        case R.id.bt_group_newgroup:
            ContentValues values = new ContentValues();
            values.put("name", "天下英雄基本也就这几个了");
            values.put("create_date", System.currentTimeMillis());
            values.put("thread_count", 0);
            getActivity().getContentResolver().insert(Constant.URI.URI_GROUP_INSERT, values);
            break;

        }

    }

}
GroupFragment.java
  •  自动下拉列表条
package com.itheima.momo.adapter;

import com.itheima.momo.R;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class AutoSearchAdapter extends CursorAdapter {

    @SuppressWarnings("deprecation")
    public AutoSearchAdapter(Context context, Cursor c) {
        super(context, c);
    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return View.inflate(context, R.layout.item_auto_search_tv, null);
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = getHolder(view);
        holder.tv_autosearch_name.setText(cursor.getString(cursor.getColumnIndex("display_name")));
        holder.tv_autosearch_address.setText(cursor.getString(cursor.getColumnIndex("data1")));
    }
    
    private ViewHolder getHolder(View view) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if(holder == null){
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        return holder;
    }
    
    class ViewHolder{
        private TextView tv_autosearch_name;
        private TextView tv_autosearch_address;

        public ViewHolder(View view) {
            tv_autosearch_name = (TextView) view.findViewById(R.id.tv_autosearch_name);
            tv_autosearch_address = (TextView) view.findViewById(R.id.tv_autosearch_address);
        }
    }
    
    //点击下拉列表条目时的返回值
    @Override
    public CharSequence convertToString(Cursor cursor) {
        return cursor.getString(cursor.getColumnIndex("data1"));
    }
}
AutoSearchAdapter.java
package com.itheima.momo.ui.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.TextView;

import com.itheima.momo.R;
import com.itheima.momo.adapter.AutoSearchAdapter;
import com.itheima.momo.base.BaseActivity;
import com.itheima.momo.dao.SmsDao;
import com.itheima.momo.utils.CursorUtils;
import com.itheima.momo.utils.ToastUtils;

public class NewMsgActivity extends BaseActivity {

    //这个组件显示联系人列表也是使用Adapter来设置列表条目
    private AutoCompleteTextView et_newmsg_address;
    private EditText et_newmsg_body;
    private AutoSearchAdapter adapter;
    private ImageView iv_newmsg_select_contact;
    private Button bt_newmsg_send;

    @Override
    public void initView() {
        setContentView(R.layout.activity_newmsg);
        et_newmsg_address = (AutoCompleteTextView) findViewById(R.id.et_newmsg_address);
        et_newmsg_body = (EditText) findViewById(R.id.et_newmsg_body);
        iv_newmsg_select_contact = (ImageView) findViewById(R.id.iv_newmsg_select_contact);
        bt_newmsg_send = (Button) findViewById(R.id.bt_newmsg_send);
        
        //这是下拉列表的背景
        et_newmsg_address.setDropDownBackgroundResource(R.drawable.bg_btn_normal);
        //这是下拉列表的竖直偏移
        et_newmsg_address.setDropDownVerticalOffset(5);
    }

    @Override
    public void initListener() {
        iv_newmsg_select_contact.setOnClickListener(this);
        bt_newmsg_send.setOnClickListener(this);
    }

    @Override
    public void initData() {
        adapter = new AutoSearchAdapter(this, null);
        //给输入框设置adapter,该adapter负责显示输入框的下拉列表
        et_newmsg_address.setAdapter(adapter);
        
        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            
            //这个方法的调用,是用来执行查询
            //constraint:用户在输入框中输入的号码,也就是模糊查询的条件
            @Override
            public Cursor runQuery(CharSequence constraint) {
                String[] projection = {
                        "data1",
                        "display_name",
                        "_id"
                };
                //模糊查询
                String selection = "data1 like '%" + constraint + "%'";
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null);
//                CursorUtils.printCursor(cursor);
                
                //返回cursor,就是把cursor交给adapter
                return cursor;
            }
        });
        
        initTitleBar();

    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
        case R.id.iv_titlebar_back_btn:
            finish();
            break;
        case R.id.iv_newmsg_select_contact:
            //跳转至系统提供的联系人选择Activity
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("vnd.android.cursor.dir/contact");
            //使用startActivityForResult启动,那么选好联系人,该Activity销毁,回调onActivityResult
            startActivityForResult(intent, 0);
            break;
        case R.id.bt_newmsg_send:
            String address = et_newmsg_address.getText().toString();
            String body = et_newmsg_body.getText().toString();
            if(!TextUtils.isEmpty(address) && !TextUtils.isEmpty(body)){
                SmsDao.sendSms(this, address, body);
            }
            break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //data中会携带一个uri,就是用户选择的联系人的uri
        Uri uri = data.getData();
        if(uri != null){
            //查询这个uri,获取联系人的id和是否有号码
            String[] projection = {
                    "_id",
                    "has_phone_number"
            };
            //不需要where条件,因为uri是“一个”联系人的uri
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            //不需要判断是否查到,但是必须移动指针
            cursor.moveToFirst();
            String _id = cursor.getString(0);
            int has_phone_number = cursor.getInt(1);
            
            if(has_phone_number == 0){
                ToastUtils.ShowToast(this, "该联系人没有号码");
            }
            else{
                //如果有号码,拿着联系人id去Phone.CONTENT_URI查询号码
                String selection = "contact_id = " + _id;
                Cursor cursor2 = getContentResolver().query(Phone.CONTENT_URI, new String[]{"data1"}, selection, null, null);
                cursor2.moveToFirst();
                String data1 = cursor2.getString(0);
                
                et_newmsg_address.setText(data1);
                //内容输入框获取焦点
                et_newmsg_body.requestFocus();
            }
        }
    }
    
    private void initTitleBar() {
        findViewById(R.id.iv_titlebar_back_btn).setOnClickListener(this);
        ((TextView)findViewById(R.id.tv_titlebar_title)).setText("发送短信");

    }
}
NewMsgActivity.java
  •  对话框默认不支持文本输入,手动把一个输入框设置为对话框的内容,让android自动弹出输入键盘

//本类中提供静态方法,获取本类的对象。
public static void showDialog(Context context, String title, OnInputDialogListener onInputDialogListener){
InputDialog dialog = new InputDialog(context, title, onInputDialogListener);
//对话框默认不支持文本输入,手动把一个输入框设置为对话框的内容,Android自动对其进行设置(设置的文本框会被布局文件覆盖)
dialog.setView(new EditText(context));
dialog.show();
}

  • TextView添加图标
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@drawable/selector_item_grouplist_bg"
    >
    <!--最左边的图片就是孤零零的图片上面没有加上任何的功能,所以直接在textview中利用属性实现  -->
    <TextView 
        android:id="@+id/tv_grouplist_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="群组名字"
        android:textSize="16sp"
        android:drawableLeft="@drawable/img_group"
        android:drawablePadding="5dp"
        android:gravity="center_vertical"
        />
    <TextView 
        android:id="@+id/tv_grouplist_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:78pm"
        android:textSize="15sp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />
    
</RelativeLayout>
View Code
  •  改变ListView的点击背景

1、去掉原来的背景

<ListView
android:id="@+id/lv_group_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:listSelector="@android:color/transparent"
></ListView>

2、设置ITEM的背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/selector_item_grouplist_bg"
>
<!--最左边的图片就是孤零零的图片上面没有加上任何的功能,所以直接在textview中利用属性实现 -->
<TextView
android:id="@+id/tv_grouplist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="群组名字"
android:textSize="16sp"
android:drawableLeft="@drawable/img_group"
android:drawablePadding="5dp"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/tv_grouplist_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:78pm"
android:textSize="15sp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>

</RelativeLayout>

----------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@color/item_grouplist_normal" android:state_pressed="false"></item>
<item android:drawable="@color/item_grouplist_press" android:state_pressed="true"></item>

</selector>

 --------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="item_grouplist_normal">#ffffff</color>
<color name="item_grouplist_press">#33666666</color>
</resources>

  • 自定义数据库刷新

import com.itheima.momo.dao.GroupOpenHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class GroupProvider extends ContentProvider {

GroupOpenHelper helper;
private SQLiteDatabase db;
private static final String TABLE_GROUPS = "groups";
private static final String TABLE_THREAD_GROUP = "thread_group";

private static final String authority = "com.itheima.momo";
public static final Uri BASE_URI = Uri.parse("content://" + authority);

static final int CODE_GROUPS_INSERT = 0;
static final int CODE_GROUPS_QUERY = 1;
static final int CODE_GROUPS_UPDATE = 2;
static final int CODE_GROUPS_DELETE = 3;
static final int CODE_THREAD_GROUP_INSERT = 4;
static final int CODE_THREAD_GROUP_QUERY = 5;
static final int CODE_THREAD_GROUP_UPDATE = 6;
static final int CODE_THREAD_GROUP_DELETE = 7;

UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
{
//添加匹配规则
matcher.addURI(authority , "groups/insert", CODE_GROUPS_INSERT);
matcher.addURI(authority , "groups/query", CODE_GROUPS_QUERY);
matcher.addURI(authority , "groups/update", CODE_GROUPS_UPDATE);
matcher.addURI(authority , "groups/delete", CODE_GROUPS_DELETE);
matcher.addURI(authority , "thread_group/insert", CODE_THREAD_GROUP_INSERT);
matcher.addURI(authority , "thread_group/query", CODE_THREAD_GROUP_QUERY);
matcher.addURI(authority , "thread_group/update", CODE_THREAD_GROUP_UPDATE);
matcher.addURI(authority , "thread_group/delete", CODE_THREAD_GROUP_DELETE);
}
@Override
public boolean onCreate() {
helper = GroupOpenHelper.getInstance(getContext());
db = helper.getWritableDatabase();
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {

switch (matcher.match(uri)) {
//这个是表示不同的表,根据id判断出不同的表然后选择插入。
case CODE_GROUPS_QUERY:
Cursor cursor = db.query(TABLE_GROUPS, projection, selection, selectionArgs, null, null, sortOrder);

//监视uri上数据改变一个的内容观察者
//只要该uri上的数据改变,内容观察者会立刻发现,重新查询
cursor.setNotificationUri(getContext().getContentResolver(), BASE_URI);
return cursor;
case CODE_THREAD_GROUP_QUERY:
cursor = db.query(TABLE_THREAD_GROUP, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), BASE_URI);
return cursor;
default:
throw new IllegalArgumentException("未识别的uri:" + uri);
}
}

@Override
public String getType(Uri uri) {
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
switch (matcher.match(uri)) {
case CODE_GROUPS_INSERT:
long rawId = db.insert(TABLE_GROUPS, null, values);
//插入失败
if(rawId == -1){
return null;
}
else{
//这个是自己创建的数据库只能调用notifychange方法,让其帮忙刷新。
getContext().getContentResolver().notifyChange(BASE_URI, null);
//把返回的行id,拼接在uri的后面,然后返回
return ContentUris.withAppendedId(uri, rawId);
}
case CODE_THREAD_GROUP_INSERT:
rawId = db.insert(TABLE_THREAD_GROUP, null, values);
//插入失败
if(rawId == -1){
return null;
}
else{
getContext().getContentResolver().notifyChange(BASE_URI, null);
//把返回的行id,拼接在uri的后面,然后返回
return ContentUris.withAppendedId(uri, rawId);
}

default:
throw new IllegalArgumentException("未识别的uri:" + uri);
}
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (matcher.match(uri)) {
case CODE_GROUPS_DELETE:
int number = db.delete(TABLE_GROUPS, selection, selectionArgs);
getContext().getContentResolver().notifyChange(BASE_URI, null);
return number;
case CODE_THREAD_GROUP_DELETE:
number = db.delete(TABLE_THREAD_GROUP, selection, selectionArgs);
getContext().getContentResolver().notifyChange(BASE_URI, null);
return number;
default:
throw new IllegalArgumentException("未识别的uri:" + uri);
}
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (matcher.match(uri)) {
case CODE_GROUPS_UPDATE:
int number = db.update(TABLE_GROUPS, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(BASE_URI, null);
return number;
case CODE_THREAD_GROUP_UPDATE:
number = db.update(TABLE_THREAD_GROUP, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(BASE_URI, null);
return number;
default:
throw new IllegalArgumentException("未识别的uri:" + uri);
}
}

}

  • final

     在Java中声明属性、方法和类时,可使用关键字final来修饰。
     final变量即为常量,只能赋值一次;
     final方法不能被子类重写;
     final类不能被继承。
  • 编辑框设置图标

<EditText
android:id="@+id/et_search_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_btn_normal"
android:drawableLeft="@drawable/icon_search"
android:singleLine="true"
android:textSize="16sp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>

  • LIstview去掉横线和背景设置

<ListView
android:id="@+id/lv_search_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:listSelector="@android:color/transparent"
></ListView>

  • 在LISTVIEW中隐藏输入法软键盘

lv_search_list.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//输入法管理器
//隐藏输入法软键盘
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

}
});

  • 限制TextViewW文字行数,以及添加“...”

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/iv_icon"
android:ellipsize="end"
android:maxLines="2"
android:text="否决了卡萨大军阀流口水大军阀卢卡斯的家乐福卡睡大觉了看法就死定了看风景"
android:textColor="#000"
android:textSize="18sp" />

  • ImageView显示方式(对齐、裁剪centercrop等)

<ImageView
android:id="@+id/iv_icon"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#9e9e9e"
android:padding="1dp"
android:scaleType="fitXY"
android:src="@drawable/image_demo" />

 

posted @ 2016-10-19 15:52  ecollab  阅读(277)  评论(0编辑  收藏  举报