P
P
pmozil2016-06-16 01:27:21
Java
pmozil, 2016-06-16 01:27:21

Database created on the first Activity and placed in a ListView on the second Activity?

There is the first activity on which the database is filled.
On the second activity, it needs to be displayed in a ListView.
What am I doing wrong?
What is missing or missing here?
What to register in the second class to display the database in the second Activity?
What object should be transferred? Nothing happens. I broke my whole head.
Thanks in advance.

public class Zrob extends Activity implements View.OnClickListener {
    private static final String TAG = "myLogs";
    public Button btnOk;
    public EditText tvSum;
    public Spinner spinner;
    DBHelper dbHelper;
    SimpleCursorAdapter cAdapter;
    ListView mList;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zrob);

        dbHelper = new DBHelper(getApplicationContext());

        btnOk = (Button) findViewById(R.id.btnOk);
        btnOk.setOnClickListener(this);

        mList = (ListView) findViewById(R.id.mList);
        spinner = (Spinner) findViewById(R.id.spinner);

        tvSum = (EditText) findViewById(R.id.editText);

        Log.d(TAG, "onCreate ZROB.class");

        String[] valuta = {"RU","UA","USD","EUR"};
     

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,valuta);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>


        spinner.setAdapter(adapter);
        spinner.setPrompt("Waluta");
        spinner.setSelection(0);

 


        dbHelper = new DBHelper(this);
    }


    @Override
    public void onClick(View v) {
    //Объект для данных
        ContentValues cv = new ContentValues();
   
        String suma = tvSum.getText().toString();
        String valuta = spinner.getSelectedItem().toString();
     
//подключаюсь к базе
       SQLiteDatabase db = dbHelper.getWritableDatabase();
       // запаковал
        cv.put("suma", suma);
        cv.put("valuta", valuta);

     

        //Получил id строки
        long rowID = db.insert("mytable", null, cv);
//Получил курсор с данными
        Cursor c = db.query("mytable", null, null, null, null, null, null);
       
        if (c.moveToFirst()) {
//получаем id столбцов
            int idColIndex = c.getColumnIndex("_id");
            int sumColIndex = c.getColumnIndex("suma");
            int valColIndex = c.getColumnIndex("valuta");
         

            do {
                Log.d(TAG,
                        "id = " + c.getInt(idColIndex) +
                                ", suma = " + c.getString(sumColIndex) +
                                ", valuta = " + c.getString(valColIndex));
                           
                    String[] headers = new String[] {"suma","valuta"};
                    cAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
                             c, headers, new int[]{kontoColIndex, sumColIndex}, 0);
                     mList.setAdapter(cAdapter);

            } while (c.moveToNext());
        } else
            Log.d(TAG, "0 rows");

        c.close();


    }

    class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
            super(context, "myDB", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d(TAG, "--- onCreate database ---");
            db.execSQL("create table mytable ("
                    + "id integer primary key autoincrement,"
                    + "suma text,"
                    + "valuta text" +");");

        }

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

        }
    }


}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question