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(); 
}