2015-06-30 145 views
-1

我是新來的android和我設法從EditText插入數據到數據庫中,我將它顯示到ListView,但現在我需要顯示一個單一的音符,當點擊項目內ListView和我只是不知道如何。我希望它可以在activity_edit_note.xml可編輯。有人可以幫我嗎?這是我的代碼。如何從單擊ListView時從數據庫中獲取數據

Notes.java

package com.cidecode.xnotes; 
public class Notes { 
    private long id; 
    private String title; 
    private String note; 
    private String date; 

    public Notes(){ 

    } 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getNote() { 
     return note; 
    } 

    public void setNote(String note) { 
     this.note = note; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    @Override 
    public String toString(){ 
    return title + "\t" + date + "\n" + note; 
} 
} 

DatabaseHelper.java

package com.cidecode.xnotes; 

import android.content.Context; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DatabaseHelper extends SQLiteOpenHelper { 

    public static final String TABLE_NOTES = "notes"; 
    public static final String COLUMN_ID = "id"; 
    public static final String COLUMN_TITLE = "title"; 
    public static final String COLUMN_NOTE = "note"; 
    public static final String COLUMN_DATE = "date"; 

    private static final String DATABASE_NAME = "xnotes.db"; 
    private static final int DATABASE_VERSION = 1; 

    // Create the database 
    private static final String DATABASE_CREATE = "create table " + TABLE_NOTES + "(" + 
      COLUMN_ID + " integer primary key autoincrement, " + COLUMN_TITLE + " text not null, " + 
      COLUMN_NOTE + " text not null, " + COLUMN_DATE + " text not null);"; 

    // Drop table notes 
    private static final String DATABASE_DROP_TABLE_NOTES = "drop table if exists " + TABLE_NOTES; 

    public DatabaseHelper(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(android.database.sqlite.SQLiteDatabase db) { 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(android.database.sqlite.SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(DatabaseHelper.class.getName(), "Upgrading database from v" + oldVersion + " to v" + 
       newVersion + " which will delete all old data."); 

     db.execSQL(DATABASE_DROP_TABLE_NOTES); 
     onCreate(db); 
    } 
} 

NotesDataSource.java

package com.cidecode.xnotes; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.widget.EditText; 

import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 

public class NotesDataSource { 
private SQLiteDatabase database; 
private DatabaseHelper dbHelper; 
private String[] allColumns = {DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_TITLE, DatabaseHelper.COLUMN_NOTE, DatabaseHelper.COLUMN_DATE}; 

public NotesDataSource(Context context){ 
    dbHelper = new DatabaseHelper(context); 
} 

public void open() throws SQLException{ 
    database = dbHelper.getWritableDatabase(); 
} 

public void close(){ 
    dbHelper.close(); 
} 

public Notes createNote(String title, String note, String date){ 
    ContentValues values = new ContentValues(); 
    values.put(DatabaseHelper.COLUMN_TITLE, title); 
    values.put(DatabaseHelper.COLUMN_NOTE, note); 
    values.put(DatabaseHelper.COLUMN_DATE, date); 
    long insertId = database.insert(DatabaseHelper.TABLE_NOTES, null, values); 

    Cursor cursor = database.query(DatabaseHelper.TABLE_NOTES, allColumns, 
      DatabaseHelper.COLUMN_ID + " = " + insertId, null, null, null, null); 
    cursor.moveToFirst(); 
    Notes newNotes = cursorToNote(cursor); 
    cursor.close(); 
    return newNotes; 
} 

public List<Notes> getAllNotes(){ 
    List<Notes> notesList = new ArrayList<Notes>(); 

    Cursor cursor = database.query(DatabaseHelper.TABLE_NOTES, allColumns, 
      null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()){ 
     Notes note = cursorToNote(cursor); 
     notesList.add(note); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    return notesList; 
} 

private Notes cursorToNote(Cursor cursor){ 
    Notes note = new Notes(); 
    note.setId(cursor.getLong(0)); 
    note.setTitle(cursor.getString(1)); 
    note.setNote(cursor.getString(2)); 
    note.setDate(cursor.getString(3)); 
    return note; 
} 

public void deleteNote(int id){ 
    database.delete(DatabaseHelper.TABLE_NOTES, DatabaseHelper.COLUMN_ID + "=" + id, null); 
} 

}

AddNote.java

package com.cidecode.xnotes; 

import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.sql.SQLException; 


public class AddNote extends ActionBarActivity { 

private EditText title = null; 
private EditText note = null; 
private String s_title, s_note, s_date; 
Button b_save; 
private SQLiteDatabase database; 
private DatabaseHelper dbHelper; 
private NotesDataSource datasource; 

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

    dbHelper = new DatabaseHelper(this); 
    datasource = new NotesDataSource(this); 
    try { 
     datasource.open(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    title = (EditText)findViewById(R.id.id_title); 
    note = (EditText)findViewById(R.id.id_write_note_here); 

    s_title = title.getText().toString(); 
    s_note = note.getText().toString(); 
    s_date = "21.11.1111"; 

    b_save = (Button)findViewById(R.id.id_save); 
    b_save.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      datasource.createNote(title.getText().toString(), note.getText().toString(), s_date); 
      Toast.makeText(getApplicationContext(), "Note is saved.", Toast.LENGTH_LONG).show(); 
      Log.w("Title: ", title.getText().toString()); 
      Log.w("Note: ", note.getText().toString()); 
      Log.w("Date: ", s_date); 
      Intent intent = new Intent(AddNote.this, MainActivity.class); 
      startActivity(intent); 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_add_note, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

activity_add_note.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:background="#EBD28F"> 

    <TextView 
     android:id="@+id/id_add_new_note" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/s_add_new_note" 
     android:gravity="center" 
     android:textSize="30sp" 
     android:layout_marginBottom="10dp"/> 

    <EditText 
     android:id="@+id/id_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/s_title" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginBottom="5dp"/> 

    <EditText 
     android:id="@+id/id_write_note_here" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:hint="@string/s_write_note_here" 
     android:gravity="top" /> 

    <Button 
     android:id="@+id/id_save" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:text="@string/s_save" 
     android:textColor="#FFFFFF" 
     android:background="#5E553A"/> 

</LinearLayout> 

MainActivity.java

package com.cidecode.xnotes; 

import android.app.ActionBar; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Window; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

import java.sql.SQLException; 
import java.util.List; 


public class MainActivity extends ListActivity { 

    Button AddNew; 
    private ListView listView; 
    private SQLiteDatabase database; 
    private DatabaseHelper dbHelper; 
    private NotesDataSource datasource; 


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

     dbHelper = new DatabaseHelper(this); 
     datasource = new NotesDataSource(this); 
     try { 
      datasource.open(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

     List<Notes> values = datasource.getAllNotes(); 
     final ArrayAdapter<Notes> adapter = new ArrayAdapter<Notes>(this, android.R.layout.simple_list_item_1, values); 
     setListAdapter(adapter); 

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

      } 
     }); 

     listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       return false; 
      } 
     }); 

     AddNew = (Button)findViewById(R.id.id_add_new); 
     AddNew.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this, AddNote.class); 
       startActivity(intent); 
      } 
     }); 



    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:background="#EBD28F"> 

    <TextView 
     android:id="@+id/id_xnotes" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/s_xnotes" 
     android:gravity="center" 
     android:textSize="30sp" 
     android:layout_marginBottom="10dp"/> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:clickable="true" 
     android:longClickable="true"></ListView> 

    <Button 
     android:id="@+id/id_add_new" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:text="@string/s_add_new" 
     android:textColor="#FFFFFF" 
     android:background="#5E553A"/> 

