Right now I'm trying to display information from a SQLite database, the columns are as listed: ID, Name, Weight, Percent weight and percent are arraylists that are parsed using the Gson library into string form so they can be input within the database.
Currently, I'm not trying to read the data from those 2 columns, but only the Name column and display it into a listview, which I have down, and I know its an issue with the Gson arrays because when I comment those commands out, and the two columns in the table, the data lists perfectly fine, but when I put it back it does not list any longer.
Here is my code that adds the users input into the database:
public void addData(){
String titleString= gson.toJson(titleArray);
String percent = gson.toJson(percentArray);
Toast.makeText(WeightClass.this, titleString,
Toast.LENGTH_LONG).show();
try {
database.insertData(className, titleString, percent);
}catch (Exception e){
String error= e.toString();
System.out.println(error);
}
Intent intent = new Intent(WeightClass.this, GradeActivity.class);
startActivity(intent);
}
The entire class containing the database:
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "class.db";
public static final String TABLE_NAME = "class_table";
public static final String POINTS_BASED_TABLE = "points_based";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "WEIGHT";
public static final String COL_4 = "PERCENT";
//TODO fix array to look nicer for the weight selection class
private Cursor res;
public Database(Context context) {super(context, DATABASE_NAME, null, 1);}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, WEIGHT TEXT, PERCENT TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
public void insertData(String name, String weightName, String percent){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
if (name != null) contentValues.put(COL_2, name);
if (weightName != null) contentValues.put(COL_3, weightName);
if (percent != null) contentValues.put(COL_4, percent);
try {
db.insert(TABLE_NAME, null, contentValues);
System.out.println("Data was Successfully Inserted");
}catch (Exception e){
System.out.println("Data was not inserted Database Class");
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
res = db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
return res;
}
And the onCreate method from the class that displays the information from the database:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
database = new Database(this);
classList = new ArrayList<String>();
className = (TextView) findViewById(R.id.class_name);
if (list == null) {
list = (ListView) findViewById(R.id.class_list);
}
classAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
classList);
list.setAdapter(classAdapter);
setListAdapter(classAdapter);
com.github.clans.fab.FloatingActionButton fab = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.fabb);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
weightClass();
}
});
com.github.clans.fab.FloatingActionButton fab2 = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.fab2);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pointClass();
}
});
res = database.getData();
res.moveToFirst();
while (res.moveToNext()) {
addItems(null, res.getString(1)); //adds items to list by name
}
//takes you to the screen where you can view class information
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(GradeActivity.this, ClassDescription.class);
startActivity(intent);
}
});
}
I know its a lot of information, so my apologies, but this one has really got me stumped. Thank you in advance for all your help
Aucun commentaire:
Enregistrer un commentaire