I need to create an SQLite database with RecyclerView in which I've already tried searching for all the samples but none so far suited my need. Most of them can't be comprehended since I couldn't understand their structure eg their folders, java classe, etc. I've been trying to figure that out myself for many days but couldn't find a valuable solutions. I'll go straight to the point, I need to understand how to read my sqlite database to the recyclerview basically only the field terms should show up. For the time being, the insertion of data are hardcoded for now since I want to see if my database is working which suddenly doesn't.
Here is my DatabaseHelper.java:
package com.zaid.recyclerviewsqlite.mynote_databases;
import java.util.ArrayList;
import java.util.List;
import com.zaid.recyclerviewsqlite.notes.DataModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBASE = "my_notes";
public static final String TBLMYNOTES = "tblnotes";
public static final int DBVERSION = 1;
public static final String NOTE_TERM = "note_term";
public static final String NOTE_ID = "note_id";
public static final String NOTE_DEF = "note_def";
public static final String NOTE_SYNTAX = "note_syntax";
public static final String NOTE_CODE = "note_code";
public int currentID;
public DatabaseHelper(Context context) {
super(context, DBASE, null, DBVERSION);
// TODO Auto-generated constructor stub
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+TBLMYNOTES+" ("+ NOTE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NOTE_TERM+" TEXT, "+NOTE_DEF+" TEXT, "+NOTE_SYNTAX+" TEXT, "+NOTE_CODE+" TEXT )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+TBLMYNOTES);
onCreate(db);
}
public long InsertNewRecord( String tblname, ArrayList<String> fields, ArrayList<String> values ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues db_values = new ContentValues();
int countArr = values.size();
for( int i = 0; i < countArr; i++ ) {
db_values.put( fields.get(i), values.get(i) );
}
long status = db.insert(TBLMYNOTES, null, db_values);
return status;
}
public Cursor getAllData(String tblname) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM "+tblname, null);
return result;
}
/**
* @return
*/
public List<DataModel> getAllDataModel(){
List<DataModel> dataList = new ArrayList<DataModel>();
String query = "SELECT * FROM" + TBLMYNOTES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(query, null);
while(c.moveToNext()){
/** DataModel model= new DataModel();
model.setId(Integer.parseInt(c.getString(0)));
model.setTerm(c.getString(1));
model.setDefinition(c.getString(2));
model.setSyntax(c.getString(3));
model.setCode(c.getString(4));
*/
int index0 = c.getColumnIndex(NOTE_ID);
int index1 = c.getColumnIndex(NOTE_TERM);
int index2 = c.getColumnIndex(NOTE_DEF);
int index3 = c.getColumnIndex(NOTE_SYNTAX);
int index4 = c.getColumnIndex(NOTE_CODE);
int id = c.getInt(index0);
String term = c.getString(index1);
String def = c.getString(index2);
String syn = c.getString(index3);
String code = c.getString(index4);
DataModel model = new DataModel(id, term, def, syn, code);
dataList.add(model);
}
return dataList;
}
public int DeleteRecord(String tblname, String strField, String strValue) {
SQLiteDatabase db = this.getWritableDatabase();
int status = db.delete(TBLMYNOTES , " "+strField+" = ? ", new String[] { strValue });
return status;
}
public int UpdateRecord(String tblname, ArrayList<String> Fields, ArrayList<String> Values) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues db_values = new ContentValues();
int countMaxArr = Fields.size();
String strFieldID = Fields.get(0);
String strFieldValue = Values.get(0);
for( int i = 0; i < countMaxArr; i++ ) {
db_values.put(Fields.get(i), Values.get(i));
}
int status = db.update(TBLMYNOTES, db_values, strFieldID+" = ? ", new String[] { strFieldValue });
return status;
}
public void setcurrID(int ID) {
currentID = ID;
}
public int getcurrID() {
return currentID;
}
public Cursor getRecordByID(String tblname, String Fieldname, String Fieldvalue) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM "+TBLMYNOTES+" WHERE "+Fieldname+" = "+Fieldvalue, null);
return result;
}
}
This is the My_Notes that displays the list of terms in recyclerview: for now I coded the insertion of database manually inside here to test if my database is working but to no avail did it do me any good.
package com.zaid.recyclerviewsqlite.notes;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import com.zaid.recyclerviewsqlite.R;
import com.zaid.recyclerviewsqlite.addNotes.My_Notes_Add_Main;
import com.zaid.recyclerviewsqlite.mynote_databases.DatabaseHelper;
public class My_Notes extends AppCompatActivity implements View.OnClickListener {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<DataModel> data;
static View.OnClickListener myOnClickListener;
private static ArrayList<Integer> removedItems;
private ImageButton addNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_notes);
//database
DatabaseHelper db = new DatabaseHelper(this);
ArrayList<String> ArrValues = new ArrayList<String>() ;
ArrayList<String> FieldsArr = new ArrayList<String>();
ArrValues.add("Hierarchy");
ArrValues.add("Definition Here!");
ArrValues.add("Syntax Here!");
ArrValues.add("Code Here!");
FieldsArr.add( "NOTE_TERM" );
FieldsArr.add( "NOTE_DEF" );
FieldsArr.add( "NOTE_SYNTAX" );
FieldsArr.add( "NOTE_CODE" );
db.InsertNewRecord("TBLMYNOTES", FieldsArr, ArrValues);
// myOnClickListener = new MyOnClickListener(this);
addNote = (ImageButton)findViewById(R.id.addNotes);
recyclerView = (RecyclerView) findViewById(R.id.my_notes_recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(new CustomAdapter(db.getAllDataModel()));
btnAdd();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void btnAdd(){
addNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),My_Notes_Add_Main.class);
startActivity(i);
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
This is my DataModel.java
package com.zaid.recyclerviewsqlite.notes;
public class DataModel {
String term_;
String definition_;
int id_;
String syntax_;
String code_;
public DataModel(){
}
public DataModel(int id, String term, String def, String syntax, String code) {
this.term_ = term;
this.id_ = id;
this.definition_=def;
this.syntax_=syntax;
this.code_=code;
}
public String getTerm() {
return term_;
}
public String getDefinition(){
return definition_;
}
public String getSyntax() {
return syntax_;
}
public String getCode() {
return code_;
}
public int getId() {
return id_;
}
public void setTerm(String term){
this.term_= term;
}
public void setId(int id){
this.id_= id;
}
public void setDefinition(String definition){
this.definition_= definition;
}
public void setSyntax(String syntax){
this.syntax_= syntax;
}
public void setCode(String code){
this.code_= code;
}
}
This is my CustomAdapter.java
package com.zaid.recyclerviewsqlite.notes;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.zaid.recyclerviewsqlite.R;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList<DataModel> dataSet;
private List<DataModel> items;
public CustomAdapter(List<DataModel> items){
this.items = items;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewTerm;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewTerm = (TextView) itemView.findViewById(R.id.textViewTerm);
}
}
public CustomAdapter(ArrayList<DataModel> data) {
this.dataSet = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_notes_card_view, parent, false);
//view.setOnClickListener(My_Notes.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
holder.textViewTerm.setText(dataSet.get(listPosition).getTerm());
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
For my libraries I have this here: I'm using eclipse so I have no idea what version they are
android-support-v7-appcompat cardview design recyclerview
My Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.zaid.recyclerviewsqlite"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyMaterialTheme" >
<activity
android:name="com.zaid.recyclerviewsqlite.My_Notes"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zaid.recyclerviewsqlite.My_Notes_Add_Main"
>
</activity>
</application>
</manifest>
Aucun commentaire:
Enregistrer un commentaire