参考書を参考にrealmを利用してリスト表示をさせたいと考えています。
buildするとsuccessfulになるのですがエミュレーターでの実行ができません。
����:Version 10.1.1 of Realm is now available: http://static.realm.io/downloads/java/latest
����:Processing class check
����:Creating DefaultRealmModule
とエラーのようなものが表示されているのですが解決方法がいまいち分かっておりません。
分かる方いらっしゃいましたらよろしくお願いいたします。
こちらプログラムとなります。
CheckHome
1package jp.ac.aut.sos; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.content.Intent; 6import android.os.Bundle; 7import android.view.View; 8import android.widget.AdapterView; 9import android.widget.ListView; 10import android.view.MenuItem; 11import android.view.Menu; 12import org.xml.sax.helpers.XMLReaderAdapter; 13 14import io.realm.Realm; 15import io.realm.RealmResults; 16 17public class Check_home extends AppCompatActivity implements MyAdapter.DeleteListener { 18 19 private static final String[] initData = {"a","b"}; 20 private Realm mRealm; 21 private MyAdapter mAdapter; 22 private ListView mListView; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_check_home); 28 initCheck(); 29 mListView = (ListView)findViewById(R.id.listView); 30 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 31 @Override 32 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 33 Intent intent = new Intent(Check_home.this, Detail_home.class); 34 intent.putExtra("check_id",Long.toString(id)); 35 startActivity(intent); 36 } 37 }); 38 39 } 40 41 private void Check_del_OnClick(View view) { 42 Intent intent1 = new Intent(this, Check_del.class 43 ); 44 startActivity(intent1); 45 } 46 47 private void Check_add_OnClick(View view) { 48 Intent intent1 = new Intent(this, Check_add.class 49 ); 50 startActivity(intent1); 51 } 52 53 private void Back_OnClick(View view) { 54 Intent intent1 = new Intent(this, MainActivity.class 55 ); 56 startActivity(intent1); 57 } 58 59 private void initCheck(){ 60 mRealm = Realm.getDefaultInstance(); 61 RealmResults<check> checks = mRealm.where(check.class).findAll().sort("id"); 62 if(checks.size()== 0){ 63 mRealm.beginTransaction(); 64 for(int i = 0; i < initData.length;i++){ 65 check check = mRealm.createObject(check.class,i); 66 check.setCode(initData[i]); 67 } 68 mRealm.commitTransaction(); 69 } 70 71 mAdapter = new MyAdapter(this,checks); 72 mListView.setAdapter(mAdapter); 73 mAdapter.setCallback(this); 74 } 75 76 private void deleteCheck(long checkId){ 77 final long id = checkId; 78 mRealm.executeTransaction(new Realm.Transaction() { 79 @Override 80 public void execute(Realm realm) { 81 check check = realm.where(check.class).equalTo("id",id).findFirst(); 82 check.deleteFromRealm(); 83 } 84 }); 85 86 } 87 88 @Override 89 protected void onDestroy(){ 90 super.onDestroy(); 91 92 mRealm.close(); 93 } 94 95 @Override 96 public boolean onOptionsItemSelected(MenuItem item){ 97 int id = item.getItemId(); 98 if(id == R.id.action_add){ 99 Intent intent = new Intent(Check_home.this,Detail_home.class); 100 startActivity(intent); 101 102 return true; 103 } 104 return super.onOptionsItemSelected(item); 105 106 } 107 108 @Override 109 public void delete(long checkId){ 110 deleteCheck(checkId); 111 } 112} 113 114 115
DetailHome
1package jp.ac.aut.sos; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.os.Bundle; 6import android.text.TextUtils; 7import android.view.View; 8import android.view.WindowManager; 9import android.widget.EditText; 10 11import io.realm.Realm; 12import io.realm.RealmResults; 13import io.realm.Sort; 14 15public class Detail_home extends AppCompatActivity implements View.OnClickListener { 16 17 private Realm mRealm; 18 private EditText mEditCode; 19 private long mCheck_id; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_detail_home); 24 25 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 26 mEditCode = (EditText)findViewById(R.id.editText); 27 findViewById(R.id.btnOK).setOnClickListener(this); 28 findViewById(R.id.btnCancel).setOnClickListener(this); 29 30 initData(); 31 32} 33 @Override 34 public void onClick(View v){ 35 int id = v.getId(); 36 if(id == R.id.btnOK){ 37 insert(); 38 } 39 finish(); 40 } 41 42 public long nextCheckId(){ 43 RealmResults<check> results = mRealm.where(check.class).findAll().sort("id", Sort.DESCENDING); 44 if(results.size() > 0){ 45 return results.first().getId() + 1; 46 } 47 return 0; 48 } 49 50 private void initData(){ 51 52 mRealm = Realm.getDefaultInstance(); 53 54 String check_id = getIntent().getStringExtra("check_id"); 55 if(TextUtils.isEmpty(check_id)){ 56 mCheck_id = nextCheckId(); 57 }else{ 58 mCheck_id = Long.parseLong(check_id); 59 mEditCode.setText(mRealm.where(check.class).equalTo("id",mCheck_id).findFirst().getCode()); 60 mEditCode.setSelection(mEditCode.getText().length()); 61 } 62 } 63 64 public void insert(){ 65 mRealm.executeTransaction(new Realm.Transaction() { 66 @Override 67 public void execute(Realm realm) { 68 check check = new check(); 69 check.setId(mCheck_id); 70 check.setCode(mEditCode.getText().toString()); 71 realm.insertOrUpdate(check); 72 } 73 }); 74 } 75 76 @Override 77 public void onDestroy(){ 78 super.onDestroy(); 79 mRealm.close(); 80 } 81} 82
Myapplication
1package jp.ac.aut.sos; 2 3import android.app.Application; 4 5import io.realm.Realm; 6import io.realm.RealmConfiguration; 7 8 9 10public class MyApplication extends Application { 11 12 @Override 13 public void onCreate(){ 14 super.onCreate(); 15 Realm.init(this); 16 RealmConfiguration realmConfig = new RealmConfiguration.Builder().build(); 17 Realm.setDefaultConfiguration(realmConfig); 18 } 19} 20
MyAdapter
1package jp.ac.aut.sos; 2 3import android.content.Context; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import android.widget.ImageView; 8import android.widget.ListAdapter; 9import android.widget.TextView; 10 11import io.realm.OrderedRealmCollection; 12import io.realm.RealmBaseAdapter; 13 14public class MyAdapter extends RealmBaseAdapter<check> implements ListAdapter { 15 16 private Context mContext; 17 private DeleteListener mListener; 18 19 private static class ViewHolder{ 20 TextView check; 21 ImageView delete; 22 } 23 24 public void setCallback(DeleteListener callback){ 25 mListener = callback; 26 } 27 28 public interface DeleteListener{ 29 void delete(long catId); 30 } 31 32 public MyAdapter(Context context,OrderedRealmCollection<check> a){ 33 super(a); 34 this.mContext = context; 35 } 36 37 @Override 38 public long getItemId(int position){ 39 return getItem(position).getId(); 40 } 41 42 @Override 43 public View getView(final int position, View convertView, ViewGroup parent){ 44 ViewHolder viewHolder; 45 if(convertView == null){ 46 convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_row,parent,false); 47 viewHolder = new ViewHolder(); 48 viewHolder.check = (TextView)convertView.findViewById(R.id.txtName); 49 ImageView delete = (ImageView)convertView.findViewById(R.id.imageDelete); 50 delete.setTag(getItemId(position)); 51 52 delete.setOnClickListener(new View.OnClickListener(){ 53 @Override 54 public void onClick(View v){ 55 mListener.delete((long) v.getTag()); 56 } 57 }); 58 59 viewHolder.delete = delete; 60 convertView.setTag(viewHolder); 61 }else{ 62 viewHolder = (ViewHolder)convertView.getTag(); 63 viewHolder.delete.setTag(getItemId(position)); 64 } 65 viewHolder.check.setText(getItem(position).getCode()); 66 return convertView; 67 } 68 69} 70
あなたの回答
tips
プレビュー