dimanche 6 mars 2016

recyclerView dosn't show the 'real' data for my list items

I have implemented a recyclerView and a SQLite database to save/retrive data for the recylerview, but the data I get on the recyclerView is not the data that should show. The recyclerView worked as it should without the SQLite db.

enter image description here

When the plus sign is clicked, a dialog will popup with editext fields, where the user can type the information:

Here is the DialogFragment class where the user shall write their information:

public class DialogAdd extends DialogFragment {

private Button okButton;
private EditText name, quantity, location, normalPrice, offerPrice;
private List<ShopListItem> shopListItem;
private Context context;
DatabaseHelper dbHelper;


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

    dbHelper = new DatabaseHelper(getContext());

    shopListItem = new ArrayList<>();
    context = getActivity();
}


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

    final View rootView = inflater.inflate(R.layout.add_productdialog,container, false);
    getDialog().setCanceledOnTouchOutside(false);
    getDialog().setTitle("Add to shoplist");




    name = (EditText) rootView.findViewById(R.id.dialog_productname);
    quantity = (EditText) rootView.findViewById(R.id.dialog_qantity);
    location = (EditText) rootView.findViewById(R.id.dialog_location);
    normalPrice = (EditText) rootView.findViewById(R.id.dialog_normalPrice);
    offerPrice = (EditText) rootView.findViewById(R.id.dialog_offerPrice);

    okButton = (Button) rootView.findViewById(R.id.dialog_okButton);
    okButton.getBackground().setColorFilter(Color.parseColor("#2fbd4b"), PorterDuff.Mode.MULTIPLY);
    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (name.getText().toString().isEmpty()) {
                Toast.makeText(context, "You must add a name", Toast.LENGTH_LONG).show();
            } else {

              dbHelper.insertData(name.toString() ,quantity.toString(),location.toString(),normalPrice.toString(),offerPrice.toString());
                getDialog().dismiss();
            }


        }
    });

    return rootView;

}

This is the mainActivity class where I create the recylerview, adapters and Database:

public class MainActivity extends AppCompatActivity{


private ImageButton addbutton;
private DialogAdd dialogAdd;
public static RecyclerView recyclerView;
private List<ShopListItem> shopListItems;
private SQLiteDatabase db;
private Cursor cursor;
private DatabaseHelper databaseHelper;
private ShoplistAdapter adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shoppinglist_mainactivity);


    databaseHelper = new DatabaseHelper(this);

    addbutton = (ImageButton) findViewById(R.id.addbtn);
    addbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogAdd = new DialogAdd();
            dialogAdd.show(getSupportFragmentManager(), "addDialog");
        }
    });



    //RecyclerView
    recyclerView = (RecyclerView)findViewById(R.id.rv_shoppinglist);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.getAppContex());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);



    initializeData();
    adapter = new ShoplistAdapter(shopListItems);
    recyclerView.setAdapter(adapter);


}


private void initializeData(){

    shopListItems = new ArrayList<>();
    Cursor resultset = databaseHelper.getAllData();

    if (resultset.moveToFirst()){
        while(!resultset.isAfterLast()){

            shopListItems.add(new ShopListItem(resultset.getString(1), resultset.getString(2), resultset.getString(3), resultset.getString(4), resultset.getString(5)));

            resultset.moveToNext();
        }
    }
    resultset.close();


    shopListItems.add(new ShopListItem("Potato", "2 KG", "MALL", "7 kr", ""));
}

This class is where the database is defined:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME ="dbshoplist.db";
public static final String TABLE_NAME ="product_table";

public static final String COL_ID = "ID";
public static final String COL_NAME ="NAME";
public static final String COL_QTY ="QUANTITY";
public static final String COL_LOCATION ="LOCATION";
public static final String COL_PRICE1 ="PRICE1";
public static final String COL_PRICE2 ="PRICE2";


/*
This constructor creates the database
 */
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,QUANTITY TEXT,LOCATION TEXT,PRICE1 TEXT,PRICE2 TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}


public boolean insertData(String name, String qty, String location, String price1, String price2){


    SQLiteDatabase db = this.getWritableDatabase();

    // content value is a row, and we fill it with the put();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_NAME, name);
    contentValues.put(COL_QTY, qty);
    contentValues.put(COL_LOCATION, location);
    contentValues.put(COL_PRICE1, price1);
    contentValues.put(COL_PRICE2, price2);


    long result = db.insert(TABLE_NAME, null,contentValues);

    if(result == -1) {
        return false;
    }else{
        return true;
        }
    }


public Cursor getAllData(){

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursorResults = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return cursorResults;

}

Aucun commentaire:

Enregistrer un commentaire