지금 제가 텍스트뷰 형식으로 리스트 뷰에 넣어주고 있는데
리스트 뷰로 바꿔서 넣을시 에러가 뜨네요...
리스트뷰로 바꿔주시거나 textview 온클릭 이벤트관련 힌트좀 부탁드립니다.
MemoListActivity 입니다
package com.andro;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MemoListActivity extends ListActivity{
ArrayAdapter<String> mAdapter;
DBManager dbmgr;
SQLiteDatabase sql;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAdapter = new ArrayAdapter<String>(getApplication(),R.layout.list_row,new ArrayList<String>());
setListAdapter(mAdapter);
Button button = (Button)findViewById(R.id.btn_add);
TextView tv = (TextView)findViewById(R.id.list_row_text);
/*
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});*/ //이부분을 추가만 하면 에러가 나는데 왜 그러는지 알 수 있을까요.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addStringData();
}
});
}
private void addStringData(){
EditText edit = (EditText)findViewById(R.id.edit_text); //EditText 값 edit 변수에 저장
mAdapter.add(edit.getText().toString()); //mAdapter에 edit변수에 저장했던 값을 스트링형으로 추가
mAdapter.notifyDataSetChanged(); //mAdapter 새로고침
try{
dbmgr = new DBManager(this); //db 생성
sql = dbmgr.getWritableDatabase();
sql.execSQL("insert into MEMO valuse('"+edit.getText().toString()+"'"+"'"+null+"');");
dbmgr.close();
} catch(SQLiteException e){}
}
}
DBManager 입니다.
package com.andro;
import android.content.Context;
import android.database.sqlite.*;
public class DBManager extends SQLiteOpenHelper{
public DBManager (Context context){
super(context,"MEMO",null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("create table memo (title text, content text);");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
list_row.xml 입니다,
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
main.xml 입니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="제 목"/>
<EditText
android:id="@+id/edit_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
<Button
android:id="@+id/btn_add"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="추가"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>