</LinearLayout> 

EditNote.java

package com.cidecode.xnotes; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 


public class EditNote extends ActionBarActivity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_edit_note, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

activity_edit_note.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:background="#EBD28F"> 

    <TextView 
     android:id="@+id/id_add_new_note" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/s_edit_note" 
     android:gravity="center" 
     android:textSize="30sp" 
     android:layout_marginBottom="10dp"/> 

    <EditText 
     android:id="@+id/id_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/s_title" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginBottom="5dp"/> 

    <EditText 
     android:id="@+id/id_write_note_here" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:hint="@string/s_write_note_here" 
     android:gravity="top" /> 

    <Button 
     android:id="@+id/id_save" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:text="@string/s_save" 
     android:textColor="#FFFFFF" 
     android:background="#5E553A"/> 

</LinearLayout> 
+2

誰會讀你的完整代碼。請查明您的問題並獲得快速幫助。 –

+0

我做到了。如何從mainactivity.java中的listview(onitemclick)中獲取項目標識並將其過去以編輯note.java。 – user3759323

回答

1

它是一種方法,通過COLUMN_ID

獲得單注
 public Notes getSingleNote(long id){ 

      Cursor cursor = database.query(DatabaseHelper.TABLE_NOTES, allColumns, 
        COLUMN_ID +"=?", new String[]{String.valueOf(id)}, null, null, null); 

      Note note = null; 
      if(cursor.moveToFirst()) 
       note = cursorToNote(cursor); 
      cursor.close(); 
      return note; 
     } 
0

聲明List<Notes> values全球。並更新您的listView項目點擊收聽者,如下所述:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Notes selectedNote = values.get(position);  
      } 
     }); 

這裏「selectedNote」選定的項目。並通過以下鏈接傳遞此項目。

How do I pass an object from one activity to another on Android?

+0

你可以幫我轉移對象嗎?我寫了新的評論。謝謝。 – user3759323

+0

請遵循以下鏈接中的Parcelable概念。使對象Notes實現Parcelable。從附加鏈接中查看「步驟3:」部分和「PacelableMethod()」方法。 http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html –

+0

請接受答案,以便他人今後可以參考同樣的問題。 –