lundi 9 novembre 2015

ListView SQLite Grabbing information from different activity

I am using two activity's, one that stores all the users information (in addition I have a Product.java that stores all the classes), and one that has all the listview (main acitivity). With that I created a DatabaseHandler.java to use SQLite. I seem to be stuck somewhere.

In my arrayadapter I put Product, which is my class in the product.java file which I believe to have made a mistake?

I am beginner with android studio (trying to get awesome at it) and I was only good at using sharedpreferences but I did a bit of looking around and there only a limit on what it could do.

Main Activity

public class MainActivity extends Activity {
        private ListView listView;
        private String[] details;
        public static ArrayList<String> names = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listView);
// replace \ with left angle bracket in the line below
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Product);
            listView.setAdapter(arrayAdapter);
            listView.setOnItemClickListener(

                    new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        }
                    }
            );
        }

Second Activity

public class Main2Activity extends Activity {
    private EditText textId;
    private EditText textName;
    private EditText textModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        textId=(EditText)findViewById(R.id.inputID);
        textName=(EditText)findViewById(R.id.inputName);
        textModel=(EditText)findViewById(R.id.inpuModelNumber);

    }

    public void save(View v){
        int anID = Integer.parseInt(textId.getText().toString());
        String aName = textName.getText().toString();
        String aModel = textModel.getText().toString();
        DatabaseHandler db = new DatabaseHandler(this);
        db.addProduct(new Product(anID, aName, aModel));

        finish();
    }

}

DatabaseHandler.java

class DatabaseHandler extends SQLiteOpenHelper {


public DatabaseHandler(Context context){
    super(context, "testDB", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE contactTable (colID, colName, colPhone)");
}

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

}


public void addProduct(Product Product){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("colID", Product.getId());
    values.put("colName", Product.getProductName());
    values.put("colPhone", Product.getProductModel());

    long result = db.insert("contactTable", null, values);
    if (result > 0){
        Log.d("dbhelper", "inserted successfully");
    } else {
        Log.d("dbhelper", "failed to insert");
    }

    db.close();
}

Aucun commentaire:

Enregistrer un commentaire