android:development:sample001
サンプルソース
画面遷移、パラメータ取得、DB検索、セル風表示、etcのサンプル
package ui.flow03;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SecActivity extends Activity {
private ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
Intent intent = getIntent();
// パラメータ取得
String data = intent.getStringExtra("keyword");
//
list = (ListView)findViewById(R.id.list);
//
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
map = new HashMap<String, String>();
// タイトルセット
map.put("No", "No");
map.put("Name", "Name");
map.put("Tel", "Tel");
map.put("Age", "Age");
listItem.add(map);
// DB接続
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db=dbHelper.getReadableDatabase();
String query = "SELECT No, Name, Tel, Age " +
"FROM MyTable " +
"WHERE Name like '%" + data + "%'";
try{
// クエリ実行
Cursor cursor = db.rawQuery(query, null);
// レコードが1件以上あればtrue、レコードが0件ならfalse
boolean isEof = cursor.moveToFirst();
while(isEof){
map = new HashMap<String, String>();
// getColumnIndexでカラムのインデックス(何カラム目)取得
// getStringで指定カラムの値を取得
map.put("No", cursor.getString(cursor.getColumnIndex("No")));
map.put("Name", cursor.getString(cursor.getColumnIndex("Name")));
map.put("Tel", cursor.getString(cursor.getColumnIndex("Tel")));
map.put("Age", cursor.getString(cursor.getColumnIndex("Age")));
listItem.add(map);
// 次のレコードがあればtrue、なければ(今の処理が最終レコードなら)false
isEof = cursor.moveToNext();
}
} finally {
db.close();
}
//
SimpleAdapter adapter = new SimpleAdapter (this.getBaseContext(),
listItem,
R.layout.affichageitem02,
new String[] {"Name", "Tel", "Age"},
new int[] {R.id.name, R.id.tel, R.id.age});
// ListViewにセット
list.setAdapter(adapter);
//
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
// どうしても警告を取り除けない事情がある場合にのみ使うアノテーション
@SuppressWarnings("unchecked")
// ListViewの項目がクリックされたら
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> map = (HashMap<String, String>) list.getItemAtPosition(position);
// インテントの定義
Intent intent = new Intent(SecActivity.this, ThirdActivity.class);
// パラメータをセットする
intent.putExtra("personal", map.get("Name") + ":" + map.get("Tel") + ":" + map.get("Age"));
// 次画面に遷移する
startActivity(intent);
}
});
// 検索条件表示用
TextView text02 = (TextView)findViewById(R.id.textView04_id);
text02.setText("検索条件:" + data);
// 戻るボタン
Button btn = (Button)findViewById(R.id.button2_id);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// インテントを終了して元の画面に戻る
finish();
}
});
}
}
android/development/sample001.txt · 最終更新: 2025/02/16 13:53 by 127.0.0.1
