各种小记录

本文总结了Android开发过程中遇到的各种问题及解决办法,包括SQLite特殊字符处理、SimpleCursorAdapter更新、ListView分割线设置、线程封装、Volley连接超时设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、sqlite中处理单引号:

所有单引号换成双单引号,如:

content.replace("'", "''");

这样是不行的,临时抱佛脚,换成了带"?"的通配形式


2、SimpleCursorAdapter 的 notifyDataSetChanged无效:

可以使用SimpleCursorAdapter 的changeCursor方法:

http://stackoverflow.com/questions/1985955/android-simplecursoradapter-not-updating-with-database-changes

http://stackoverflow.com/questions/14034770/using-notifydatasetchanged-on-simplecursoradapter-does-not-work


3、listview不显示分割线:

setDriver(null),或者在xml文件中属性设置为@null


4、sqlite 中replace into 无效:

之前写好的代码调试都通过了,今天突然发现不能运行了:sqlite,表中创建唯一索引,然后使用replace into实现插入或者更新,结果变成了只插入,折腾了几个小时,最后改掉了表中一个名为id的字段,实在是无语


5、临时写java的Thread封装:

package com.ttdevs.thread;

import android.os.Process;

public abstract class BaseThread extends Thread {

	private volatile boolean mQuit = false;

	public BaseThread() {

	}

	/**
	 * prepare if needed
	 */
	public boolean prepare() {

		return true;
	}

	/**
	 * run body
	 */
	public abstract void execute();

	/**
	 * recycle if needed
	 */
	public void recycle() {

	}

	/**
	 * stop thread
	 */
	public final void quit() {
		mQuit = true;
		interrupt();
	}

	@Override
	public void run() {
		// TODO
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		if (!prepare()) {
			return;
		}
		while (true) {
			try {
				execute();
			} catch (Exception e) {// } catch (InterruptedException e) {
				if (mQuit) {
					recycle();
					return;
				}
				continue;
			}
		}
	}
}

6、volley中设置连接超时:

设置超时的地方:
HurlStack:
    /**
     * Opens an {@link HttpURLConnection} with parameters.
     * @param url
     * @return an open connection
     * @throws IOException
     */
    private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
        HttpURLConnection connection = createConnection(url);

        int timeoutMs = request.getTimeoutMs();
        connection.setConnectTimeout(timeoutMs);
        connection.setReadTimeout(timeoutMs);
        connection.setUseCaches(false);
        connection.setDoInput(true);

        // use caller-provided custom SslSocketFactory, if any, for HTTPS
        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
        }

        return connection;
    }
HttpClientStack:
    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        onPrepareRequest(httpRequest);
        HttpParams httpParams = httpRequest.getParams();
        int timeoutMs = request.getTimeoutMs();
        // TODO: Reevaluate this connection timeout based on more wide-scale
        // data collection and possibly different for wifi vs. 3G.
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
        return mClient.execute(httpRequest);
    }
Request中:
/**
     * Sets the retry policy for this request.
     */
    public void setRetryPolicy(RetryPolicy retryPolicy) {
        mRetryPolicy = retryPolicy;
    }
RetryPolicy :
/**
 * Retry policy for a request.
 */
public interface RetryPolicy {

    /**
     * Returns the current timeout (used for logging).
     */
    public int getCurrentTimeout();

    /**
     * Returns the current retry count (used for logging).
     */
    public int getCurrentRetryCount();

    /**
     * Prepares for the next retry by applying a backoff to the timeout.
     * @param error The error code of the last attempt.
     * @throws VolleyError In the event that the retry could not be performed (for example if we
     * ran out of attempts), the passed in error is thrown.
     */
    public void retry(VolleyError error) throws VolleyError;
}

7、android 与图片相关:


8、将eclipse项目中的文件夹移除SVN的控制(添加到svn:ignore)

以删除android项目中的bin文件夹为例
a、关闭项目的自动build:project-Build Automatically
b、删除bin文件夹
c、提交
d、打开项目的自动build,这个时候会生成bin文件夹
e、将bin添加到svn:ignore:右键-Team-添加至svn:ignore
f、提交

9、屏幕旋转,activity的成员变量被全部释放?!

貌似是这样子的

10、ViewPager频繁被回收

ViewPager
public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

Parameters
limit How many pages will be kept offscreen in an idle state.


11、android SQLite 分页查询,参数为页大小和起始编号

先看数据:


select * from china_city where province = '北京' order by code limit 5 offset 0 ,查询第0条之后的五条:


select * from china_city where province = '北京' order by code limit 5 offset 5,查询第5条之后的五条:


12、获取进程内存:

int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

13、与联系人相关:


14、设置字体大小:

这里可以用setTextSize()的另外一种形式,可以指定单位:
setTextSize(int unit, int size)
TypedValue.COMPLEX_UNIT_PX : Pixels
TypedValue.COMPLEX_UNIT_SP : Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值