带有SUGAR ORM的SQLite android

时间:2015-05-22 19:47:43

标签: android database sqlite

我正在使用糖orm for android数据库,我有2个问题:

  1. 我无法从带有byte []
  2. 的糖中获取图像
  3. 我无法删除数据并刷新列表
  4. logcat的一些错误:

    1.   

      SkImageDecoder :: Factory返回null

    2.   

      SQLiteQuery:SELECT * FROM BOOKMARK_MODEL WHERE id =?限制1   threadid = 1:线程退出,未捕获异常(组= 0xa62ce288)

    3. 我的代码:

      public class Bookmark extends ActionBarActivity implements AdapterView.OnItemClickListener {
      
          BookmarkAdapter adapter;
          LayoutInflater vi;
          int Resource;
          ViewHolder holder;
          ListView listBookmark;
          List<BookmarkModel> list;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_bookmark);
              getSupportActionBar().setHomeButtonEnabled(true);
              getSupportActionBar().setDisplayHomeAsUpEnabled(true);
              list = BookmarkModel.listAll(BookmarkModel.class);
              listBookmark = (ListView)findViewById(R.id.listBookmark);
              adapter = new BookmarkAdapter(this, R.layout.item_bookmark, list);
              listBookmark.setAdapter(adapter);
              TextView empty = (TextView)findViewById(R.id.textView);
              listBookmark.setEmptyView(empty);
              listBookmark.setOnItemClickListener(this);
          }
      
      
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              getMenuInflater().inflate(R.menu.menu_bookmark, menu);
              return true;
          }
      
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              int id = item.getItemId();
      
              if (id == R.id.action_settings) {
                  return true;
              }else{
                  finish();
              }
      
              return super.onOptionsItemSelected(item);
          }
      
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              final long position = i + 1;
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.setTitle("Hapus Data");
              builder.setMessage("Apa kamu yakin untuk menghapus data ke "+position)
                      .setCancelable(false)
                      .setPositiveButton("Iya", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              BookmarkModel model = BookmarkModel.findById(BookmarkModel.class, 4L);
                              model.delete();
                              adapter.notifyDataSetChanged();
                          }
                      })
                      .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              dialog.cancel();
                          }
                      });
              AlertDialog alert = builder.create();
              alert.show();
          }
      
          static class ViewHolder {
              public ImageView img;
              public TextView brandName;
              public TextView descTerm;
              public TextView type;
              public TextView details;
          }
      
          class BookmarkAdapter extends ArrayAdapter<BookmarkModel>{
              List<BookmarkModel> model;
              Context mContext;
      
              public BookmarkAdapter(Context context, int resource, List<BookmarkModel> objects) {
                  super(context, resource, objects);
                  vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  Resource = resource;
                  model = objects;
                  mContext = context;
              }
      
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                  View v = convertView;
                  if (v == null) {
                      holder = new ViewHolder();
                      v = vi.inflate(Resource, null);
                      holder.img = (ImageView) v.findViewById(R.id.thumbnail);
                      holder.brandName = (TextView) v.findViewById(R.id.brName);
                      holder.type = (TextView) v.findViewById(R.id.type);
                      holder.details = (TextView) v.findViewById(R.id.detailS);
                      holder.descTerm = (TextView) v.findViewById(R.id.descTerm);
                      v.setTag(holder);
                  } else {
                      holder = (ViewHolder) v.getTag();
                  }
                  byte[] data = model.get(position).getImg();
                  Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                  holder.img.setImageBitmap(bmp);
                  holder.brandName.setText(model.get(position).getBrandName());
                  holder.type.setText(model.get(position).getType());
                  holder.descTerm.setText(model.get(position).getDescTerm());
                  String date = model.get(position).getDate().replace("\n", "");
                  String location = model.get(position).getLocation().replace("\n", "");
                  holder.details.setText(date +"\n"+ location);
                  return v;
              }
          }
      }
      

0 个答案:

没有答案