sql >> Database teknologi >  >> RDS >> SQLite

Hvordan læser og opdaterer jeg SQLite-database ved hjælp af ListView i Android?

Dette bør fjernes.

Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityChampagne.class);
startActivity(launchActivity);

Og du har denne

 public class MainActivityChampagne  

Din MainActivityChampagne klasse udvider ikke aktiviteten. Det er en ikke-aktivitetsklasse.

For at indstille data til listevisning.

 MainActivityChampagne db = new MainActivityChampagne(MainActivityPlanes.this);
 db.open();
 ArrayList<String> list = db.getData();    

Brug nu listen til at indstille dataene til listevisning.

Definer row.xml med en tekstvisning med id textView1.

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivityPlanes.this, R.layout.row, R.id.textView1,list);  
  List = (ListView) findViewById(R.id.listView1);  
  List.setAdapter(adapter);  

Når du har indstillet adapteren til listevisning.

 List.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
  {           
        .  // on list item click do what is necessary
  }
  });  

Rediger:

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="114dp"
        android:layout_marginTop="30dp"
        android:text="TextView" />

</RelativeLayout>

Snapshot

Edit2:Opdateringen er rettet. Du skal dog ændre nedenstående i overensstemmelse med dine behov.

public class MainActivityChampagne {

public static final String KEY_NAME = "title";
public static final String KEY_NOTES = "lastcall";

private static final String DATABASE_NAME = "Champagnedb5";
private static final String DATABASE_TABLE = "champagneTable";
private static final int DATABASE_VERSION = 1;
private static final String KEY_ID = "id";

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // TODO Auto-generated method stub\
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
                KEY_NAME + " TEXT NOT NULL," + 
                KEY_NOTES + " TEXT NOT NULL" + ");"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
        onCreate(db);

    }

}

public MainActivityChampagne(Context c){
    ourContext = c;

}

public MainActivityChampagne open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;

}

public void close(){
    ourHelper.close();

}

public long createEntry(String titles, String notes) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, titles);
    cv.put(KEY_NOTES, notes);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public ArrayList<String> getData() {
    //TODO Auto-generated method stub
    String[] columns = new String[] { KEY_NAME };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";
    int iNotes = c.getColumnIndex(KEY_NAME);
    ArrayList<String> string_array = new ArrayList<String>();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
    result = c.getString (iNotes) + "\n";
    string_array.add(result);
    }
           return string_array;

}

public void updateEntry(String thetitles, String thenotes) {
    // TODO Auto-generated method stub
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, thetitles);
    cvUpdate.put(KEY_NOTES, thenotes);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, "id "+"="+1, null);
    // this will update row 1 coz id is 1. 
    //ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + thetitles + KEY_NOTES + "=" + thenotes, null);

}

}


  1. Beregning af medianen med en dynamisk markør

  2. Hvad betyder et tidsstempel i T-Sql i C#?

  3. SQL Server XML-forespørgsel med flere navnerum

  4. Forstå PIVOT-funktionen i T-SQL