今天在http://android-developers.blogspot.com/2012/05/using-dialogfragments.html上看到使用DialogFragments才知道原來要在fragment的畫面上呈現一個Dialog這麼簡單,但網路上可以查到的資料好像還是相當少,附上簡單的實作DialogFragments連結,一看就懂了。
先附上官網的效果圖...
點左上角File的Download就可以下載囉
DialogFragments範例下載
2012年5月12日 星期六
2012年5月4日 星期五
Android Sqlite 開關問題
一直以來使用Sqlite開關方面,一直出現問題譬如以下錯誤畫面
05-04 14:14:50.870: E/AndroidRuntime(7173): java.lang.IllegalStateException: database /data/data/etwarm.namespace/databases/Super_data.db (conn# 0) already closed
或
close() was never explicitly called on database
等等等...........
這類的錯誤(close訊息)都只是上個db沒有關閉又開了一次,譬如開了一次getReadableDatabase()又再開了一次getReadableDatabase(),這樣在DDMS就會看到錯誤訊息,雖然不至於影響整個應用程序,但看到錯誤訊息心理就不是很舒服。
個人在程序上的方式如下,自己筆記一下。
//這個應用程序有使用2個db,先在最上頭宣告為null值
private DBOpenHelper dbHelper=null;
private Customer_DBOpenHelper customer_dbHelper=null;
SQLiteDatabase db=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//宣告
dbHelper = new DBOpenHelper(this);
customer_dbHelper = new Customer_DBOpenHelper(this);
}
//使用2個public,當dbHelper開時,customer_dbHelper就關,customer_dbHelper要開時dbHelper就關
public void db_help_open(){
if(customer_dbHelper!=null){customer_dbHelper.close();}
db = dbHelper.getReadableDatabase();
}
public void customer_db_help_open(){
if(dbHelper!=null){dbHelper.close();}
db = customer_dbHelper.getReadableDatabase();
}
//當離開畫面時呼叫all_cursor_db_close()這個自己寫的方法
public void onPause() {
// TODO Auto-generated method stub
all_cursor_db_close();
super.onPause();
}
//把他全部關閉
public void all_cursor_db_close(){
if(cursor!=null){cursor=null;}
if(customer_dbHelper != null){customer_dbHelper.close();}
if(dbHelper != null){dbHelper.close();}
}
如果各位有更好的做法及建議,歡迎各位共同討論!!
2012年5月1日 星期二
Index -1 requested, with a size of 1的問題原因
今天在使用Sqlite的cursor時出現以下錯誤訊息
05-02 02:54:56.928: E/AndroidRuntime(1660): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
再來看一下官方文件
public abstract int getColumnIndex (String columnName)
Since: API Level 1
Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use
getColumnIndexOrThrow(String) instead, which will make the error more clear.if(cursor.getCount()>0)
{
while(cursor.moveToNext())
{
String test=cursor.getString(cursor.getColumnIndex("customer_want"));
Log.i("Tag","這是cursor取得的值:"+test.toString());
}
}
2012年4月30日 星期一
Failure delivering result ResultInfo問題原因
使用返回按鍵回上個Activity時遇到了以下錯誤。
Failure delivering result ResultInfo{who=android:fragment:2, request=0, result=0, data=null} to activity {etwarm.namespace/etwarm.buy.fragment.buy_home_main}: java.lang.NullPointerException
我們從錯誤訊息可以觀察到data=null所以錯誤了,那data在哪呢?其實就是返回的那個Activity有使用到onActivityResult,只要判斷一下data這個是否為空即可,如if(data!=null ),請參考以下。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//把勾選要自訂的物件回傳
super.onActivityResult(requestCode, resultCode, data);
if(data!=null ){
if(requestCode == SELECT_OJ_CODE)
{
String select_oj_id=data.getStringExtra("SELECT_OJ");
if(!select_oj_id.equals("")){
Log.i("Tag", select_oj_id.toString());
dao.update_want(table_name, test_tmp, select_oj_id);
}
}
}
}
2012年4月25日 星期三
cannot be cast to android.widget.錯誤
今天很簡單的只是把checkbox調到textview的前面卻出現以下問題。
04-25 15:49:03.441: E/AndroidRuntime(15710): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.CheckBox
解決方式很令人意外的重新命名checkbox的id後又好了,真是奇怪。
04-25 15:49:03.441: E/AndroidRuntime(15710): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.CheckBox
解決方式很令人意外的重新命名checkbox的id後又好了,真是奇怪。
2012年4月23日 星期一
JDOM搭配HttpClient Post
今天要把Android上的某字串以XML的方式POST給Server,使用了JDOM(要先下載\並匯入.jar檔)將訊息建立成XML格式後存入字串變數,再以Post的方式給Server,如以下:
package com.fff;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.output.XMLOutputter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class JOM_POST_Test1Activity extends Activity {
/** Called when the activity is first created. */
String aa="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
BuildXMLDoc();
} catch (Exception e) {
e.printStackTrace();
}
final String TAG = "ApacheHttpClientTest";
String response = "";
String url = "http://10.40.204.137:8080/project/test";
List params = new ArrayList();
params.add(new BasicNameValuePair("id",aa));
params.add(new BasicNameValuePair("name","kuka"));
try
{
response = httpPost(url, params);
} catch (Exception e)
{
e.printStackTrace();
}
Log.i(TAG, "####"+response);
}
public void BuildXMLDoc() throws IOException, JDOMException {
List list = new ArrayList();
List list2 = new ArrayList();
list.add(0, "1111111111"); list2.add(0, "11,22,33,44,55");
list.add(1, "2222222222"); list2.add(1, "11,22,33,44,55");
list.add(2, "3333333333"); list2.add(2, "11,22,33,44,55");
Element root = new Element("list");
Document Doc = new Document(root);
Element elements;
XMLOutputter outputter = new XMLOutputter();
for(小於list容量時)
{
elements= new Element("user");
elements.setAttribute("id", "" + i);
elements.addContent(new Element("_id").setText(list.get(i)));
elements.addContent(new Element("oj_want").setText(list2.get(i)));
root.addContent(elements);
aa+= outputter.outputString(elements);
}
System.out.println(aa);
/*
//如要產生XML的檔案使用以下
XMLOutputter XMLOut = new XMLOutputter();
XMLOut.output(Doc, new FileOutputStream("user.xml"));
*/
}
public String httpPost(String url, List params) throws Exception
{
String response = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try
{
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = httpclient.execute(httppost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK)
{
response = EntityUtils.toString(httpResponse.getEntity());
}
else
{
response = "錯誤"+statusCode;
}
}catch (Exception e)
{
e.printStackTrace();
}
return response;
}
}
2012年4月18日 星期三
使用addTextChangedListener偵測EditTex
今天想做個Edittext輸入自動搜尋,原本使用setOnKeyListener的時候,輸入中文都要再按一次完成才會即時才會擷取,結果發現addTextChangedListener更好用呢,一輸入就即時更新字串,跟WhatsApp的搜尋聯絡人一樣,很即時搜尋唷。
package comfff.fff;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Test_setOnKeyActivity extends Activity {
/** Called when the activity is first created. */
private EditText myEditText01;
private TextView myTextView01;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText01 = (EditText)findViewById(R.id.EditText01);
myTextView01 = (TextView)findViewById(R.id.TextView01);
myEditText01.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
System.out.println("beforeTextChanged");
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
System.out.println("onTextChanged");
if(myEditText01.getText().length()==0)
{
myTextView01.setText("空白");
}
else{
myTextView01.setText(myEditText01.getText());
}
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("afterTextChanged");
}
});
}
}
訂閱:
文章 (Atom)
星期、月份英文縮寫
中文 英文 3字母 縮寫 3字母 縮寫 2字母 縮寫 2字母 縮寫 1字母 縮寫 1字母 縮寫 其他 縮寫 星期日 Sunday SUN Sun SU Su S S 星期一 Monday MON Mon MO Mo M M 星期二 Tuesday TUE Tue TU ...
-
買車、買房、投資、貸款 算一下未來價值,未來負債,使用"圖表"方便看 每個人都該對錢的利息和利率有概念的必裝軟體! 讓你簡單換算利率,利息,並顯示圖表查看 買車、買房、投資、貸款 單利率、複利率、...
-
官方blog就有非常好的介紹,之前還因為這個問題很煩惱,結果根本不難 http://android-developers.blogspot.tw/2011/07/new-tools-for-managing-screen-sizes.html
-
要把經緯度店址可以使用以下作法 Geocoder gc = new Geocoder(getActivity(), Locale.TRADITIONAL_CHINESE); List<Address> lstAddress = gc.getFromLocation...
