package cnc.Light;
import java.util.*;
import android.app.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class Light extends Activity {
LightDBHelper mHelper;
Cursor cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHelper = new LightDBHelper(this);
SQLiteDatabase db = mHelper.getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM Light", null);
startManagingCursor(cursor);
SimpleCursorAdapter Adapter = null;
Adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor, new String[] {"place"},
new int[] {android.R.id.text1});
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(Adapter);
final Context ctx = this;
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(ctx, Detail.class);
intent.putExtra("data_id", Long.toString(id));
intent.putExtra("data_id", cursor.getString(cursor.getColumnIndex("id")));
startActivity(intent);
}
});
}
}
class LightDBHelper extends SQLiteOpenHelper{
public LightDBHelper(Context context){
super(context, "Light.db", null, 1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE Light(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"place TEXT, contents TEXT);");
db.execSQL("INSERT INTO Light VALUES (NULL, '학교', '조도기준은 100');");
db.execSQL("INSERT INTO Light VALUES (NULL, '병원', '조도기준은 110');");
db.execSQL("INSERT INTO Light VALUES (NULL, '교실', '조도기준은 120');");
db.execSQL("INSERT INTO Light VALUES (NULL, '몰라', '조도기준은 130');");
db.execSQL("INSERT INTO Light VALUES (NULL, '알아', '조도기준은 140');");
db.execSQL("INSERT INTO Light VALUES (NULL, '안다구', '조도기준은 150');");
db.execSQL("INSERT INTO Light VALUES (NULL, '안가르쳐조', '조도기준은 160');");
db.execSQL("INSERT INTO Light VALUES (NULL, '젠장', '조도기준은 170');");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS Light");
onCreate(db);
}
}
id를 보내주고
package cnc.Light;
import java.util.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
public class Detail extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Intent intent = getIntent();
int id = intent.getIntExtra("data_id", 0); //전달된 값을 받음
TextView text_title = (TextView) findViewById(R.id.text2); //id값이 제대로 전달 되었는지 찍어주는 곳입니다.
// text_title.setText(id);
}
}
id를 받으려고 하는데 리스트뷰에서 아이템을 클릭하면 오류가 뜹니다.
해결방법좀부탁드릴께요



