2016-03-05 81 views
0

我已经实现了显示虚拟卡的CardView。但我不得不 显示数据从这个卡从SQLite数据库,即从每行到每个卡 。另外,我想要在卡中按下删除按钮时删除特定行。我怎样才能做到这一点?从SQLite数据库显示数据到Cardview

这是我的名片片段代码:

public class UserAppFragment extends Fragment { 

public UserAppFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    //using recycle view cutomizing card views 
    RecyclerView recyclerView = (RecyclerView) inflater.inflate(
      R.layout.recycler_view, container, false); 
    ContentAdapter adapter = new ContentAdapter(); 
    recyclerView.setAdapter(adapter); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Click action 
      Toast.makeText(getContext(),"FAB",Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(getContext(), EventAdder.class); 
      startActivity(intent); 
     } 
    }); 

    return recyclerView; 
} 
//view holder for cards data start 
public static class ViewHolder extends RecyclerView.ViewHolder { 
    public ViewHolder(LayoutInflater inflater, ViewGroup parent) { 
     super(inflater.inflate(R.layout.fragment_home_card_list, parent, false)); 
     itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Context context = v.getContext(); 
       Intent intent = new Intent(context, EventAdder.class); 
       context.startActivity(intent); 
      } 
     }); 


     ImageButton shareImageButton = 
       (ImageButton) itemView.findViewById(R.id.share_button); 
     shareImageButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       Snackbar.make(v, "Added to Favorite", 
         Snackbar.LENGTH_LONG).show(); 
      } 
     }); 

     ImageButton deleteImageButton = (ImageButton) itemView.findViewById(R.id.delete_button); 
     deleteImageButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       Snackbar.make(v, "Card deleted", 
         Snackbar.LENGTH_LONG).show(); 
      } 
     }); 

    } 
} //view holder end 

// start recycleview.adapter wrapping viewholder created 
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> { 
    // Set numbers of List in RecyclerView. 
    private static final int LENGTH = 15; 
    public ContentAdapter(){ 


    } 


    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ViewHolder(LayoutInflater.from(parent.getContext()), parent); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     // no-op 
    } 

    @Override 
    public int getItemCount() { 
     return LENGTH; 
    } 
} 
// end recycleview.adapter wrapping viewholder created 

@Override 
public void onDetach() { 
    super.onDetach(); 
} 

}

+0

你应该尝试在谷歌搜索一样... http://www.googleautobot.com/?q=updating+view+from+ SQLite +数据库+ Android这是第一个相同的教程之一http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Omkar

+0

@Omkar我已经搜索谷歌和许多网站,但我可以没有得到一个明确的理解,在Cardview – user3556657

回答

3

按照下列步骤操作:

1)创建使用DB Helper您的应用程序SQLite数据库。

例子:

public class FeedReaderDbHelper extends SQLiteOpenHelper { 
    // If you change the database schema, you must increment the database version. 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "FeedReader.db"; 

    public FeedReaderDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
} 

要访问上面的类:

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext()); 

2)你需要在数据库中插入数据。重要的是你必须在表格中有一个id列。

// Gets the data repository in write mode 
SQLiteDatabase db = mDbHelper.getWritableDatabase(); 

// Create a new map of values, where column names are the keys 
ContentValues values = new ContentValues(); 
values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id); 
values.put(FeedEntry.COLUMN_NAME_TITLE, title); 
values.put(FeedEntry.COLUMN_NAME_CONTENT, content); 

// Insert the new row, returning the primary key value of the new row 
long newRowId; 
newRowId = db.insert(
     FeedEntry.TABLE_NAME, 
     FeedEntry.COLUMN_NAME_NULLABLE, 
     values); 

现在,您的数据库中有数据。 (您可以使用以下POJO类也一样,在表中插入数据)

3)作出POJO对象从数据库中提取数据。

public class Example{ 

    int id; 

    String title; 

    String content; 

    public Example() { 

    } 

    public Example(int id, String title, String content) { 
     this.id = id; 
     this.title = title; 
     this.content = content; 
    } 
} 

4)以List<Example>的形式从数据库中读取数据。

SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

String selectQuery = "SELECT * FROM yourTableName"; 

Cursor c = database.rawQuery(selectQuery, null); 

List<Example> example = new ArrayList<Example>(); 

if (cursor.moveToFirst()) { 
    do { 

     example.add(new Example(
      c.getInt(0),  // id 
      c.getString(1),  // title 
      c.getString(2)  // content 
     )); 

    } while (cursor.moveToNext()); 
} 

5)在RecyclerView的onBindViewHolder方法获取数据列表List<Example> example与UI参考设置您的数据。此外,在这里

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    Example temp = example.get(position); 

    holder.tvTitle.setText(temp.title); 

    holder.tvContent.setText(temp.content); 

    holder.deleteImageButton.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      // Define 'where' part of query. 
      String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; 

      // Specify arguments in placeholder order. 
      String[] selectionArgs = { String.valueOf(temp.id) }; 

      // Issue SQL statement. 
      db.delete(table_name, selection, selectionArgs); 

      // delete item from list 
      example.remove(temp); 

      // adapter must be in global scope. 
      adapter.notifyDataSetChanged(); 

      Snackbar.make(v, "Card deleted", 
        Snackbar.LENGTH_LONG).show(); 
     } 
    }); 
} 

希望设置onClickListener这可以帮助你,或尝试观看一些影片的详细说明。

+0

实施这是什么holder.tvTitle,tvContent等?我不知道如何让我的卡的3 textviews ui refenence。 – user3556657

+0

holder.tvTitle是来自您的回收商视图模板的文本视图。要将视图持有者类中的UI映射到卡片布局,然后在该布局上为每个文本视图使用findViewBy(int resource),然后将该持有者传递给回收器视图适配器。 – Omkar

+0

谢谢。现在它的工作! – user3556657

0

使用这种方法来获得数据库的上下文参数

public void onBindViewHolder(ViewHolder holder,final int position) {  

    CardView cardView = holder.cardView; 

    Context context = cardView.getContext(); 
    YoupostDatabase youpostDatabase = new YoupostDatabase(context); 
    SQLiteDatabase db = youpostDatabase.getReadableDatabase(); 
}