Android、java初心者です。皆様のお知恵をお貸しください
簡単なメモアプリを作ろうと考えています。メモした内容をデータベースに保存してそのデータをListで表示させたのですがうまくいきません。
・登録ボタンで登録
・検索ボタンを押すと画面遷移(このときintentを渡しています)して一覧表示(List)
のようにしたいと考えています。
SimpleCursorAdapterを使うとは思うのですが使い方がいまいちわかりません。どうか表示の方法を教えてください。よろしくお願いします
MemoSQliteHelperクラス
public class MemoSQLiteHelper extends SQLiteOpenHelper {
private static final int version = 1;
private static final String name ="reportmemo.db";
public MemoSQLiteHelper(Context context) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS Memo" + "(content INTEGER PRIMARY KEY ,memo TEXT);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
NewmemoActivityクラス
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class NewmemoActivity extends Activity implements View.OnClickListener {
private MemoSQLiteHelper MSSQLhelper = null; private EditText txtmemo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_newmemo); Button enterbtn = (Button) findViewById(R.id.memoEnterBtn); enterbtn.setOnClickListener(this); Button search = (Button) findViewById(R.id.memoSearchBtn); search.setOnClickListener(this); txtmemo = (EditText) findViewById(R.id.memoEntryEdit); MSSQLhelper = new MemoSQLiteHelper(NewmemoActivity.this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.memoEnterBtn://登録 enter(); break; case R.id.memoSearchBtn://検索 Intent intent = new Intent(getApplicationContext(), MemosearchActivity.class);//検索画面に遷移します。 intent.putExtra("検索","1"); startActivity(intent); break; } } //登録 private void enter() { ContentValues cv = new ContentValues(); SQLiteDatabase db = MSSQLhelper.getWritableDatabase(); cv.put("memo", txtmemo.getText().toString()); long rt = db.insert("Memo", null, cv); db.close(); if (0 < rt) { Toast.makeText(this, "追加できました", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "追加できませんでした", Toast.LENGTH_LONG).show(); } }
}
MemosearchActivityクラス
public class MemosearchActivity extends Activity implements View.OnClickListener {
private MemoSQLiteHelper MSSQLhelper = null; private ListView memolist; private int Search; private SimpleCursorAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_memosearch); Intent intent = getIntent();//intetnを受け取る Search = Integer.parseInt(intent.getStringExtra("検索")); switch (Search) { case 1://このときlistloadメソッドを作ります。 listload(); break; } } private void listload() { String tablename = "Memo"; String[] cols = null; String selection = null; String[] selectionArgs = null; String groupBy = null; String having = null; String orderBy = null; memolist = (ListView) findViewById(R.id.memoList); MSSQLhelper = new MemoSQLiteHelper(MemosearchActivity.this); SQLiteDatabase sdb = MSSQLhelper.getReadableDatabase(); Cursor cursor = sdb.query("reportmemo", null, null, null, null, null, null); /*String[] columns = new String[]{"_content", "memo" }; String[] headers = new String[]{"content", "memo"}; int[] layouts = new int[]{android.R.id.text1, android.R.id.text2}; adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, cursor, headers, layouts); memolist.setAdapter(adapter);*/ /*memolist.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[], new int[]{R.id.id, R.id.date, R.id.weight}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER)); sdb.close();*/ ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 ); while (cursor.moveToNext()) { adapter.add(String.valueOf(cursor.getInt(0))); adapter.add(cursor.getString(1)); memolist.setAdapter(adapter); //終了処理 cursor.close(); sdb.close(); } } @Override public void onClick(View v) { }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/08 06:07
2015/07/08 06:33