The database established on the first Activity and placed on ListView on the second Activity?



  • There's a first activism that fills the database. The second activate needs to be portrayed. ListView♪ What am I not doing, what's left here or what's missing?
    What do you want to do in the second class to decorate the database in the second activate, What's the object to transfer? It's not working.

    public class MyClass 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);
    
    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);
    
    
    
    
    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) {
    
    }
    

    }
    }



  • Kurs Absolutely. You don't have to close until the list's out.
    Close the courseor or in the method. onPause()But then he needs to be rebuilt. onResume()or onStop()but in certain circumstances it is possible that this method will not be called.

    The best solution will be https://ru.stackoverflow.com/a/427870/177345 to obtain data in the course.

    The design of the adapter in the cycle is completely wrong. Adapter is appointed once. When the adapter is initialized, it is given the cadet full and must not retract all of its elements in the cycle, the adapter will retract them when it's on the screen. At the same time, the fourth argument (on your headers) refers the names of the poles to the OBD, which should be displayed on the list - they should be present in a sample of the courseor, the fifth argument is the Aydish View, on the website of the list. layout.two_line_list_item.xmlwhere these data will be posted. In this case, reference is made to the systemic idynamics from the system markings which are predetermined in advance (android.R.id.text1 and android.R.id.text2):

    Cursor c = db.query("mytable", null, null, null, null, null, null);
    String[] headers = new String[] {"suma text","valuta text"};
    cAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
                             c, headers, new int[] {android.R.id.text1, android.R.id.text2}, 0);
    mList.setAdapter(cAdapter);    
    //c.close();
    

    BD column with IDs https://ru.stackoverflow.com/a/440080/177345 No way. id() For corrective work with classes SDK Android^

    db.execSQL("create table mytable ("
                + "_id integer primary key autoincrement,"
                + "suma text,"
                + "valuta text" +");");
    

    Read more detail, for example. http://metanit.com/java/android/14.1.php

    Which second activism is completely unconscious. In the code, you have the data and the list on the same activate, and what, where and to whom, it's not clear.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2