我已經實現了顯示虛擬卡的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();
}
}
你應該嘗試在谷歌搜索一樣... http://www.googleautobot.com/?q=updating+view+from+ SQLite +數據庫+ Android這是第一個相同的教程之一http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Omkar
@Omkar我已經搜索谷歌和許多網站,但我可以沒有得到一個明確的理解,在Cardview – user3556657